yhr@VM-24-15-ubuntu:~/gitcode$ git branch dev yhr@VM-24-15-ubuntu:~/gitcode$ git branch dev * master
我们发现此时 heads 多了一个我们刚刚创建的 dev 分支
<3> 切换分支(git checkout)
此时 dev 分支指向的也是最近一次的提交,但是,进行修改等操作还是只能在 master 分支进行操作,此时如果想要切换到 dev 分支下进行操作该怎么办呢?
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout dev Switched to branch 'dev' yhr@VM-24-15-ubuntu:~/gitcode$ git branch * dev master yhr@VM-24-15-ubuntu:~/gitcode$ cat .git/HEAD ref: refs/heads/dev
接下来,我们在 dev 分支下对 ReadMe 文件新增一行代码:
yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git add ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "md ReadMe" [dev a2c0656] md ReadMe 1 file changed, 1 insertion(+) yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch dev nothing to commit, working tree clean yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git aaa on dev branch
我们在 dev 分支下对 ReadMe 文件修改,现在切换回 master 分支,看一下 ReadMe 文件:
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master Switched to branch 'master' yhr@VM-24-15-ubuntu:~/gitcode$ git branch dev * master yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git
我们发现,在 master 分支下,ReadMe 文件并没有变化,我们新增的那一行代码并没有显示出来,我们再切回 dev 分支:
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout dev Switched to branch 'dev' yhr@VM-24-15-ubuntu:~/gitcode$ git branch * dev master yhr@VM-24-15-ubuntu:~/gitcode$ ls file1 ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git aaa on dev branch
在 dev 分支上,内容还在。为什么会出现这个现象呢?我们来看看 dev 分支和 master 分支的指向,会发现两者的指向是不同的:
为了在 master 主分支上能看到新的提交,就需要将 dev 分支合并到 master 分支,合并分支的核心逻辑是'将源分支的提交记录合并到目标分支',需先切换到目标分支,再执行合并命令
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master Switched to branch 'master' yhr@VM-24-15-ubuntu:~/gitcode$ git merge dev Updating ebc48f7..a2c0656 Fast-forward ReadMe | 1 + 1 file changed, 1 insertion(+) yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git aaa on dev branch
git merge 分支名命令用于合并指定分支到当前分支。合并后,master 就能看到 dev 分支提交的内容
我们会看到上面合并之后,会出现 Fast-forward 代表'快进模式',也就是直接把 master 指向 dev 的当前提交,所以合并速度非常快。当然,也不是每次合并都能 Fast-forward,我们后面还没讲其它方式的合并
3.2 删除分支(git branch -d 分支名):
合并完成后,dev 分支对于我们来说就没用了,那么 dev 分支就可以删除掉,注意如果当前正处于某个分支下,就不能删除当前分支,如:
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout dev Switched to branch 'dev' yhr@VM-24-15-ubuntu:~/gitcode$ git branch -d dev error: Cannot delete branch 'dev' checked out at '/home/yhr/gitcode'
而可以在其它分支下删除当前分支,如:
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master Switched to branch 'master' yhr@VM-24-15-ubuntu:~/gitcode$ git branch -d dev Deleted branch dev (was a2c0656). yhr@VM-24-15-ubuntu:~/gitcode$ git branch * master
yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git bbb on dev branch yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master 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 no changes added to commit (use "git add" and/or "git commit -a") yhr@VM-24-15-ubuntu:~/gitcode$ git add . yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "merge dev1" [master de63d7e] merge dev1 yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master nothing to commit, working tree clean yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git bbb on dev branch
yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git bbb on dev branch abcde i am coding... yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: ReadMe no changes added to commit (use "git add" and/or "git commit -a")
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout dev2 M ReadMe Switched to branch 'dev2' yhr@VM-24-15-ubuntu:~/gitcode$ git stash Saved working directory and index state WIP on dev2: ba59442 md ReadMe
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master Switched to branch 'master' yhr@VM-24-15-ubuntu:~/gitcode$ git checkout -b fix_bug Switched to a new branch 'fix_bug' yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git add . yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "fix bug:f" [fix_bug e035682] fix bug:f 1 file changed, 1 insertion(+), 1 deletion(-)
修复完成后,切换到 master 分支,并完成合并:
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master Switched to branch 'master' yhr@VM-24-15-ubuntu:~/gitcode$ git merge --no-ff -m "merge fix_bug" fix_bug Merge made by the 'ort' strategy. ReadMe | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) yhr@VM-24-15-ubuntu:~/gitcode$ git status On branch master nothing to commit, working tree clean yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git bbb on dev branch abcdef
bug 的修复工作已经做完了,我们还要继续回到 dev2 分支进行开发,切换回 dev2 分支,工作区是干净的,刚才的工作存到哪里去了?用 git stash list 命令看看:
yhr@VM-24-15-ubuntu:~/gitcode$ git stash list stash@{0}: WIP on dev2: ba59442 md ReadMe
工作现场还在,Git 把 stash 内容存在某个地方了,但是需要恢复一下,如果恢复现场呢?我们可以使用 git stash pop 命令,恢复的同时会把 stash 也删了,示例如下:
yhr@VM-24-15-ubuntu:~/gitcode$ git stash pop On branch dev2 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: ReadMe no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (c3c2100ccdccd938ee9b96b7456ee29bf7cfb5e00)
恢复完代码之后我们还可以继续完成开发,开发完后就可以提交了,例如:
yhr@VM-24-15-ubuntu:~/gitcode$ cat ReadMe Hello git Hello Git bbb on dev branch abcde i am coding... yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git add . yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "md ReadMe:Done" [dev2 10e7b71] md ReadMe:Done 1 file changed, 2 insertions(+) yhr@VM-24-15-ubuntu:~/gitcode$ git branch * dev2 fix_bug master
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout -b dev3 Switched to a new branch 'dev3' yhr@VM-24-15-ubuntu:~/gitcode$ vim ReadMe yhr@VM-24-15-ubuntu:~/gitcode$ git add . yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "md ReadMe" [dev3 63bfcad] md ReadMe 1 file changed, 1 insertion(+) yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master Switched to branch 'master' yhr@VM-24-15-ubuntu:~/gitcode$ git branch -d dev3 #删除失败 error: The branch 'dev3' is not fully merged. If you are sure you want to delete it, run 'git branch -D dev3'.