命令名称 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
* master 087a1a7 my third commit (*代表当前所在的分区)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
hot-fix 087a1a7 my third commit (刚创建的新的分支,并将主分支 master的内容复制了一份)
* master 087a1a7 my third commit
--在 master 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ vim hello.txt
--添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git add hello.txt
--提交本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git commit -m "my forth commit" hello.txt
[master f363b4c] my forth commit
1 file changed, 1 insertion(+), 1 deletion(-)
--查看分支
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
hot-fix 087a1a7 my third commit (hot-fix 分支并未做任何改变)
* master f363b4c my forth commit (当前 master 分支已更新为最新一次提交
的版本)
--查看 master 分支上的文件内容
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ cat hello.txt
hello git! hello alex! 2222222222222
hello git! hello alex! 3333333333333
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex! master test
hello git! hello alex!
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
--发现当先分支已由 master 改为 hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$
--查看 hot-fix 分支上的文件内容发现与 master 分支上的内容不同
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ cat hello.txt
hello git! hello alex! 2222222222222
hello git! hello alex! 3333333333333
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
--在 hot-fix 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ cat hello.txt
hello git! hello alex! 2222222222222
hello git! hello alex! 3333333333333
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex! hot-fix test
--添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git add hello.txt
--提交本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git commit -m "hot-fix commit" hello.txt
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ cat hello.txt
hello git! hello alex! 2222222222222
hello git! hello alex! 3333333333333
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
<<<<<<< HEAD
hello git! hello alex! master test
hello git! hello alex!
=======
hello git! hello alex!
hello git! hello alex! hot-fix test
>>>>>>> hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ 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 ..." to mark resolution)
both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
hello git! hello alex! 2222222222222
hello git! hello alex! 3333333333333
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex!
hello git! hello alex! master test
hello git! hello alex! hot-fix test
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ git add hello.txt
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ git commit -m "merge hot-fix"
[master 69ff88d] merge hot-fix
--发现后面 MERGING 消失,变为正常
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$
master、hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD 决定的。所以创建分支的本质就是多创建一个指针。
HEAD 如果指向 master,那么我们现在就在 master 分支上。
HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上。
所以切换分支的本质就是移动 HEAD 指针。