git分支、标签管理与别名

时间:2022-07-25
本文章向大家介绍git分支、标签管理与别名,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

笔记内容:git分支、标签管理与别名

22.9 分支管理

分支管理是git比较重要的一个概念,平时用的也比较多。我们先来在本地的仓库里操作一下分支:

[root@localhost ~]# cd /data/gitroot/
[root@localhost /data/gitroot]# git branch   # 查看当前仓库的分支,*表示当前的分支是哪一个
* master
[root@localhost /data/gitroot]# ls
Hello.java
[root@localhost /data/gitroot]#

创建分支:

[root@localhost /data/gitroot]# git branch example  # 创建分支
[root@localhost /data/gitroot]# git branch 
  example
* master
[root@localhost /data/gitroot]# ls
Hello.java
[root@localhost /data/gitroot]#

切换分支:

[root@localhost /data/gitroot]# git checkout example  # 切换分支
切换到分支 'example'
[root@localhost /data/gitroot]# git branch 
* example
  master
[root@localhost /data/gitroot]# ls
Hello.java
[root@localhost /data/gitroot]#

在example分支下创建一个新文件;

[root@localhost /data/gitroot]# echo "123456abcdefg" > test.txt
[root@localhost /data/gitroot]# git add test.txt
[root@localhost /data/gitroot]# git commit -m "add test.txt"
[example c83af5a] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost /data/gitroot]# ls
Hello.java  test.txt
[root@localhost /data/gitroot]#

切换到master分支后,可以发现该分支下没有我们刚刚创建的文件:

[root@localhost /data/gitroot]# git checkout master 
切换到分支 'master'
[root@localhost /data/gitroot]# ls
Hello.java
[root@localhost /data/gitroot]#

这说明分支跟分支之间是相互隔离开的,在当前分支下进行的操作不会影响到其他分支。

分支的合并:

如果想要别的分支也同步当前分支的操作,可以对分支进行合并,合并分支之前,需要先切换到目标分支:

[root@localhost /data/gitroot]# git checkout master 
切换到分支 'master'
[root@localhost /data/gitroot]# git branch 
  example
* master
[root@localhost /data/gitroot]# ls
Hello.java
[root@localhost /data/gitroot]# git merge example  # 合并example分支
更新 5341f93..c83af5a
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost /data/gitroot]# ls
Hello.java  test.txt
[root@localhost /data/gitroot]#

关于合并可能会发生冲突的问题:

如果master分支和example分支都对text.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。

解决冲突的方法是在master分支下,编辑test.txt,改为example分支里面test.txt的内容。 然后提交test.txt,再合并example分支。

但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 可以编辑test.txt内容,改为想要的,然后提交。切换到example分支,然后合并master分支到example分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。

冲突情况示例: 1.在master分支下对文件进行更改:

[root@localhost /data/gitroot]# git branch 
  example
* master
[root@localhost /data/gitroot]# ls
Hello.java  test.txt
[root@localhost /data/gitroot]# echo "ABCDEFG" >> test.txt  # 在master下对test.txt进行了更改
[root@localhost /data/gitroot]# git add test.txt
[root@localhost /data/gitroot]# git commit -m "ch test.txt"
[master f18dbd4] ch test.txt
 1 file changed, 1 insertion(+)
[root@localhost /data/gitroot]#

2.切换到example分支,也对相同的文件进行更改:

[root@localhost /data/gitroot]# git checkout example 
切换到分支 'example'
[root@localhost /data/gitroot]# git branch 
* example
  master
[root@localhost /data/gitroot]# ls
Hello.java  test.txt
[root@localhost /data/gitroot]# echo "HIJKLMN" >> test.txt  # 在example下对test.txt进行了更改
[root@localhost /data/gitroot]# git add test.txt
[root@localhost /data/gitroot]# git commit -m "ch test.txt"
[example 462c72c] ch test.txt
 1 file changed, 1 insertion(+)
[root@localhost /data/gitroot]#

3.这时把example与master进行合并就会出问题了:

[root@localhost /data/gitroot]# git checkout master 
切换到分支 'master'
[root@localhost /data/gitroot]# git merge example 
自动合并 test.txt
冲突(内容):合并冲突于 test.txt
自动合并失败,修正冲突然后提交修正的结果。
[root@localhost /data/gitroot]#

4.需要把master分支下的文件内容改成与example下的文件内容一致后才能解决冲突的问题。但是如果master分支下的文件内容是你想要的,那么就把example下的文件内容修改成master分支下的文件内容,然后合并master到example下即可:

[root@localhost /data/gitroot]# git branch 
  example
* master
[root@localhost /data/gitroot]# vim test.txt  # 把冲突时生成的标记信息给去掉
123456abcdefg
ABCDEFG
HIJKLMN
[root@localhost /data/gitroot]# git add test.txt
[root@localhost /data/gitroot]# git commit -m "ch test.txt"
[root@localhost /data/gitroot]# cat test.txt 
123456abcdefg
ABCDEFG
HIJKLMN
[root@localhost /data/gitroot]# git checkout example
切换到分支 'example'
[root@localhost /data/gitroot]# cat test.txt 
123456abcdefg
HIJKLMN
[root@localhost /data/gitroot]# git merge master 
更新 462c72c..7bc085d
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
[root@localhost /data/gitroot]# cat test.txt 
123456abcdefg
ABCDEFG
HIJKLMN
[root@localhost /data/gitroot]#

解决分支冲突问题挺绕的,所以平时使用分支合并时要注意避免发生合并冲突的问题。

删除分支的命令:

[root@localhost /data/gitroot]# git checkout master  # 由于不能删除当前所在分支,所以需要切换一下
切换到分支 'master'
[root@localhost /data/gitroot]# git  branch -d example 
已删除分支 example(曾为 7bc085d)。
[root@localhost /data/gitroot]# 

如果分支没有合并,删除之前会提示,那就不合并,可以使用以下命令进行强制删除:

git branch -D example

使用分支的原则: 对于分支的应用,建议大家以这样的原则来:

  • master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
  • 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master
  • 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到dev分支

dev分支合并bob分支的命令是:

git checkout dev //先切换到dev分支,然后 git merge bob


22.10 远程分支管理

在GitHub上创建一个dev分支:

创建完成:

克隆远程的仓库:

[root@localhost ~]# cd /tmp/
[root@localhost /tmp]# git clone https://github.com/Binary-ZeroOne/example.git
正克隆到 'example'...
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 12 (delta 0), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (12/12), done.
[root@localhost /tmp]# cd example
[root@localhost /tmp/example]# git branch 
* master
[root@localhost /tmp/example]#

如上可以看到,当我们使用git clone命令克隆远程仓库的时候默认只会把master分支克隆下来,而不会克隆其他的分支。

查看远程仓库所有分支的命令:

[root@localhost /tmp/example]# git ls-remote origin
b71be6bf1ab975692356683a327079c4f04180f5    HEAD
b71be6bf1ab975692356683a327079c4f04180f5    refs/heads/dev
b71be6bf1ab975692356683a327079c4f04180f5    refs/heads/master
[root@localhost /tmp/example]#

现在我们需要把dev分支给克隆下来,需要自己先手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称要一致:

# -b指定本地分支名称,origin后面跟的是远程分支的名称
[root@localhost /tmp/example]# git checkout -b dev origin/dev 
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'
[root@localhost /tmp/example]# git branch 
* dev
  master
[root@localhost /tmp/example]# ls
example.txt  README.md
[root@localhost /tmp/example]# echo "123456abc" > test.txt  # 添加一个新文件
[root@localhost /tmp/example]# ls
example.txt  README.md  test.txt
[root@localhost /tmp/example]# git add test.txt
[root@localhost /tmp/example]# git commit -m "add test.txt"
[dev 3dded3a] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost /tmp/example]# git push  # 推送到远程仓库上
Username for 'https://github.com': Binary-ZeroOne
Password for 'https://Binary-ZeroOne@github.com': 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 308 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Binary-ZeroOne/example.git
   b71be6b..3dded3a  dev -> dev  # 可以看到推送到了远程仓库的dev分支上了
[root@localhost /tmp/example]#

关于git push分支的两种情况:

1.当本地分支和远程分支一致时,git push默认会把所有本地分支的变更一同推送到远程(matching模式下),如果想只推送某一个分支,可以使用git push origin branch-name命令:

[root@localhost /tmp/example]# git push origin dev
Username for 'https://github.com': Binary-ZeroOne
Password for 'https://Binary-ZeroOne@github.com': 
Everything up-to-date
[root@localhost /tmp/example]#

2.当本地分支比远程分支多,默认git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name 命令, 如果推送失败,先用git pull抓取远程的新提交:

[root@localhost /tmp/example]# git branch dev2  # 创建一个新的本地分支
[root@localhost /tmp/example]# git branch 
* dev
  dev2
  master
[root@localhost /tmp/example]# git checkout dev2
切换到分支 'dev2'
[root@localhost /tmp/example]# ls
example.txt  README.md  test.txt
[root@localhost /tmp/example]# echo "123" > newFile.txt
[root@localhost /tmp/example]# git add newFile.txt
[root@localhost /tmp/example]# git commit -m "add newFile.txt"
[dev2 b1dc04e] add newFile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 newFile.txt
[root@localhost /tmp/example]# git push origin dev2  # 这时候就需要指定分支名称推送到远程上
Username for 'https://github.com': Binary-ZeroOne
Password for 'https://Binary-ZeroOne@github.com': 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 335 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Binary-ZeroOne/example.git
 * [new branch]      dev2 -> dev2
[root@localhost /tmp/example]#

现在查看远程仓库的分支,可以看到多了一个dev2分支:


22.11 标签管理

标签类似于虚拟机的快照功能,可以给版本库打一个标签,记录某个时刻库的状态,也可以随时恢复到该标签标记的状态。

通常情况下我们都是对master分支打标签(其他分支也可以),所以先切换到master分支上:

[root@localhost /tmp/example]# git checkout master
切换到分支 'master'
[root@localhost /tmp/example]#

给master打一个标签v1.0

[root@localhost /tmp/example]# git tag v1.0  # 给master打一个标签v1.0
[root@localhost /tmp/example]# git show v1.0  # 查看标签的信息
commit b71be6bf1ab975692356683a327079c4f04180f5
Author: Binary-ZeroOne <31101209+Binary-ZeroOne@users.noreply.github.com>
Date:   Fri Jan 12 16:04:01 2018 +0800

    Update example.txt

diff --git a/example.txt b/example.txt
index fadffaa..62ebbc0 100644
--- a/example.txt
+++ b/example.txt
@@ -1,2 +1,3 @@
 This is a example
 This is a change operation
+This is another change operation
[root@localhost /tmp/example]# git tag # 可以查看当前分支下的所有标签
v1.0
[root@localhost /tmp/example]#

tag是针对commit来打标签的,所以可以针对历史的commit来打tag:

[root@localhost /tmp/example]# git log --pretty=oneline --abbrev-commit  # 先查看历史的commit
b71be6b Update example.txt
09b7380 change example.txt
aacb77a example commit
4b710bc first commit
[root@localhost /tmp/example]#
[root@localhost /tmp/example]# git tag v0.8 aacb77a  # 针对历史commit打标签
[root@localhost /tmp/example]# git tag
v0.8
v1.0
[root@localhost /tmp/example]#

可以对标签进行描述:

[root@localhost /tmp/example]# git log --pretty=oneline --abbrev-commit
b71be6b Update example.txt
09b7380 change example.txt
aacb77a example commit
4b710bc first commit
[root@localhost /tmp/example]# git tag -a v0.1 -m "first tag" 4b710bc  # -m指定描述信息
[root@localhost /tmp/example]# git tag
v0.1
v0.8
v1.0
[root@localhost /tmp/example]# git show v0.1
tag v0.1
Tagger: zero <none@none.com>
Date:   Tue Jan 16 02:24:40 2018 +0800

first tag

commit 4b710bcd4e0817f610a42595dea1c469607e37d4
Author: zero <none@none.com>
Date:   Fri Jan 12 23:17:40 2018 +0800

    first commit

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..02dc8ac
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# example
[root@localhost /tmp/example]#

删除标签:

[root@localhost /tmp/example]# git tag -d v0.1
已删除 tag 'v0.1'(曾为 2be29d6)
[root@localhost /tmp/example]# git tag
v0.8
v1.0
[root@localhost /tmp/example]#

推送某个指定的标签到远程仓库上:

将v1.0标签推送到远程仓库上:

[root@localhost /tmp/example]# git push origin v1.0
Username for 'https://github.com': Binary-ZeroOne
Password for 'https://Binary-ZeroOne@github.com': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/Binary-ZeroOne/example.git
 * [new tag]         v1.0 -> v1.0
[root@localhost /tmp/example]#

这时候再看远程仓库可以发现多了个v1.0标签:

推送所有标签到远程仓库上:

[root@localhost /tmp/example]# git push --tag origin
Username for 'https://github.com': Binary-ZeroOne
Password for 'https://Binary-ZeroOne@github.com': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/Binary-ZeroOne/example.git
 * [new tag]         v0.8 -> v0.8
[root@localhost /tmp/example]#

如果本地删除了一个标签,远程也想要删除需要这样操作:

[root@localhost /tmp/example]# git tag v1.0 -d  # 先删除本地标签
已删除 tag 'v1.0'(曾为 b71be6b)
[root@localhost /tmp/example]# git push origin :refs/tags/v1.0  # 然后再删除远程标签
Username for 'https://github.com': Binary-ZeroOne
Password for 'https://Binary-ZeroOne@github.com': 
To https://github.com/Binary-ZeroOne/example.git
 - [deleted]         v1.0
[root@localhost /tmp/example]#

22.12 git别名

git commit 这个命令是不是有点长? 用别名可以缩短命令的长度提高我们的工作效率:

[root@localhost /tmp/example]# git config --global alias.ci commit  # 把commit的别名设置为ci
[root@localhost /tmp/example]# echo "This is test file" > test.txt
[root@localhost /tmp/example]# git add test.txt
[root@localhost /tmp/example]# git ci -m "add test.txt"  # 这时候就可以使用别名了
[master 2566ad6] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost /tmp/example]#

其他的命令也可以设置别名:

[root@localhost /tmp/example]# git config --global alias.co  checkout
[root@localhost /tmp/example]# git config --global alias.br  branch
[root@localhost /tmp/example]# git br
  dev
  dev2
* master
[root@localhost /tmp/example]# git co dev
切换到分支 'dev'
[root@localhost /tmp/example]#

查看git命令的别名:

[root@localhost /tmp/example]# git config --list |grep alias
alias.ci=commit
alias.co=checkout
alias.br=branch
[root@localhost /tmp/example]#

使用如下命令配置log命令的别名,可以让log查询的结果更易读:

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

输入以上命令配置log命令的别名后,现在使用这个命令查看日志效果如下:

取消别名:

git config --global --unset alias.别名 例如:git config --global --unset alias.br