git分支操作

git分支操作

  1. 查看分支:git branch -v

    $ git branch -v
    
    * master 9e2aa44 second commit
    

    *代表当前所在分支

2.创建分支 git branch 分支名

$ git branch hot-fix

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git branch -v
  hot-fix 9e2aa44 second commit

* master  9e2aa44 second commit

3.切换分支 git checkout 分支名

$ git checkout hot-fix
Switched to branch 'hot-fix'

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git branch -v

* hot-fix 9e2aa44 second commit
  master  9e2aa44 second commit

在切换的新分支hot-fix上编写代码后,要提交到本地库

$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git status
On branch hot-fix
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.txt

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git commit -m "在新的分支上编写代码" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[hot-fix b593135] 在新的分支上编写代码
 1 file changed, 1 insertion(+)

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git status
On branch hot-fix
nothing to commit, working tree clean

4.合并分支(正常合并)git merge 分支名 (把指定分支合并到当前分支上) 通俗点讲如果要把hot-fix合并到master上要先切换分支到master上

$ git checkout master
Switched to branch 'master'

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git merge hot-fix
Updating 9e2aa44..b593135
Fast-forward
 hello.txt | 1 +
 1 file changed, 1 insertion(+)

5.合并分支(冲突合并)

冲突产生的原因:

合并分支时,两个分支对同一个位置(这里的同一个位置指的是同一个文件)(两同)作出修改,git无法决定使用哪一个,必须人为决定

(1)先修改hot-fix分支的hello.txt的内容,在hello.txt文件最后一行作出改动,然后提交到本地库

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ vim hello.txt

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git status
On branch hot-fix
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git add hello.txt

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git commit -m "hot-fix修改" hello.txt
[hot-fix ce5e34d] hot-fix修改
 1 file changed, 1 insertion(+), 1 deletion(-)
g
Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git status
On branch hot-fix
nothing to commit, working tree clean

(2)切回master分支,再先修改master分支的hello.txt的内容,在hello.txt文件最后一行作出改动,然后提交到本地库

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (hot-fix)
$ git checkout master
Switched to branch 'master'

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ vim hello.txt

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$  git add hello.txt

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (master)
$ git commit -m "master修改" hello.txt
[master a77710b] master修改
 1 file changed, 1 insertion(+), 1 deletion(-)

(3)合并分支 出现冲突

Administrator@BF-202006081840 MINGW64 /g/git-space/git-demo (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.

(4)人为解决冲突

手动打开冲突的文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uVpOzyIP-1656856626665)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220703213829839.png)]

备注:

<<<<<<< HEAD

xxxxxxxxxxxxxxx

=======

中的内容指的是当前分支的内容

而绿色框中指的被合并分支的内容

(5)解决完冲突后要再次git add 和 git commit -m “备注” (注意 git commit -m “备注” 后面不能再跟文件名

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6kZizJ42-1656856626666)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220703215126344.png)]

如果 git commit -m “备注” 后面跟了文件名会出现错误:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d0Ov25LZ-1656856626669)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220703215252382.png)]

合并只会修改master分支的内容,并不会修改被合并(hot-fix)分支的内容,切回hot-fix分支查看hello.txt文件没有发生改变

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XhGJccOY-1656856626671)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220703215550564.png)]

-m “备注” 后面跟了文件名会出现错误:

[外链图片转存中…(img-d0Ov25LZ-1656856626669)]

合并只会修改master分支的内容,并不会修改被合并(hot-fix)分支的内容,切回hot-fix分支查看hello.txt文件没有发生改变

[外链图片转存中…(img-XhGJccOY-1656856626671)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值