查看分支信息
git branch
git branch 命令会列出所有分支,当前分支前面会标一个*
号,效果如下所示:
anyang@anyang:~/anyang/learngit$ git branch
* master
创建分支
git branch <name>
效果如下图所示:
anyang@anyang:~/anyang/learngit$ git branch test
anyang@anyang:~/anyang/learngit$ git branch
- master
test
git checkout 命令加上 -b 参数表示创建并切换分支,例如:git checkout -b <name>
,效果如下所示:
anyang@anyang:~/anyang/learngit$ git checkout -b test1
Switched to a new branch 'test1'
anyang@anyang:~/anyang/learngit$ git branch
master
test
* test1
切换分支
git checkout <name>
效果如下所示:
anyang@anyang:~/anyang/learngit$ git branch
master
test
* test1
anyang@anyang:~/anyang/learngit$ git checkout test
Switched to branch 'test'
anyang@anyang:~/anyang/learngit$ git branch
master
* test
test1
合并指定分支到当前分支
git merge <name>
合并分支时,加上--no-ff 参数就可以用普通模式进行合并,即合并后的历史能够看出曾经发生过合并,默认情况下,Git 采用 fast forward 方式进行分支合并,即通过指针移动完成的分支合并,因此看不出曾经发生过合并,效果图如下所示:
anyang@anyang:~/anyang/learngit$ git branch
master
* test
test1
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
I am test.
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ git commit -m "Add I am test"
[test 026803a] Add I am test
1 file changed, 1 insertion(+)
anyang@anyang:~/anyang/learngit$ git checkout master
Switched to branch 'master'
anyang@anyang:~/anyang/learngit$ git branch
删除指定分支
git branch -d <name>
如果要删除一个没有被合并过的分支,可以通过git branch -D <name>
强行删除,效果图如下所示:
anyang@anyang:~/anyang/learngit$ git branch
* master
test
test1
anyang@anyang:~/anyang/learngit$ git branch -d test
Deleted branch test (was 026803a).
anyang@anyang:~/anyang/learngit$ git branch
* master
test1
anyang@anyang:~/anyang/learngit$ git checkout test1
Switched to branch 'test1'
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
I am test1.
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ git commit -m "Add I am test1"
[test1 2cc6751] Add I am test1
1 file changed, 1 insertion(+)
anyang@anyang:~/anyang/learngit$ git checkout master
Switched to branch 'master'
anyang@anyang:~/anyang/learngit$ git branch -d test1
error: The branch 'test1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D test1'.
anyang@anyang:~/anyang/learngit$ git branch -D test1
Deleted branch test1 (was 2cc6751).
anyang@anyang:~/anyang/learngit$ git branch
* master
查看分支合并图
git log --graph --pretty=oneline --abbrev-commit
假设我们新建分支 branch1,并添加“Hello, branch1.”到 README.md 文件,紧接着切换到 master 分支,添加“Hello, master.”到 README.md 文件,然后合并 branch1 分支到 master 分支,发生了冲突,通过查看冲突文件 README.md,修改 master 分支的文件和 branch1 分支相同,再重新合并即可,效果如下图所示:
anyang@anyang:~/anyang/learngit$ git branch
* master
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
anyang@anyang:~/anyang/learngit$ git checkout -b branch1
M README.md
Switched to a new branch 'branch1'
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ git commit -m "Add Hello, branch1"
[branch1 e9d1cb7] Add Hello, branch1
1 file changed, 1 insertion(+), 1 deletion(-)
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
Hello, branch1.
anyang@anyang:~/anyang/learngit$ git checkout master
Switched to branch 'master'
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
Hello, master.
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ git commit -m "Add Hello, master"
[master 16a1460] Add Hello, master
1 file changed, 1 insertion(+), 1 deletion(-)
anyang@anyang:~/anyang/learngit$ git merge branch1
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
anyang@anyang:~/anyang/learngit$ 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.md
no changes added to commit (use "git add" and/or "git commit -a")
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
Hello, branch1.
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ git commit -m "conflict fixed"
[master 44d9f6f] conflict fixed
anyang@anyang:~/anyang/learngit$ git merge branch1
Already up-to-date.
anyang@anyang:~/anyang/learngit$ git log --graph --pretty=oneline --abbrev-> > commit
* 44d9f6f conflict fixed
|\
| * e9d1cb7 Add Hello, branch1
* | 16a1460 Add Hello, master
|/
* 026803a Add I am test
* 513dec6 Add Hello, world!
* 72f8209 Add README.md
相关资料:
- Git 官网
- Git 官方文档
- [廖雪峰的 Git 教程](http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8
067c8c017b000) - Git 常用命令查询文档
- Git 在线学习网址