Git 实用教程(三)_分支操作

Git 分支相当轻量化,因为分支工作的本质是指针在起作用。

一、查看和创建分支

命令:git branch

使用方法:

  • 无参数,查看所有分支

git branch

  • 创建一个分支,创建的分支内容和当前分支一模一样

git branch <name>

实际操作:

//注:操作系统 Centos7,下同
[slashwan@study test]$ git branch
* master
[slashwan@study test]$ git branch develop  //创建一个 develop 分支
[slashwan@study test]$ git branch          //现在有两个分支
  develop
* master

当前工作分支前有 * 标记。

二、切换分支

命令:git checkout

使用方法:

  • 切换至某分支

git checkout <name>

  • 创建新分支并切换

git checkout -b <name>

实际操作:
通常主分支 master 保持稳定,切换至开发分支 develop 进行开发:

[slashwan@study test]$ git checkout develop
Switched to branch 'develop'

突然发现主分支有 bug ,赶紧建个新分支进行维护:

[slashwan@study test]$ git checkout master  //先回主分支
Switched to branch 'master'
[slashwan@study test]$ git checkout -b bug  //创建个 bug 分支并切换
Switched to a new branch 'bug'

三、合并分支

命令:git merge

使用方法:

  • 合并某分支到当前分支

git merge <name>

  • 合并分支时,禁用 Fast-forward 功能

git merge --no-ff -m <message> <name>

实际操作:
文件中没有自我介绍,在 bug 分支添加自我介绍的内容:

# study Git
print ('Hello, my name is slashwan, could you tell me your name please?')

接着在 bug 分支提交:

[slashwan@study test]$ git add hello.py
[slashwan@study test]$ git commit -m "my name"
[bug a4682b5] my name
 1 file changed, 1 insertion(+), 1 deletion(-)

回到主分支 master ,把 bug 分支合并过来:

[slashwan@study test]$ git checkouut master
Switched to branch 'master'
[slashwan@study test]$ git merge bug
Updating d82b985..a4682b5
Fast-forward
 hello.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

四、删除分支

命令:git branch -d

使用方法:

  • 删除某分支

git branch -d <name>

  • 强制删除某分支

git branch -D <name>

实际操作:
切换到 develop 分支删除 bug 分支试试,

[slashwan@study test]$ git checkouut develop
Switched to branch 'develop'
[slashwan@study test]$ git branch -d bug
error: The branch 'bug' is not fully merged.
If you are sure you want to delete it, run 'git branch -D bug'.

返回了错误信息,因为我们是在 master 分支合并 bug 分支的,对于 develop 分支来说,bug 分支还未合并,虽然可以用参数 -D 强制删除,但不推荐使用,为避免出错,还是回到 master 分支删除吧。

[slashwan@study test]$ git checkouut master
Switched to branch 'master'
[slashwan@study test]$ git branch -d bug
Deleted branch bug (was d82b985).

删除成功。

五、解决冲突

冲突是在合并分支时可能发生的问题,需要手动解决,下面操作特意制造了一次冲突。

实际操作:
在 develop 分支修改文件,添加一个换行符 \n,

# study git
print ('Hello, \nmy name is slashwan, could you tell me your name please?'

并提交:

[slashwan@study test]$ git add hello.py
[slashwan@study test]$ git commit  -m "add one \n"
[develop 7645b1d] add one \n
 1 file changed, 1 insertion(+), 1 deletion(-)

在 master 分支修改文件,添加两个换行符 \n,

# study git
print ('Hello, \nmy name is slashwan, \ncould you tell me your name please?'

并提交:

[slashwan@study test]$ git checkout master
Switched to branch 'master'
[slashwan@study test]$ git add hello.py
[slashwan@study test]$ git commit -m "add two \n"
[master 9c334be] add two \n
 1 file changed, 1 insertion(+), 1 deletion(-)

在 master 分支合并 develop 分支:

[slashwan@study test]$ git merge develop
Auto-merging hello.py
CONFLICT (content): Merge conflict in hello.py  //告诉我们发生了冲突,冲突文件也写出来了
Automatic merge failed; fix conflicts and then commit the result.

打开冲突的文件,手动解决:

# study git
<<<<<<< HEAD
print ('Hello, \nmy name is slashwan, \ncould you tell me your name please?')
=======
print ('Hello, \nmy name is slashwan, could you tell me your name please?')
>>>>>>> develop

Git 用 <<<<<<<=======>>>>>>> 标记了有冲突的部分及冲突来源,手动修改为:

# study git
print ('Hello, \nmy name is slashwan, \ncould you tell me your name please?')

再次提交至仓库,冲突解决:

[slashwan@study test]$ git add hello.py
[slashwan@study test]$ git commit -m "resolve conflict“
[master 9faffd8] resolve conflict
[slashwan@study test]$ git branch -d develop
Deleted branch develop (was 7645b1d).

六、隐藏操作

有时遇到代码写到一半,要新建分支修复 Bug 或者其他情况。这一半没有什么实际意义的代码是不建议 commit 的,否则提交日志的可读性会很差。这时 git stash 命令就有用了,它会把当前工作隐藏,切换到其他地方完成任务后,再回来显示隐藏接着工作就行了,执行了 git add 也可以使用 stash。

命令:git stash

使用方法:

  • 隐藏操作的相关指令

git stash(隐藏当前的修改)
git list(查看所有隐藏,冒号前是此隐藏的 id)
git stash apply <id>(重新显示 id 的隐藏)
git stash drop <id>(删除 list 列表中 id 的记录)
git stash pop <id>(显示并删除 id 的隐藏)

实际操作:
在 develop 分支开发输入名字的功能,进行了一半,

# study git
print ('Hello, \nmy name is slashwan, \ncould you tell me your name please?')
name = input ('Please write your name here: ')
print (name)

发现 master 分支有 bug,隐藏当前工作,切换至 master 处理问题:

[slashwan@study test]$ git stash
Saved working directory and index state WIP on develop: 7d22906 resolve conflict
HEAD is now at 7d22906 resolve conflict
[slashwan@study test]$ git checkout master 
Switched to branch 'master'
[slashwan@study test]$ git checkout master -b bug
Switched to a new branch 'bug'

修改一下标点符号和首字母的大写:

# study git
print ('Hello! \nMy name is slashwan! \nCould you tell me your name please?')

提交修改,然后合并 bug 分支:

[slashwan@study test]$ git add hello.py
[slashwan@study test]$ git commit -m "debug"
[bug 91d8e62] debug
 1 file changed, 2 insertions(+), 1 deletion(-)
[slashwan@study test]$ git checkout master  //切换至 master
Switched to branch 'master'
[slashwan@study test]$ git merge bug  //合并 bug 分支
Updating 7d22906..91d8e62
Fast-forward
 hello.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

此时,develop 分支还处于有 bug 的版本,让它也合并 bug 分支更新:

[slashwan@study test]$ git checkout develop  //切换至 develop
Switched to branch 'develop'
[slashwan@study test]$ git merge bug  //合并 bug
Updating 7d22906..91d8e62
Fast-forward
 hello.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

上方 develop 隐藏记录还未还原,合并分支很顺利,接下来可能会有麻烦,

[slashwan@study test]$ git stash list  //查看隐藏记录
stash@{0}: WIP on develop: 7d22906 resolve conflict
[slashwan@study test]$ git stash apply  //显示隐藏记录
Auto-merging hello.py
CONFLICT (content): Merge conflict in hello.py  //还原过程出现冲突

还原隐藏记录,也可看作是一次合并,因为此时 develop 更新了一次,而隐藏的记录还处于隐藏时的旧版本,所以出现冲突,手动解决。

当然,bug 分支也可以不用合并到 develop 分支,直接删除 bug 分支,这样还原隐藏记录会很顺利,但之后在 develop 开发完合并到 master 时,大概率还是要处理一次冲突,因为 bug 的修改未更新到 develop。

推荐第一种方式,毕竟 bug 刚改完记得清楚,等到开发完合并时冲突,bug 忘了,又是一次新的 debug。

可以看到冲突地方的来源是 Updated upstreamStashed changes

# study git
<<<<<<< Updated upstream
print ('Hello, \nMy name is slashwan, \nCould you tell me your name please?')
=======
print ('Hello, \nmy name is slashwan, \ncould you tell me your name please?')
name = input ('Please write your name here: ')
print (name)
>>>>>>> Stashed changes

解决冲突,并开发完,

# study git
print ('Hello, \nMy name is slashwan, \nCould you tell me your name please?')
name = input ('Please write your name here: ')
print ('Nice to meet you, ' + name + '.')

终于可以提交新功能了:

[slashwan@study test]$ git add hello.py
[slashwan@study test]$ git commit -m "enter name"
[develop 91d8e62] enter name

删除隐藏记录,删除 bug 分支:

[slashwan@study test]$ git stash drop  //删除隐藏记录
Dropped refs/stash@{0} (3518b58b35f19bcd3767644149cefa2a62a5aef0)
[slashwan@study test]$ git brand -d bug  //删除 bug 分支
Deleted branch bug (was 91d8e62).

主分支 master 合并 develop,更新功能:

[slashwan@study test]$ git checkout master
Switched to branch 'master'
[slashwan@study test]$ git merge develop
Updating 91d8e62..8d45906
Fast-forward
 hello.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

推荐阅读:

Git 实用教程_简介

Git 实用教程(二)_本地命令

猜你喜欢

转载自blog.csdn.net/huaxuewan/article/details/85108725