Git 之 (2) git分支管理、远程分支管理、标签管理、git别名

4. 分支管理

1 查看分支

[root@yt-01 LNMP]# cd /data/gitroot
[root@yt-01 gitroot]# git branch
* master
[root@yt-01 gitroot]# ls
1.txt

2 创建分支

[root@yt-01 gitroot]# git branch yuntai
[root@yt-01 gitroot]# git branch
* master
  yuntai

3 分支管理

# 切换到分支
[root@yt-01 gitroot]# git checkout yuntai
切换到分支 'yuntai'
[root@yt-01 gitroot]# git branch    //*号在yuntai分支前,表示目前的分支
  master
* yuntai
[root@yt-01 gitroot]# ls    //2个分支下文件相同
1.txt

# 新分支下创建新文件
[root@yt-01 gitroot]# vim 2.txt
[root@yt-01 gitroot]# git add 2.txt
[root@yt-01 gitroot]# git commit -m "jjjjjj"
[yuntai 98bf627] jjjjjj
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@yt-01 gitroot]# cat 2.txt
1234hghklal

# 切换回master分支
[root@yt-01 gitroot]# git checkout master
切换到分支 'master'
[root@yt-01 gitroot]# ls
1.txt
由此可见,在不同的分支可看到的文件的内容是不同的,在所有分支都能看到master分支的文件。

4 分支合并与冲突

# 分支合并
[root@yt-01 gitroot]# git merge yuntai   //把yuntai分支合并到了master分支
更新 5acf95f..98bf627
Fast-forward
 2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt

[root@yt-01 gitroot]# ls
1.txt 2.txt

# 分支合并的冲突问题
[root@yt-01 gitroot]# vim 2.txt
[root@yt-01 gitroot]# git add 2.txt
[root@yt-01 gitroot]# git commit -m "sdjjjjjj"
[master cf73e6e] sdjjjjjj
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@yt-01 gitroot]# git branch
* master
  yuntai

[root@yt-01 gitroot]# git checkout yuntai
切换到分支 'yuntai'

[root@yt-01 gitroot]# vim 2.txt
[root@yt-01 gitroot]# git add 2.txt
[root@yt-01 gitroot]# git commit -m "dddjjjj"
[yuntai ba7c380] dddjjjj
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@yt-01 gitroot]# git merge master
自动合并 2.txt
冲突(内容):合并冲突于 2.txt
自动合并失败,修正冲突然后提交修正的结果。

# 发生冲突!!!需要先修复冲突后才能合并
# 解决冲突

# 使用git diff或cat查看冲突在哪:

[root@yt-01 gitroot]# git diff
diff --cc 2.txt
index 4311c21,4c236d2..0000000
--- a/2.txt
+++ b/2.txt
@@@ -1,1 -1,1 +1,5 @@@
++<<<<<<< HEAD
 +1234hghklal1234hghklal1234hghklal1234hghklal1234hghklal1234hghklal1234hghklal
++=======
+ doaiodwdndian
++>>>>>>> master

# cat和vim命令中也会提醒冲突的地方
[root@yt-01 gitroot]# cat 2.txt
<<<<<<< HEAD
1234hghklal1234hghklal1234hghklal1234hghklal1234hghklal1234hghklal1234hghklal
=======
doaiodwdndian
>>>>>>> master

[root@yt-01 gitroot]# git checkout master   //分支冲突的话,必须先解决冲突才能切换分支,所以分支冲突很麻烦,一定要注意
2.txt: needs merge
error: 您需要先解决当前索引的冲突
[root@yt-01 gitroot]# git branch
  master
* yuntai
[root@yt-01 gitroot]# vim 2.txt

[root@yt-01 gitroot]# git merge master          //没有提交修改,所以报错
error: 'merge' is not possible because you have unmerged files.
提示:请先在工作区改正文件,然后酌情使用
提示:'git add/rm <file>' 标记解决方案,
提示:或使用 'git commit -a'。
fatal: Exiting because of an unresolved conflict.

修改后提交
[root@yt-01 gitroot]# git add 2.txt
[root@yt-01 gitroot]# git commit -m "修改冲突内容"
[yuntai 0ac22aa] 修改冲突内容

合并分支
[root@yt-01 gitroot]# git merge master
Already up-to-date.
[root@yt-01 gitroot]# ls
1.txt 2.txt
[root@yt-01 gitroot]# git checkout master
切换到分支 'master'

解决冲突的办法是更改目标分支下冲突文件和源分支中内容一致,然后在提交、合并。
原则:只能将新分支合并到旧的分支,即merge后面跟内容最新的分支。
  • 如果master分支和yuntai分支都对2.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。
  • 解决冲突的方法是在master分支下,编辑2.txt,改为aming分支里面2.txt的内容。 然后提交2.txt,再合并yuntai分支。
  • 但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 可以编辑2.txt内容,改为想要的,然后提交。切换到yuntai 分支,然后合并master分支到yuntai分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。

5 删除分支

如果我们确定不想要某个分支,我们可以删除分支

[root@yt-01 gitroot]# git branch -d yuntai   //如果分支没有合并,删除之前会提示,那就不合并,强制删除
error: 分支 'yuntai' 没有完全合并。
如果您确认要删除它,执行 'git branch -D yuntai'。

[root@yt-01 gitroot]# git branch -D yuntai   //强制删除
已删除分支 yuntai(曾为 0ac22aa)。
[root@yt-01 gitroot]# git branch
* master

6 分支使用原则

  • master分支是非常重要的,线上发布代码用这个分支,平时开发代码不要在该分支操作;

  • 创建dev分支,专门用作开发,只有到发布到线上之前再把dev合并到master上

  • 开发人员应该在dev分支的基础上再分支成个人分支,自己的分支(在自己的pc上)里面开发代码,然后合并到dev分支。

  • 在dev分支合并bob分支的命令:

# git checkout dev
# git merge bob

5. 远程分支管理

1 我们现在远程创建2个分支

2 在远程仓库克隆到本地

[root@yt-01~]# cd /data/
[root@yt-01 data]# git clone [email protected]:zhouqunic/gittest.git
正克隆到 'gittest'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
接收对象中: 100% (6/6), done.
[root@yt-01 data]# ls
gitroot gittest mysql svnroot wwwroot

[root@yt-01 data]# cd gittest/
[root@yt-01 gittest]# ls
123.txt README.md
[root@yt-01 gittest]#

[root@yt-01 gittest]# git branch
* master
# 克隆远程的时候只能把master克隆下来

查看远程分支,并克隆下来

[root@yt-01 gittest]# git ls-remote origin   //查看远程所有分支
6007c36bb6151394b59c936deb0f67456fd8c639	HEAD
6007c36bb6151394b59c936deb0f67456fd8c639	refs/heads/dev
6007c36bb6151394b59c936deb0f67456fd8c639	refs/heads/master

[root@yt-01 gittest]# git checkout -b dev origin/dev   //克隆远程分支
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'

[root@yt-01 gittest]# git branch   //查看分支
* dev
  master
[root@yt-01 gittest]# ls
123.txt README.md
[root@yt-01 gittest]# vim 4.txt
[root@yt-01 gittest]# vim 4.txt
[root@yt-01 gittest]# git add 4.txt
[root@yt-01 gittest]# git commit -m "add 4.txt"
[dev 4585f09] add 4.txt
 1 file changed, 2 insertions(+)
 create mode 100644 4.txt
[root@yt-01 gittest]# git push     //git push会把所有的分支变更推送到远程分支上
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 302 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:zhouqunic/gittest.git
   6007c36..4585f09 dev -> dev

[root@yt-01 gittest]# git push origin dev         //只推送选定的分支
Everything up-to-date

本地分支比远程分支还多

root@yt-01 gittest]# git branch dev2     //新建本地分支dev2
[root@yt-01 gittest]# git branch
* dev
  dev2
  master
[root@yt-01 gittest]# git checkout dev2   //切换到新分支
切换到分支 'dev2'
[root@yt-01 gittest]# ls
123.txt 4.txt README.md
[root@yt-01 gittest]# echo -e "5555" > 5.txt   //新增新文件,并推送到本地
[root@yt-01 gittest]# ls
123.txt 4.txt 5.txt README.md
[root@yt-01 gittest]# git add 5.txt
[root@yt-01 gittest]# git commit -m "add 5.txt"
[dev2 13cb704] add 5.txt
 1 file changed, 1 insertion(+)
 create mode 100644 5.txt

[root@yt-01 gittest]# git push    //推送到远程,结果出现报错,提示用git push --set-upstream origin dev2命令推送
fatal: 当前分支 dev2 没有对应的上游分支。
为推送当前分支并建立与远程上游的跟踪,使用
    git push --set-upstream origin dev2
[root@yt-01 gittest]# git push --set-upstream origin dev2   //从本地推送远程没有的分支
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 324 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:zhouqunic/gittest.git
 * [new branch] dev2 -> dev2
分支 dev2 设置为跟踪来自 origin 的远程分支 dev2。

[root@yt-01 gittest]# git ls-remote origin      //查看远程分支,新分支dev2已经推送成功
6007c36bb6151394b59c936deb0f67456fd8c639	HEAD
4585f09ea5741cd9030c781a5e0a75629683fcef	refs/heads/dev
13cb704ff422999a9d0ec83f67f6e03f61dfaa94	refs/heads/dev2
6007c36bb6151394b59c936deb0f67456fd8c639	refs/heads/master
  • 本地新建的分支如果不推送到远程,对其他人就是不可见的
  • 查看远程分支 git ls-remote origin,可以看到所有分支
  • 对于git push分支分两种情况
  • 当本地分支和远程分支一致时,git push会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用git push origin branch-name
  • 当本地分支比远程分支多,默认git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name 如果推送失败,先用git pull抓取远程的新提交.
  • git clone的时候默认只把master分支克隆下来,如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称要一致

6. 标签管理

  • 标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态。也可以随时恢复到该状态。
[root@yt-01 gittest]# git checkout master    //先切到master分支上
[root@yt-01 gittest]# git tag v1.0     //给master打一个标签v1.0
[root@yt-01 gittest]# git show v1.0     //查看标签信息
commit 6007c36bb6151394b59c936deb0f67456fd8c639
Author: yuntai <[email protected]>
Date: Tue Apr 3 16:01:02 2018 +0800
    新建123.txt文件
diff --git a/123.txt b/123.txt
new file mode 100644
index 0000000..f3bed95
--- /dev/null
+++ b/123.txt
@@ -0,0 +1,3 @@
+123 123 123
+123 123
+123

# tag是针对commit来打标签的,所以可以针对历史的commit来打tag
# git tag 可以查看所有的标签

[root@yt-01 gittest]# git log --pretty=oneline --abbrev-commit //先查看历史的commit
6007c36 新建123.txt文件
f7045bf 创建README.md

[root@yt-01 gittest]# git tag v0.8  f7045bf //针对历史commit打标签

[root@yt-01 gittest]# git tag -d v0.8 //删除标签
已删除 tag 'v0.8'(曾为 f7045bf)

[root@yt-01 gittest]# git tag -a v0.8 -m "tag just v1.1 and so on"  f7045bf    //可以对标签进行描述

[root@yt-01 gittest]# git push origin v1.0 //推送指定标签到远程
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:zhouqunic/gittest.git

[root@yt-01 gittest]# git push --tag origin   //推送所有标签
Counting objects: 1, done.
Writing objects: 100% (1/1), 166 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To [email protected]:zhouqunic/gittest.git
 * [new tag] v0.8 -> v0.8

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

[root@yt-01 gittest]# git tag v1.0 -d                            //删除本地标签
[root@yt-01 gittest]# git push origin :refs/tags/v1.0     //删除远程标签

实际上,tag和分支十分类似,只是概念上的不同。

7. git别名

git设置别名

# 把ci设置为commit命令的别名
[root@yt-01 gittest]# git config --global alias.ci commit

# 把co设置为checkout命令的别名
[root@yt-01 gittest]# git config --global alias.co checkout

# 设置号的别名会保存在文件/root/.gitconfig中,所以可以通过编辑该文件来设置别名
[root@yt-01 gittest]# cat /root/.gitconfig
[user]
 email = [email protected]
 name = yuntai
[push]
 default = simple
[alias]
 ci = commit
 co = checkout

# 查看git别名使用命令
[root@yt-01 gittest]# git config --list |grep alias
alias.ci=commit
alias.co=checkout

查询log小技巧

[root@yt-01 gittest]# 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"     //把git lg彩色排列的历史记录

[root@yt-01 gittest]# git lg
* 6007c36 - (HEAD, tag: v1.0, origin/master, origin/HEAD, master) 新建123.txt文件 (2 小时之前) <yuntai>
* f7045bf - (tag: v0.8) 创建README.md (3 小时之前) <yuntai>

取消别名

[root@yt-01 gittest]# git config --global --unset alias.co //取消co的别名
[root@yt-01 gittest]# vim vim /root/.gitconfig  //或者直接去配置文件里面删除alias的配置
[user]
 email = [email protected]
 name = yuntai
[push]
 default = simple
[alias]
 ci = commit
 lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

猜你喜欢

转载自my.oschina.net/zhouyuntai/blog/1791171