git

常用命令

创建分支 - git branch

单纯的使用 git branch 命令会显示当前的分支名称,使用 git branch feature1 创建了一个名为 feature1 的分支,git checkout feature1 表示切换到 feature1 分支,git checkout -b feature2 则是上述两个命令的结合,创建并切换到 feature2 分支。

$ git branch # 查看
* master

$ git branch feature1 # 创建
$ git branch
* master
  feature1

$ git checkout feature1 # 变更
Switched to branch 'feature1'

$ git checkout -b feature2 # 创建 + 变更
Switched to a new branch 'feature2'

删除分支 - git branch -d

合并分支 - git merge

$ git branch
* feature2
  feature1
  master

$ git merge feature1 # 此时需要填写 merge 信息,然后就成功将 feature1 的内容合并到 feature2 中

$ git branch -d feature1 # 此时 feature1 就消失了,后面的编号代表它 commit 的值前 7 位
Deleted branch feature1 (was 651f232).

分支合并时冲突

分支合并时冲突主要表现在,两个分支对一份文件有不同的修改,分支 A 中文件写的是 a,分支 B 中文件写的是 ab,分支 C 中文件写的是 ac,合并的时候内容就产生冲突,这时候就需要手动修改内容,完成合并的过程。

# 在 A 分支写下 a.txt 文件,内容是 a
$ git branch
* A
$ cat a.txt
a

# 创建 B 分支,修改 a.txt 文件,内容是 ab
$ git checkout -b B
Switched to a new branch 'B'
$ cat a.txt
ab
$ git branch
  A
* B

# 回到 A 分支,创建 C 分支,修改 a.txt 文件,内容是 ac
$ git checkout A
Switched to branch 'A'
$ git checkout -b C
Switched to a new branch 'C'
$ cat a.txt
ac

# 回到 B 分支,合并 C 分支,此时产生冲突
$ git checkout B
Switched to branch 'B'
$ git merge C
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.

# 查看冲突的文件内容,不做修改,重新 commit
$ vim a.txt
<<<<<<< HEAD
ab
=======
ac
>>>>>>> C

$ git add .
$ git commit -m "abc"

# 此时可以看到合并已经完成
$ cat a.txt
<<<<<<< HEAD
ab
=======
ac
>>>>>>> C

$ git log
commit 6019def448ca10da6aa462c0b1ed7f1b1a768bbd (HEAD -> B)
Merge: b088741 fa4d442
Author: ChenBin113 <m13680309305_1@163.com>
Date:   Sun Dec 1 17:55:05 2019 +0800

    abc

commit fa4d442a66125c89af5cabf946a669af9ba256ba (C)
Author: ChenBin113 <m13680309305_1@163.com>
Date:   Sun Dec 1 17:53:06 2019 +0800

    ac

commit b088741babb987489d27a558978b63df3144ba0e
Author: ChenBin113 <m13680309305_1@163.com>
Date:   Sun Dec 1 17:50:34 2019 +0800

    ab

commit d34197eb7937fb2198676e8b9f2c80e061433f96 (A)
Author: ChenBin113 <m13680309305_1@163.com>
Date:   Sun Dec 1 17:40:25 2019 +0800

    A: a

# 删除 C 分支
$ git branch -d C
Deleted branch C (was fa4d442).

git tag

git tag -a v1.0 表示对当前的 commit 打上 tag 为 v1.0 的标记,通常用来记录重大的版本发布。

原文地址:https://www.cnblogs.com/chenxianbin/p/11967948.html