Git 保姆级教程(二):Git 分支

简介: Git 保姆级教程(二):Git 分支

一、分支简介

1.1 git branch name(创建分支)

比如,创建一个 testing 分 支, 你需要使用 git branch 命令:

[root@localhost git_study]# git branch testing

你可以简单地使用 git log 命令查看各个分支当前所指的对象。 提供这一功能的参数是 --decorate

[root@localhost git_study]# git log --oneline --decorate
2abbca3 (HEAD, tag: v1.1, tag: v1.0, testing, mian) belive youself
550e697 --amend 55
03f8d1d basic-2.8(git commit -a)
bb2a7f8 basic-2.7(git commit -m)
bdd3afc (tag: v0.1) basic-2.7(gitcommit)

1.2 git checkout name(分支切换)

要切换到一个已存在的分支,你需要使用 git checkout 命令

[root@localhost git_study]# git checkout testing 
切换到分支 'testing'
[root@localhost git_study]# git log --oneline --decorate
2abbca3 (HEAD -> testing, tag: v1.1, tag: v1.0, mian) belive youself
550e697 --amend 55
03f8d1d basic-2.8(git commit -a)
bb2a7f8 basic-2.7(git commit -m)
bdd3afc (tag: v0.1) basic-2.7(gitcommit)

这样 HEAD 就指向 testing 分支了

你可以简单地使用 git log 命令查看分叉历史。 运行 git log --oneline --decorate --graph --all

$ git log --oneline --decorate --graph --all
* c2b9e (HEAD, master) made other changes
| * 87ab2 (testing) made a change
|/
* f30ab add feature #32 - ability to add new formats to the
* 34ac2 fixed bug #1328 - stack overflow under certain conditions
* 98ca9 initial commit of my project

二、分支的新建与合并

2.1 git checkout -b name(新建并切换分支)

[root@localhost git_study]# git checkout -b clc
切换到一个新分支 'clc'
[root@localhost git_study]# git log --oneline --decorate
2abbca3 (HEAD -> clc, tag: v1.1, tag: v1.0, testing, mian) belive youself
550e697 --amend 55
03f8d1d basic-2.8(git commit -a)
bb2a7f8 basic-2.7(git commit -m)
bdd3afc (tag: v0.1) basic-2.7(gitcommit)

2.2 git merge name(分支的合并)

你只需要找到你想合并入的分支,然后运行 git merge 命令:

注意:合并只能在旧分支中合并新分支

[root@localhost git_study]# git checkout mian
切换到分支 'mian'
[root@localhost git_study]# cat target.txt 
CET-4 and CET-6
[root@localhost git_study]# git merge clc
更新 2abbca3..939c2db
Fast-forward
 target.txt | 2 ++
 1 file changed, 2 insertions(+)
[root@localhost git_study]# cat target.txt 
CET-4 and CET-6
 
Win the Game

2.3 git branch -d name(删除分支)

1. [root@localhost git_study]# git branch -d testing 
2. 已删除分支 testing(曾为 2abbca3)。

2.4 遇到冲突时的分支合并

如果你在两个不同的分支中,对同一个文件的同一个部分进行了不同的修改,Git 就没法干净的合并它们

$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

此时 Git 做了合并,但是没有自动地创建一个新的合并提交。 Git 会暂停下来,等待你去解决合并产生的冲突。 你可以在合并冲突后的任意时刻使用 git status 命令来查看那些因包含合并冲突而处于未合并

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
Unmerged paths:
  (use "git add <file>..." to mark resolution)
  both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")

三、分支管理

3.1 git branch(查看分支列表)

git branch 命令不只是可以创建与删除分支。 如果不加任何参数运行它,会得到当前所有分支的一个列表:

[root@localhost git_study]# git branch
* clc
  mian
  morant
  testing

3.2 git branch(查看最后一次提交)

如果需要查看每一个分支的最后一次提交,可以运行 git branch -v 命令:

[root@localhost git_study]# git branch -v
* clc     939c2db win the game
  mian    939c2db win the game
  morant  939c2db win the game
  testing 2abbca3 belive youself

3.3 git branch --merged name(查看当前已合并的分支)

如果要查看哪些分支已经合并到当前分支,可以运行 git branch --merged:

$ git branch --merged
  iss53
* master
 git branch --no-merged master
  topicA
  featureB

3.4 git branch --no-merged name(查看当前未合并的分支)

$ git branch --no-merged
  testing
[root@localhost git_study]# git branch --no-merged mian 

四、远程分支

4.1 git push origin name(推送)

如果希望和别人一起在名为 serverfix 的分支上工作,你可以像推送第一个分支那样推送它。 运行 git push :  

$ git push origin serverfix
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done.
Total 24 (delta 2), reused 0 (delta 0)
To https://github.com/schacon/simplegit
 * [new branch] serverfix -> serverfix

4.2 跟踪分支

在Git中,跟踪分支(tracking branch)是一个特殊的本地分支,它与一个远程分支有直接的联系。这种联系意味着当你执行某些Git命令(如git pullgit push)时,Git会自动知道应该与哪个远程分支进行交互

这是一个十分常用的操作所以 Git 提供了 --track 快捷方式:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

如果你尝试检出的分支 (a) 不存在且 (b) 刚好只有一个名字与之匹配的远程分支,那么 Git 就会为你创建一个跟踪分支:

$ git checkout serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'


如果想要将本地分支与远程分支设置为不同的名字,你可以轻松地使用上一个命令增加一个不同名字的本地分支:

$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch serverfix from origin.
Switched to a new branch 'sf'

设置已有的本地分支跟踪一个刚刚拉取下来的远程分支,或者想要修改正在跟踪的上游分支, 你可以在任意时间使用 -u 选项运行 git branch 来显式地设置

$ git branch -u origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.

如果想要查看设置的所有跟踪分支,可以使用 git branch 的 -vv 选项

$ git branch -vv
  iss53 7e424c3 [origin/iss53: ahead 2] forgot the brackets
  master 1ae2a45 [origin/master] deploying index fix
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this
should do it
  testing 5ea463a trying something new

如果想要查看设置的所有跟踪分支,可以使用 git branch 的 -vv 选项

$ git branch -vv
  iss53 7e424c3 [origin/iss53: ahead 2] forgot the brackets
  master 1ae2a45 [origin/master] deploying index fix
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this
should do it
  testing 5ea463a trying something new

4.3 删除远程分支

可以运行带有 --delete 选项的 git push 命令 来删除一个远程分支

$ git push origin --delete serverfix
To https://github.com/schacon/simplegit
 - [deleted] serverfix

五、变基

具体来说,当你有一个分支(比如feature分支)是基于另一个分支(比如master分支)拉出来的,而这个master分支在后续又有新的提交,那么此时可以用master上的这些新提交来作为feature分支的新基底

你可以使用 rebase 命令将提交到某一分支上的所有修改都移至另一分支上

$ git checkout experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command


相关文章
|
21天前
|
项目管理 开发工具 git
Git项目管理——分支(三)
Git项目管理——分支(三)
20 2
|
1月前
|
JSON 开发工具 git
git rebase 合并当前分支的多个commit记录
git rebase 合并当前分支的多个commit记录
|
22天前
|
开发工具 git
git切换到另一分支更改也会随之过去
git切换到另一分支更改也会随之过去
20 1
|
22天前
|
开发工具 git
git将一个远程分支的部分修改提交到另一个远程分支
git将一个远程分支的部分修改提交到另一个远程分支
12 1
|
15天前
|
敏捷开发 测试技术 持续交付
【git分支管理策略】如何高效的管理好代码版本
【git分支管理策略】如何高效的管理好代码版本
32 0
|
1月前
|
存储 开发工具 git
|
1月前
|
开发工具 git
git 如何删除本地和远程分支
git 如何删除本地和远程分支
79 0
|
10月前
|
开发工具 git
git 操作之合并其它分支的某次提交(commits)到当前分支
git cherry-pick合并其它分支的某次提交(commits)到当前分支
253 0
|
10月前
|
数据可视化 Go 开发工具
cggit 简化 Git 提交、合并、分支偏移小神器,提升开发、修BUG效率!
cggit 简化 Git 提交、合并、分支偏移小神器,提升开发、修BUG效率!
317 0
|
11月前
|
开发工具 git
GIT合并分支的三种方法
GIT合并分支的三种方法
1639 0