git分支、标签管理与别名

笔记内容:git分支、标签管理与别名
笔记日期:2018-01-15

  • 22.9 分支管理
  • 22.10 远程分支管理
  • 22.11 标签管理
  • 22.12 git别名

22.9 分支管理

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

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

创建分支:

[[email protected] /data/gitroot]# git branch example  # 创建分支
[[email protected] /data/gitroot]# git branch
  example
* master
[[email protected] /data/gitroot]# ls
Hello.java
[[email protected] /data/gitroot]#

切换分支:

[[email protected] /data/gitroot]# git checkout example  # 切换分支
切换到分支 ‘example‘
[[email protected] /data/gitroot]# git branch
* example
  master
[[email protected] /data/gitroot]# ls
Hello.java
[[email protected] /data/gitroot]#

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

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

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

[[email protected] /data/gitroot]# git checkout master
切换到分支 ‘master‘
[[email protected] /data/gitroot]# ls
Hello.java
[[email protected] /data/gitroot]#

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

分支的合并:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

删除分支的命令:

[[email protected] /data/gitroot]# git checkout master  # 由于不能删除当前所在分支,所以需要切换一下
切换到分支 ‘master‘
[[email protected] /data/gitroot]# git  branch -d example
已删除分支 example(曾为 7bc085d)。
[[email protected] /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分支:

创建完成:

克隆远程的仓库:

[[email protected] ~]# cd /tmp/
[[email protected] /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.
[[email protected] /tmp]# cd example
[[email protected] /tmp/example]# git branch
* master
[[email protected] /tmp/example]#

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

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

[[email protected] /tmp/example]# git ls-remote origin
b71be6bf1ab975692356683a327079c4f04180f5    HEAD
b71be6bf1ab975692356683a327079c4f04180f5    refs/heads/dev
b71be6bf1ab975692356683a327079c4f04180f5    refs/heads/master
[[email protected] /tmp/example]#

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

# -b指定本地分支名称,origin后面跟的是远程分支的名称
[[email protected] /tmp/example]# git checkout -b dev origin/dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 ‘dev‘
[[email protected] /tmp/example]# git branch
* dev
  master
[[email protected] /tmp/example]# ls
example.txt  README.md
[[email protected] /tmp/example]# echo "123456abc" > test.txt  # 添加一个新文件
[[email protected] /tmp/example]# ls
example.txt  README.md  test.txt
[[email protected] /tmp/example]# git add test.txt
[[email protected] /tmp/example]# git commit -m "add test.txt"
[dev 3dded3a] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[[email protected] /tmp/example]# git push  # 推送到远程仓库上
Username for ‘https://github.com‘: Binary-ZeroOne
Password for ‘https://[email protected]‘:
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分支上了
[[email protected] /tmp/example]#

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

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

[[email protected] /tmp/example]# git push origin dev
Username for ‘https://github.com‘: Binary-ZeroOne
Password for ‘https://[email protected]‘:
Everything up-to-date
[[email protected] /tmp/example]#

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

[[email protected] /tmp/example]# git branch dev2  # 创建一个新的本地分支
[[email protected] /tmp/example]# git branch
* dev
  dev2
  master
[[email protected] /tmp/example]# git checkout dev2
切换到分支 ‘dev2‘
[[email protected] /tmp/example]# ls
example.txt  README.md  test.txt
[[email protected] /tmp/example]# echo "123" > newFile.txt
[[email protected] /tmp/example]# git add newFile.txt
[[email protected] /tmp/example]# git commit -m "add newFile.txt"
[dev2 b1dc04e] add newFile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 newFile.txt
[[email protected] /tmp/example]# git push origin dev2  # 这时候就需要指定分支名称推送到远程上
Username for ‘https://github.com‘: Binary-ZeroOne
Password for ‘https://[email protected]‘:
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
[[email protected] /tmp/example]#

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


22.11 标签管理

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

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

[[email protected] /tmp/example]# git checkout master
切换到分支 ‘master‘
[[email protected] /tmp/example]#

给master打一个标签v1.0

[[email protected] /tmp/example]# git tag v1.0  # 给master打一个标签v1.0
[[email protected] /tmp/example]# git show v1.0  # 查看标签的信息
commit b71be6bf1ab975692356683a327079c4f04180f5
Author: Binary-ZeroOne <[email protected]>
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
[[email protected] /tmp/example]# git tag # 可以查看当前分支下的所有标签
v1.0
[[email protected] /tmp/example]#

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

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

可以对标签进行描述:

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

first tag

commit 4b710bcd4e0817f610a42595dea1c469607e37d4
Author: zero <[email protected]>
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
[[email protected] /tmp/example]#

删除标签:

[[email protected] /tmp/example]# git tag -d v0.1
已删除 tag ‘v0.1‘(曾为 2be29d6)
[[email protected] /tmp/example]# git tag
v0.8
v1.0
[[email protected] /tmp/example]#

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

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

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

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

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

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

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

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


22.12 git别名

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

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

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

[[email protected] /tmp/example]# git config --global alias.co  checkout
[[email protected] /tmp/example]# git config --global alias.br  branch
[[email protected] /tmp/example]# git br
  dev
  dev2
* master
[[email protected] /tmp/example]# git co dev
切换到分支 ‘dev‘
[[email protected] /tmp/example]#

查看git命令的别名:

[[email protected] /tmp/example]# git config --list |grep alias
alias.ci=commit
alias.co=checkout
alias.br=branch
[[email protected] /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

原文地址:http://blog.51cto.com/zero01/2061244

时间: 2024-08-04 15:10:46

git分支、标签管理与别名的相关文章

Git学习 -- 标签管理

新建标签 git tag <tagname>   默认为HEAD,也可以指定一个commit id eg.   git tag v0.9  git tag v1.0 31aa59c git tag <tagname> -m "xxx"   可以指定标签信息 git tag -s <tagname>             可以用PGP签名标签 查看标签 git tag     查看所有标签 git show <tagname>    参看

【代码管理】GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流

找到一篇很详细的Git教程,真的很不错,推荐!!! GitHub操作总结 : 总结看不明白就看下面的详细讲解. . 作者 :万境绝尘  . GitHub操作流程 : 第一次提交 : 方案一 : 本地创建项目根目录, 然后与远程GitHub关联, 之后的操作一样; -- 初始化git仓库 :git init ; -- 提交改变到缓存 :git commit -m 'description' ; -- 本地git仓库关联GitHub仓库 : git remote add origin [email 

GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流(转载)

最近听同事说他都在使用GitHub,GitHub是程序员的社区,在里面可以学到很多书上学不到的东西,所以最近在准备入手这方面的知识去尝试学习,正好碰到这么详细完整的文章,就转载了,希望对自己和大家有帮助. GitHub操作总结 : 总结看不明白就看下面的详细讲解. GitHub操作流程 : 第一次提交 : 方案一 : 本地创建项目根目录, 然后与远程GitHub关联, 之后的操作一样; -- 初始化Git仓库 :git init ; -- 提交改变到缓存 :git commit -m 'desc

【Git】标签管理

来源:廖雪峰 为什么要标签: 发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照. Git的标签虽然是版本库的快照,但其实它就是指向某个commit的指针(跟分支很像,但是分支可以移动,标签不能移动),所以,创建和删除标签都是瞬间完成的. “请把上周一的那个版本打包发布,commit号是6a5819e...” “一串乱七八糟的数字不好找!” 如果换

版本控制git之五-标签管理

打标签 像其他版本控制系统(VCS)一样,Git 可以给历史中的某一个提交打上标签,以示重要. 比较有代表性的是人们会使用这个功能来标记发布结点(v1.0 等等). 在本节中,你将会学习如何列出已有的标签.如何创建新标签.以及不同类型的标签分别是什么. 列出标签 在 Git 中列出已有的标签是非常简单直观的. 只需要输入 git tag: $ git tag v0.1 v1.3 这个命令以字母顺序列出标签:但是它们出现的顺序并不重要. 你也可以使用特定的模式查找标签. 例如,Git 自身的源代码

Git之标签管理

创建标签,默认标签是打在最新提交的commit上 可以用-a指定标签名,-m指定说明文字,通过-s用私钥签名一个标签(需安装GnuPG) $ git tag <tag> 查看所有标签,标签不是按时间顺序列出,而是按字母排序的. $ git tag 对以往的commit打标签 $ git tag <tag> <commit id> 查看标签信息 $ git show <tag> 删除本地标签 $ git tag -d <tag> 推送某个标签到远程

&lt;Git使用&gt;标签管理

命令git tag <tagname>用于新建一个标签,默认为HEAD,也可以指定一个commit id: 命令git tag -a <tagname> -m "blablabla..."可以指定标签信息: 命令git tag可以查看所有标签. 命令git push origin <tagname>可以推送一个本地标签: 命令git push origin --tags可以推送全部未推送过的本地标签: 命令git tag -d <tagname

Git分支管理及常见操作

众所周知,使用Git分支,我们可以从开发主线上分离开来,然后在不影响主线的同时继续工作. 既然要使用Git分支,这里就涉及到Git分支的管理及常见操作,如列出分支,分支的创建,分支的删除,分支的合并等操作. 以前看到这就头痛,总是搞不明白,今天研究了许久才搞懂,这里做个笔记. 如,我的Git工具安装在E盘>myGit中,安装后目录如下: 我本地创建的仓库为myProject,首先我们可以先查看下本地仓库的分支情况.打开Git工具,操作如下. [email protected] MINGW64 ~

git分支管理与冲突解决(转载)

Git 分支管理和冲突解决 原文:http://www.cnblogs.com/mengdd/p/3585038.html 创建分支 git branch 没有参数,显示本地版本库中所有的本地分支名称. 当前检出分支的前面会有星号. git branch newname 在当前检出分支上新建分支,名叫newname. git checkout newname 检出分支,即切换到名叫newname的分支. git checkout –b newname master 这个命令将上面两个命令合并:在