GitGit分支管理
Posted 王六六的IT日常
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GitGit分支管理相关的知识,希望对你有一定的参考价值。
参考👉Git 教程
使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。
有人把 Git 的分支模型称为必杀技特性,而正是因为它,将 Git 从版本控制系统家族里区分出来。
创建分支命令:
git branch (branchname)
切换分支命令:
git checkout (branchname)
当切换分支的时候,Git 会用该分支的最后提交的快照替换你的工作目录的内容, 所以多个分支不需要多个目录。
创建一个测试目录:
Administrator@Wanzi-PC MINGW64 ~
$ cd /g
Administrator@Wanzi-PC MINGW64 /g
$ cd Git
Administrator@Wanzi-PC MINGW64 /g/Git
$ mkdir gitdemo
Administrator@Wanzi-PC MINGW64 /g/Git
$ cd gitdemo/
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo
$ git init
Initialized empty Git repository in G:/Git/gitdemo/.git/
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ touch README
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git add README
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git commit -m '第一次版本提交'
[master (root-commit) a0585cb] 第一次版本提交
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
列出分支以及创建新分支testing:
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git branch
* master
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$
$ git branch testing
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git branch
* master
testing
演示如何切换分支,我们用 git checkout (branch)
切换到我们要修改的分支。
当我们切换到 testing 分支的时候,我们添加的新文件 test.txt 被移除了。切换回 master 分支的时候,它们有重新出现了。
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ ls
README
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ echo 'runoob.com' > test.txt
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git add .
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git commit -m 'add test.txt'
[master 9eb916a] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ ls
README test.txt
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git checkout testing
Switched to branch 'testing'
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (testing)
$ ls
README
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (testing)
$ git checkout master
Switched to branch 'master'
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ ls
README test.txt
也可以使用 git checkout -b branchname
命令来创建新分支并立即切换到该分支下,从而在该分支中操作。
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git checkout -b newtets
Switched to a new branch 'newtets'
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (newtets)
$ git rm test.txt
rm 'test.txt'
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (newtets)
$ ls
README
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (newtets)
$ touch runoob,php
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (newtets)
$ git add .
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (newtets)
$ git commit -am 'removed test.txt、add runoob.php'
[newtets d9764ea] removed test.txt、add runoob.php
2 files changed, 1 deletion(-)
create mode 100644 runoob,php
delete mode 100644 test.txt
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (newtets)
$ ls
README runoob,php
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (newtets)
$ git checkout master
Switched to branch 'master'
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ ls
README test.txt
创建了一个分支newtets,在该分支的上移除了一些文件 test.txt,并添加了 runoob.php 文件,然后切换回我们的主分支master,删除的 test.txt 文件又回来了,且新增加的 runoob.php 不存在主分支中。
使用分支将工作切分开来,从而让我们能够在不同开发环境中做事,并来回切换。
删除分支
git branch -d branchname
分支合并
一旦某分支有了独立内容,你终究会希望将它合并回到你的主分支。
使用以下命令将任何分支合并到当前分支中去:
git merge
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git branch
* master
newtets
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ ls
README test.txt
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ git merge newtets
Updating 9eb916a..d9764ea
Fast-forward
runoob,php | 0
test.txt | 1 -
2 files changed, 1 deletion(-)
create mode 100644 runoob,php
delete mode 100644 test.txt
Administrator@Wanzi-PC MINGW64 /g/Git/gitdemo (master)
$ ls
README runoob,php
以上实例中我们将 newtets分支合并到主分支去,test.txt 文件被删除。
合并完后就可以删除分支:
$ git branch -d newtets
Deleted branch newtets(was c1501a2).
删除后, 就只剩下 master 分支了:
$ git branch
* master
vim编辑文件👉i进行插入👉Esc+:wq👉退出编辑
以上是关于GitGit分支管理的主要内容,如果未能解决你的问题,请参考以下文章
GitGit 分支管理 ( 解决分支合并冲突 | 创建并切换分支 git switch -c feature1 | 修改 feature1 分支并提交 | 修改 master 主版本并提交 )(代码片
GitGit 分支管理 ( 解决分支合并冲突 | 创建并切换分支 git switch -c feature1 | 修改 feature1 分支并提交 | 修改 master 主版本并提交 )(代码片
GitGit 分支管理 ( 解决分支合并冲突 | 本地处理文件冲突 )