(2) Git branch management, remote branch management, label management, git alias

4. Branch management

1 View branch

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

2 Create a branch

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

3 Branch management

# 切换到分支
[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 Branch Merge and Conflict

# 分支合并
[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后面跟内容最新的分支。
  • If both the master branch and the yuntai branch have edited 2.txt, a conflict will be prompted when merging, and the conflict needs to be resolved before continuing to merge.
  • The way to resolve the conflict is to edit 2.txt under the master branch and change it to the content of 2.txt in the aming branch. Then submit 2.txt, and then merge the yuntai branch.
  • But there is a problem with this, what if the changes in the master branch are what we want? You can edit the content of 2.txt, change it to what you want, and submit it. Switch to the yuntai branch, and then merge the master branch to the yuntai branch (reverse merge). There is a principle of merging branches, which is to merge the latest branch into the old branch. That is to say, the branch name followed by merge must be the latest branch.

5 Delete branch

If we are sure we don't want a branch, we can delete the branch

[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 Principles of Using Branches

  • The master branch is very important. This branch is used for online code release. Usually, the development code should not be operated on this branch;

  • Create a dev branch, dedicated to development, and only merge dev into master before publishing it online

  • Developers should branch into a personal branch on the basis of the dev branch, develop code in their own branch (on their own pc), and then merge into the dev branch.

  • The command to merge the bob branch in the dev branch:

# git checkout dev
# git merge bob

5. Remote branch management

1 We now create 2 branches remotely

2 Clone the remote repository to the local

[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克隆下来

Check out the remote branch and clone it

[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

There are more local branches than remote branches

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
  • If the new branch created locally is not pushed to the remote, it will not be visible to others
  • View the remote branch git ls-remote origin, you can see all branches
  • There are two cases for the git push branch
  • When the local branch is consistent with the remote branch, git push will push all the changes of the local branch to the remote together. If you want to push only one branch, use git push origin branch-name
  • When there are more local branches than remote branches, by default, git push only pushes the local and remote branches. When you want to push the extra local branches to the remote, use git push origin branch-name. If the push fails, use git pull to grab it first. New commits from the remote.
  • When git clone, only the master branch is cloned by default. If you want to clone all the branches, you need to manually create them. Create the branch corresponding to the remote branch locally, use git checkout -b branch-name origin/branch-name, local The name of the remote branch should be the same

6. Tag Management

  • The tag is similar to the snapshot function, which can label the repository to record the state of the repository at a certain moment. You can also revert to this state at any time.
[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     //删除远程标签

In fact, tags and branches are very similar, just conceptually different.

7. git alias

git set alias

# 把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

Query log tips

[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>

unalias

[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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325123687&siteId=291194637