分支管理
创建与合并分支
首先,我们创建dev
分支,然后切换到dev
分支:
$ git checkout -b dev Switched to a new branch 'dev'
git checkout
命令加上-b
参数表示创建并切换,相当于以下两条命令:
$ git branch dev $ git checkout dev Switched to branch 'dev'
然后,用git branch
命令查看当前分支:
$ git branch
* dev
master
git branch
命令会列出所有分支,当前分支前面会标一个*
号。
查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>
或者git switch <name>
创建+切换分支:git checkout -b <name>
或者git switch -c <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>
解决冲突
git log --graph
命令可以看到分支合并图。
C:\others\learngit>git branch feature1 C:\others\learngit>git switch feature1 Switched to branch 'feature1' C:\others\learngit>git add readme.txt C:\others\learngit>git commit -m "AND simple" [feature1 5fd701b] AND simple 1 file changed, 2 insertions(+), 1 deletion(-) C:\others\learngit>git switch master Switched to branch 'master' Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) C:\others\learngit>git add readme.txt C:\others\learngit>git commit -m "& simple" [master d79f07f] & simple 1 file changed, 2 insertions(+), 1 deletion(-) C:\others\learngit>git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result. C:\others\learngit>notepad readme.txt C:\others\learngit>git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: readme.txt Untracked files: (use "git add <file>..." to include in what will be committed) gitskills/ no changes added to commit (use "git add" and/or "git commit -a") C:\others\learngit>git add readme.txt C:\others\learngit>git commit -m "conflict fixed" [master 8d2ba56] conflict fixed C:\others\learngit>git log --graph --pretty=oneline --abbrev-commit * 8d2ba56 (HEAD -> master) conflict fixed |\ | * 5fd701b (feature1) AND simple * | d79f07f & simple |/ * 0883a8e branch test * af8ceb4 branch test * c10e901 (origin/master) new wcy.txt * 518c738 rm LICENSE * d6658dd git tracks changes * 0093dcb understand how stage works * 3748311 append GPL * 37c24cc add distributed * d73a975 wrote a readme file C:\others\learngit> git branch -d feature1 Deleted branch feature1 (was 5fd701b). C:\others\learngit>
手动
分支管理策略
:\others\learngit>git switch -c dev Switched to a new branch 'dev' C:\others\learngit>notepad readme.txt C:\others\learngit> git add readme.txt C:\others\learngit>git commit -m "add merge" [dev 0b8db7d] add merge 1 file changed, 2 insertions(+), 1 deletion(-) C:\others\learngit>git switch master Switched to branch 'master' Your branch is ahead of 'origin/master' by 5 commits. (use "git push" to publish your local commits) C:\others\learngit>git merge --no-ff -m "merge with no-ff" dev Merge made by the 'recursive' strategy. readme.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) C:\others\learngit>git log --graph --pretty=oneline --abbrev-commit * 8f24561 (HEAD -> master) merge with no-ff |\ | * 0b8db7d (dev) add merge |/ * 8d2ba56 conflict fixed |\ | * 5fd701b AND simple * | d79f07f & simple |/ * 0883a8e branch test * af8ceb4 branch test * c10e901 (origin/master) new wcy.txt * 518c738 rm LICENSE * d6658dd git tracks changes * 0093dcb understand how stage works * 3748311 append GPL * 37c24cc add distributed * d73a975 wrote a readme file C:\others\learngit>
可以看到,不使用Fast forward
模式,merge后就像这样:
合并分支时,加上--no-ff
参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward
合并就看不出来曾经做过合并。
Bug分支