22.9 分支管理

22.9 分支管理

一、分支管理:

git branch //查看分支
git branch aming //建立分支
git checkout aming //切换到了aming分支下
再用git branch查看,会看到有两个分支master和aming,当前使用的分支前面会有一个*在aming分支下 ,编辑2.txt,并提交到新分支
echo "askdfjlksadjflk" > 2.txt
git add 2.txt
git commit -m "laksjdflksjdklfj"
切换回master分支
git checkout master //此时cat 2.txt发现并无更改内容git


##本地分支建立--注意 这个是本地分支:
[root@Dasoncheng gitroot]# git branch
* master
[root@Dasoncheng gitroot]# git branch aming
[root@Dasoncheng gitroot]# git branch
  aming
* master
[root@Dasoncheng gitroot]# git checkout aming 
Switched to branch 'aming'
[root@Dasoncheng gitroot]# git add .
[root@Dasoncheng gitroot]# git commit -m "add branch aming"
# On branch aming
nothing to commit, working directory clean
##在分支aming上面建立文件aming.txt,到master分支查看:
[root@Dasoncheng gitroot]# echo "aaaaaaaa" > aming.txt
[root@Dasoncheng gitroot]# git add aming.txt
[root@Dasoncheng gitroot]# git commit -m "add a file of aming.txt"
 1 file changed, 1 insertion(+)
 create mode 100644 aming.txt
[root@Dasoncheng gitroot]# ll
total 12
-rw-r--r-- 1 root root 21 Nov  7 11:04 1.txt
-rw-r--r-- 1 root root 15 Nov  7 16:36 2.txt
-rw-r--r-- 1 root root  9 Nov  8 15:26 aming.txt
[root@Dasoncheng gitroot]# git checkout master 
Switched to branch 'master'
[root@Dasoncheng gitroot]# ll
total 8
-rw-r--r-- 1 root root 21 Nov  7 11:04 1.txt
-rw-r--r-- 1 root root 15 Nov  7 16:36 2.txt
##能够看出,这两个分支的文件是分开保存的(分支对文件的操做不受影响);下一部分咱们来合并分支!!

提问1:如何在github上面建立分支? --3里面有详细讲解:
在本地新建一个分支: git branch Branch1
切换到你的新分支: git checkout Branch1
将新分支发布在github上: git push origin Branch1
在本地删除一个分支: git branch -d Branch1
提问2:如何建立本地分支呢?
[root@Dasoncheng learning]# git branch aming ##建立本地分支aming;
[root@Dasoncheng learning]# git branch
aming
*master
[root@Dasoncheng learning]# git checkout aming ##切换到分支aming;
Switched to branch 'aming'
[root@Dasoncheng learning]# git add .
[root@Dasoncheng learning]# git commit -m "add a new branch"
提交就能够了;
其余不懂的能够:git branch --help 更多命令相似;github

二、分支的合并:

git checkout master //合并分支以前,先切换到目标分支
git merge aming //把aming分支合并到了master
若是master分支和aming分支都对2.txt进行了编辑,当合并时会提示冲突,须要先解决冲突才能够继续合并。
解决冲突的方法是在master分支下,编辑2.txt,改成aming分支里面2.txt的内容。 而后提交2.txt,再合并aming分支。
可是这样有一个问题,万一master分支更改的内容是咱们想要的呢? 能够编辑2.txt内容,改成想要的,而后提交。切换到aming分支,而后合并master分支到aming分支便可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字必定是最新的分支。
git branch -d aming //删除分支
若是分支没有合并,删除以前会提示,那就不合并,强制删除
git branch -D amingvim

那么问题就来了,若是咱们想aming和master两个分支的文件同样怎么作(这个是一次性合并 不一样分支修改相同文件,仍是不会影响)?咱们能够合并分支!!
如今master和aming分支都有2.txt,但内容不同 咱们合并试试:缓存

[root@Dasoncheng gitroot]# git branch
* aming
  master
[root@Dasoncheng gitroot]# git checkout master
Switched to branch 'master'
[root@Dasoncheng gitroot]# git merge aming
Updating 8d12096..8ebe1fa
Fast-forward
 2.txt | 2 --
 1 file changed, 2 deletions(-)
[root@Dasoncheng gitroot]# ll
total 8
-rw-r--r-- 1 root root 21 Nov  7 11:04 1.txt
-rw-r--r-- 1 root root  4 Nov  8 16:15 2.txt
[root@Dasoncheng gitroot]# cat 2.txt 
aaa
##合并成功;没有报错,2.txt内容是aming里面的。
##合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字必定是最新的分支。
##接下来咱们在两个分支里面 都修改2.txt并提交:
[root@Dasoncheng gitroot]# git branch
  aming
* master
[root@Dasoncheng gitroot]# echo 'bbb' >> 2.txt
[root@Dasoncheng gitroot]# git add 2.txt
[root@Dasoncheng gitroot]# git commit -m "ch 2.txt"
[master c1153a0] ch 2.txt
 1 file changed, 1 insertion(+)
[root@Dasoncheng gitroot]# git checkout aming 
Switched to branch 'aming'
[root@Dasoncheng gitroot]# vim 2.txt 
[root@Dasoncheng gitroot]# echo 'ccc' >> 2.txt
[root@Dasoncheng gitroot]# git add 2.txt
[root@Dasoncheng gitroot]# git commit -m "ch 2.txt aming"
[aming 3a53834] ch 2.txt aming
 1 file changed, 1 insertion(+)
[root@Dasoncheng gitroot]# git checkout master 
Switched to branch 'master'
[root@Dasoncheng gitroot]# git merge aming  ##将aming合并到master;
Auto-merging 2.txt
CONFLICT (content): Merge conflict in 2.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@Dasoncheng gitroot]# cat 2.txt 
aaa
<<<<<<< HEAD
bbb
=======
ccc
>>>>>>> aming
##上面报错了!咱们打开冲突文件2.txt 里面记录了 修改的信息;两个文件不同 有冲突,那咱们如何合并 解决冲突呢?
##根据需求,查看那个分支下的2.txt是咱们须要的 将另外一个2.txt修改成咱们须要的内容,保持两个文件的内容一致(即保证两边没有冲突) 那么合并就没有问题了
[root@Dasoncheng gitroot]# git branch
  aming
* master
[root@Dasoncheng gitroot]# echo 'bbb' >> 2.txt  ##master分支修改2.txt
[root@Dasoncheng gitroot]# git add 2.txt
[root@Dasoncheng gitroot]# git commit -m "ch 2.txt"
[master c1153a0] ch 2.txt
 1 file changed, 1 insertion(+)
[root@Dasoncheng gitroot]# git checkout aming 
Switched to branch 'aming'
[root@Dasoncheng gitroot]# vim 2.txt 
[root@Dasoncheng gitroot]# echo 'ccc' >> 2.txt  ##aming分支修改2.txt
[root@Dasoncheng gitroot]# git add 2.txt
[root@Dasoncheng gitroot]# git commit -m "ch 2.txt aming"
1 file changed, 1 insertion(+)
[root@Dasoncheng gitroot]# git checkout master 
Switched to branch 'master'
[root@Dasoncheng gitroot]# git merge aming  ##切换到master合并aming分支:
Auto-merging 2.txt
CONFLICT (content): Merge conflict in 2.txt
Automatic merge failed; fix conflicts and then commit the result.  ##合并报错:指明2.txt有冲突;master的2.txt是我须要的 那么我到aming里面合并master便可!!
[root@Dasoncheng gitroot]# cat 2.txt 
aaa
<<<<<<< HEAD
bbb
=======
ccc
>>>>>>> aming
[root@Dasoncheng gitroot]# git checkout aming  ##但这里没法切换aming,咱们先解决当前的问题;
2.txt: needs merge
error: you need to resolve your current index first
[root@Dasoncheng gitroot]# git status  ##查看状态究竟是怎么回事?
# On branch master
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#	both modified:      2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@Dasoncheng gitroot]# git add 2.txt  ##按照提示 咱们一步一步来:
[root@Dasoncheng gitroot]# git commit -m "ch 2.txt"
[root@Dasoncheng gitroot]# cat 2.txt 
aaa
<<<<<<< HEAD
bbb
=======
ccc
>>>>>>> aming
[root@Dasoncheng gitroot]# vim 2.txt   ##将master的2.txt修改成咱们须要的;
[root@Dasoncheng gitroot]# git add 2.txt
[root@Dasoncheng gitroot]# git commit -m "ch 2.txt"
 1 file changed, 3 deletions(-)
[root@Dasoncheng gitroot]# git checkout aming   #切换到aming分支 进行反合并;
Switched to branch 'aming'
[root@Dasoncheng gitroot]# cat 2.txt 
aaa
ccc
[root@Dasoncheng gitroot]# git merge master
Updating 3a53834..4d0e891
Fast-forward
 2.txt | 1 +
 1 file changed, 1 insertion(+)
[root@Dasoncheng gitroot]# cat 2.txt 
aaa
bbb
ccc
##git branch -d 后面接分支名,删除分支 -D强制删除分支;更多git branch --help
[root@Dasoncheng gitroot]# git branch -d aming
Deleted branch aming (was 4d0e891).
[root@Dasoncheng gitroot]# git branch 
* master

三、使用分支的原则:

对于分支的应用,建议你们以这样的原则来:
master分支是很是重要的,线上发布代码用这个分支,平时咱们开发代码不要在这个分支上。
建立一个dev分支,专门用做开发,只有当发布到线上以前,才会把dev分支合并到master 开发人员应该在dev的基础上再分支成我的分支,我的分支(在本身pc上)里面开发代码,而后合并到dev分支
mark
dev分支合并bob分支的命令是:
git checkout dev //先切换到dev分支,而后
git merge bobbash

##github上面建立分支:
[root@Dasoncheng learning]# git branch 
* master
[root@Dasoncheng learning]# git branch aming
[root@Dasoncheng learning]# git branch 
  aming
* master
[root@Dasoncheng learning]# git checkout aming 
Switched to branch 'aming'
[root@Dasoncheng learning]# git push origin aming 
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:chengzhenge/learning.git
 * [new branch]      aming -> aming
[root@Dasoncheng learning]#   
##在aming分支上面建立文件aming.txt推送以后,切换到master查看是否有aming 文件!
[root@Dasoncheng learning]# ll
total 12
-rw-r--r-- 1 root root  7 Nov  7 19:27 1.txt
-rw-r--r-- 1 root root  6 Nov  7 19:28 2.txt
-rw-r--r-- 1 root root 11 Nov  7 19:27 README.md
[root@Dasoncheng learning]# echo 'aming' >aming.txt
[root@Dasoncheng learning]# git add aming.txt
[root@Dasoncheng learning]# git commit -m "add aming.txt"
[root@Dasoncheng learning]# git push
[root@Dasoncheng learning]# git checkout master 
Switched to branch 'master'
[root@Dasoncheng learning]# ll
total 12
-rw-r--r-- 1 root root  7 Nov  7 19:27 1.txt
-rw-r--r-- 1 root root  6 Nov  7 19:28 2.txt
-rw-r--r-- 1 root root 11 Nov  7 19:27 README.md

22.10 远程分支管理

本地新建的分支若是不推送到远程,对其余人就是不可见的
查看远程分支 git ls-remote origin,能够看到全部分支**(或者git branch -a 这个更快--这个好像是缓存)**
对于git push分支分两种状况
1.当本地分支和远程分支一致时: **
git push会把全部本地分支的变动一同推送到远程,若是想只推送一个分支,使用git push origin branch-name
** 2.当本地分支比远程分支多时:

默认git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name 若是推送失败,先用git pull抓取远程的新提交
git clone的时候默认只把master分支克隆下来,若是想把全部分支都克隆下来,须要手动建立,在本地建立和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称要一致.net


[root@Dasoncheng learning]# git ls-remote origin  ##查看远程全部分支;
b817f6a6359d8facd1842f784aa46126cc174227	HEAD
72afdb29b294a971c041a90c72edf8ca7edd0805	refs/heads/aming
b817f6a6359d8facd1842f784aa46126cc174227	refs/heads/dev
b817f6a6359d8facd1842f784aa46126cc174227	refs/heads/master
[root@Dasoncheng learning]# git branch -a  ##也能够查看远程全部分支;--这个好像是缓存
  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/aming
  remotes/origin/dev
  remotes/origin/master
[root@Dasoncheng learning]# git branch
  aming
* master
[root@Dasoncheng learning]# git branch -d aming  
error: The branch 'aming' is not fully merged.
If you are sure you want to delete it, run 'git branch -D aming'.
[root@Dasoncheng learning]# git branch -D aming
Deleted branch aming (was 72afdb2).
[root@Dasoncheng learning]# git branch --help
[root@Dasoncheng learning]# git checkout dev
error: pathspec 'dev' did not match any file(s) known to git.
[root@Dasoncheng learning]# git checkout -b dev
Switched to a new branch 'dev'
[root@Dasoncheng learning]# git branch 
* dev
  master

一、当本地分支与远程分支一致时(或者少于远程分支)

[root@Dasoncheng learning]# git branch 
* dev
  master
[root@Dasoncheng learning]# ll
total 12
-rw-r--r-- 1 root root  7 Nov  7 19:27 1.txt
-rw-r--r-- 1 root root  6 Nov  7 19:28 2.txt
-rw-r--r-- 1 root root 11 Nov  7 19:27 README.md
[root@Dasoncheng learning]# echo aaa > a.txt
[root@Dasoncheng learning]# git add a.txt
[root@Dasoncheng learning]# git commit -m "add a.txt"
 create mode 100644 a.txt
[root@Dasoncheng learning]# git checkout master 
Switched to branch 'master'
[root@Dasoncheng learning]# echo bbb >b.txt
[root@Dasoncheng learning]# git add b.txt
[root@Dasoncheng learning]# git commit -m "add b.txt"
 create mode 100644 b.txt
[root@Dasoncheng learning]# git push  
##推送的时候,会将本地全部分支的改动推送到远程;
##若是想只推送一个分支,使用git push origin branch-name
   b817f6a..787452d  dev -> dev
   b817f6a..310883c  master -> master

二、当本地分支比远程分支多时:

[root@Dasoncheng learning]# git branch dev3  ##建立一个远程没有的分支dev3
[root@Dasoncheng learning]# git branch 
  dev
  dev3
* master
[root@Dasoncheng learning]# git checkout dev3 
Switched to branch 'dev3'
[root@Dasoncheng learning]# echo ccc >c.txt
[root@Dasoncheng learning]# git add c.txt
[root@Dasoncheng learning]# git commit -m "add c.txt"
 create mode 100644 c.txt
[root@Dasoncheng learning]# git push  
Everything up-to-date  
##推送 ,提示全部的事物都是最新的 没有变更;咱们能够这样git push origin dev3
[root@Dasoncheng learning]# git push origin dev3
 * [new branch]      dev3 -> dev3  ##推送成功;
[root@Dasoncheng learning]#

提问:如何删除远程分支aming?
[root@Dasoncheng learning]# git branch -r -d origin/aming
Deleted remote branch origin/aming (was 72afdb2). ##此时,aming分支已经删除
[root@Dasoncheng learning]# git branch -a
dev
*master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
[root@Dasoncheng learning]# git push origin :<aming>
-bash: syntax error near unexpected token `newline'
##推送一个本地空的分支到远程 至关于删除该分支;可是报错了,我没弄懂 上面的命令就能够删除aming分支了
##远程看了一下,aming并无删除 只是本地缓存删了; 饿 个人天3d

22.11 标签管理

  • 标签相似于快照功能,能够给版本库打一个标签,记录某个时刻库的状态。也能够随时恢复到该状态(实际用到的较少 但会)。
  • git checkout master 先切到master分支上
  • git tag v1.0 给master打一个标签v1.0
  • git show v1.0 查看标签信息
  • git tag 能够查看全部的标签
  • tag是针对commit来打标签的,因此能够针对历史的commit来打tag
  • git log --pretty=oneline --abbrev-commit //先查看历史的commit
  • git tag v0.9 46d3c1a //针对历史commit打标签
  • git tag -a v0.8 -m "tag just v1.1 and so on" 5aacaf4 //能够对标签进行描述
  • git tag -d v0.8 //删除标签
  • git push origin v1.0 //推送指定标签到远程
  • git push --tag origin //推送全部标签
  • 若是本地删除了一个标签,远程也想要删除须要这样操做:
  • git tag v1.0 -d //删除本地标签
  • git push origin :refs/tags/v1.0 //删除远程标签
[root@Dasoncheng ~]# cd /home/learning/
[root@Dasoncheng learning]# git branch   ##通常都是针对master作tag
  aming2
  dev
  dev3
* master
[root@Dasoncheng learning]# git tag v1.0  ##对当前状态打标签v1.0
[root@Dasoncheng learning]# git tag  ##查看当前分支 标签信息;
v1.0
[root@Dasoncheng learning]# git show v1.0   ##查看标签详细信息;
commit efd8425d07fac286a39cd029211cc9e700e62594
Merge: b4d5d65 b91cd87
Author: chengzhenge <gecz0000@163.com>
Date:   Mon Nov 13 19:33:43 2017 +0800

    Merge branch 'aming2'
    merge aming2
[root@Dasoncheng learning]# git log --pretty=oneline   ##显示历史commit;
efd8425d07fac286a39cd029211cc9e700e62594 Merge branch 'aming2' merge aming2
b91cd87fa3e04abadeb95809ee1485397418bb1a add aaa
69f094efff7cdb26d8db674764a43d370c7073b6 rm 1
b4d5d65c6f48a7d74315afa0b9abc2fcbe4620f8 Merge branch 'aming2'
ffb285ea75f46da68f77119711672f17ccf6bd67 add bb
24dd4c31d23c4aea9fa7882a87f713787f3f31ef add aa
……
[root@Dasoncheng learning]# git log --pretty=oneline --abbrev-commit   ##简化显示历史commit
efd8425 Merge branch 'aming2' merge aming2
b91cd87 add aaa
69f094e rm 1
b4d5d65 Merge branch 'aming2'
ffb285e add bb
24dd4c3 add aa
……
[root@Dasoncheng learning]# git tag v0.9 ffb285e    ##针对历史commit打标签
[root@Dasoncheng learning]# git tag -a v0.8 -m "tag history commit" 24dd4c3  
##针对commit打标签并 备注:
[root@Dasoncheng learning]# git push origin v1.0  ##推送指定标签到远程;
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 536 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To git@github.com:chengzhenge/learning.git
 * [new tag]         v1.0 -> v1.0
[root@Dasoncheng learning]# git push --tags origin   ##推送全部标签到远程;
Counting objects: 1, done.
Writing objects: 100% (1/1), 160 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:chengzhenge/learning.git
 * [new tag]         v0.8 -> v0.8
[root@Dasoncheng learning]# git tag
v0.8
v1.0
[root@Dasoncheng learning]# git show v0.8  ##tag history commit就是备注信息;
tag v0.8
Tagger: chengzhenge <gecz0000@163.com>
Date:   Tue Nov 14 16:00:00 2017 +0800

tag history commit

commit 24dd4c31d23c4aea9fa7882a87f713787f3f31ef
Author: chengzhenge <gecz0000@163.com>
Date:   Mon Nov 13 19:29:18 2017 +0800

    add aa

diff --git a/aa.txt b/aa.txt
new file mode 100644
index 0000000..8baef1b
--- /dev/null
+++ b/aa.txt
@@ -0,0 +1 @@
+abc
[root@Dasoncheng learning]# git tag v0.8 -d  ##若是本地删除tag,远程须要删除须要作一下操做:
Deleted tag 'v0.8' (was 88b74f5)
[root@Dasoncheng learning]# git push origin :refs/tag/v0.8  ##其实就是本地删除,而后同步到远程,即删除;
remote: warning: Deleting a non-existent ref.
To git@github.com:chengzhenge/learning.git
 - [deleted]         refs/tag/v0.8

22.12 git别名

  • git commit 这个命令是否是有点长? 用别名能够提升咱们的工做效率
  • git config --global alias.ci commit
  • git config --global alias.co checkout
  • git config --global alias.br branch
  • 查看git别名使用命令
  • git config --list |grep alias
  • 查询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"
  • 取消别名
  • git config --global --unset alias.br
[root@Dasoncheng learning]# git config --global alias.ct commit   ##定义别名commit为ct;
[root@Dasoncheng learning]# ls
2.txt  a  aaa.txt  aa.txt  aming2.txt  bb.txt  b.txt  README.md
[root@Dasoncheng learning]# echo aaa > 1a.txt
[root@Dasoncheng learning]# git add 1a.txt
[root@Dasoncheng learning]# git ct -m "add 1a.txt"
[master 8e7b931] add 1a.txt
 1 file changed, 1 insertion(+)
 create mode 100644 1a.txt
[root@Dasoncheng learning]# git config --global alias.br branch  ##定义别名branch为br;
[root@Dasoncheng learning]# git br
  aming2
  dev
  dev3
* master
[root@Dasoncheng learning]# git config --list |grep alias  ##查看alias别名列表;
alias.ct=commit
alias.br=branch
[root@Dasoncheng learning]# git config --list  ##查看全部信息;
user.name=chengzhenge
user.email=gecz0000@163.com
alias.ct=commit
alias.br=branch
……
[root@Dasoncheng learning]# cat /root/.gitconfig   ##这里也能够编辑alias,不过建议命令编辑;
[user]
	name = chengzhenge
	email = gecz0000@163.com
[alias]
	ct = commit
	br = branch
[root@Dasoncheng learning]# git config --global --unset alias.br  ##取消别名;
[root@Dasoncheng learning]# git br
git: 'br' is not a git command. See 'git --help'.

Did you mean one of these?
	branch
	var
[root@Dasoncheng learning]# cat /root/.gitconfig   ##别名取消后,配置里面也没有了;
[user]
	name = chengzhenge
	email = gecz0000@163.com
[alias]
	ct = commit
##下面是git查询log小技巧:彩色 有序显示!
[root@Dasoncheng learning]# 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"
[root@Dasoncheng learning]# git lg
* 8e7b931 - (HEAD, master) add 1a.txt (3 minutes ago) <chengzhenge>
*   efd8425 - (tag: v1.0) Merge branch 'aming2' merge aming2 (21 hours ago) <chengzhenge>
|\  
| * b91cd87 - (origin/aming2, aming2) add aaa (21 hours ago) <chengzhenge>
| * 69f094e - rm 1 (21 hours ago) <chengzhenge>
* |   b4d5d65 - Merge branch 'aming2' (21 hours ago) <chengzhenge>
|\ \  
| |/  
| * 24dd4c3 - add aa (21 hours ago) <chengzhenge>
* | ffb285e - (origin/master, origin/HEAD) add bb (21 hours ago) <chengzhenge>
* |   2b52bfb - test b.txt (21 hours ago) <chengzhenge>
……
相关文章
相关标签/搜索