git branch

文章目录
  1. 1. 前言
  2. 2. 创建分支
  3. 3. 上传分支
  4. 4. 删除分支
  5. 5. 恢复已删除分支
  6. 6. 合并分支
    1. 6.1. merge
    2. 6.2. rebase
  7. 7. 书签

前言

Branches in Git are incredibly lightweight as well. They are simply pointers to a specific commit – nothing more. This is why many Git enthusiasts chant the mantra:

branch early, and branch often.

Because there is no storage / memory overhead with making many branches, it’s easier to logically divide up your work than have big beefy branches.

When we start mixing branches and commits, we will see how these two features combine. For now though, just remember that a branch essentially says “I want to include the work of this commit and all parent commits.”

创建分支

1、创建本地bugFix分支
git branch bugFix

2、切换到bugFix分支

1
2
git branch -a
git checkout bugFix

3、创建远程bugFix分支
git push origin HEAD:bugFix
创建远程bugFix2分支
git push origin HEAD:bugFix2

4、下载后切换到bugFix分支
git checkout origin/bugFix

上传分支

在bugFix分支下进行了修改,然后提交修改,命令如下:

1
2
3
git add .
git commit -m "something"
git push origin HEAD:bugFix

删除分支

1、删除本地分支
git branch -D bugFix

2、删除线上分支
git push --delete origin bugFix

恢复已删除分支

1、查看全局日志
git reflog或者git log -g

2、新建分支
git branch bugFix 3eac14d

3、上传分支
git checkout bugFix
git push origin HEAD:bugFix

合并分支

merge

1、修改或添加文件,提交一次
git add .git commit

2、切换回master
git checkout master

3、再次修改或添加文件,提交一次
git add .git commit

4、合并bugFix分支到master
git merge bugFix
如果有冲突,会有提示:

1
2
3
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

5、打开test.txt,可以看到类似如下冲突:

1
2
3
4
5
<<<<<<< HEAD
hello merge master
=======
hello merge bugFix
>>>>>>> bugFix

可以看到 ======= 隔开的上半部分,是 HEAD(即 master 分支,在运行 merge 命令时检出的分支)中的内容,下半部分是在bugFix分支中的内容。解决冲突的办法无非是二者选其一或者由你亲自整合到一起。

6、修改test.txt如下:

1
hello merge master

7、在解决了所有文件里的所有冲突后,运行git add将把它们标记为已解决(resolved)。
然后使用git commit命令进行提交,merge就算完成了。

8、切换到bugFix分支
git checkout bugFix

9、合并master分支到bugFix
git merge master

rebase

假设当前分支是bugFix,git rebase master进行的操作,把bugFix分支起点变成master分支的子节点。整个流程大致如下:

1
2
3
4
5
6
7
git branch bugFix
git checkout bugFix
git commit
git checkout master
git commit
git checkout bugFix
git rebase master

书签

Learn Git Branching
http://learngitbranching.js.org/

git merge简介
http://blog.csdn.net/hudashi/article/details/7664382

Git Book 中文版 - rebase
http://gitbook.liuhui998.com/4_2.html