本文共 10621 字,大约阅读时间需要 35 分钟。
git branch 查看分支
git branch newch 创建新的分支,创建后查看新的分支[root@nfs1 monice]# git branch newch[root@nfs1 monice]# git branch* master newch
git checkout newch 切换分支到newch下,切换分支后再使用git branch查看到分支已经切换到了newch下
[root@nfs1 monice]# git checkout newchSwitched to branch 'newch'[root@nfs1 monice]# git branch master* newch
在新的分支下创建新的文件并提交到git,再切换到master分支,查看相同的文件并没有发生任何更改
首先在master分支下查看2.txt的内容,分支master中的内容是123456[root@nfs1 monice]# cat 2.txt 123456
然后切换到创建的新版本当中,创建新的内容到文件中,提交更改到git中。并再次切换到master分支中
[root@nfs1 monice]# git checkout newchSwitched to branch 'newch'[root@nfs1 monice]# echo "123456 " > 2.txt[root@nfs1 monice]# git add 2.txt [root@nfs1 monice]# git commit -m "12345"[master 6dbc773] 12345 1 file changed, 1 insertion(+) create mode 100644 2.txt
查看newch分支中的2.txt内容和master中的2.txt内容,可以了解到git分支之间是分隔开来的,各自分支改动不会影响到其他分支
[root@nfs1 monice]# git branch* master newch[root@nfs1 monice]# cat 2.txt 123456-----------------------分支master中的2.txt内容[root@nfs1 monice]# git checkout newchSwitched to branch 'newch'[root@nfs1 monice]# git branch master* newch-----------------------分支newch中的2.txt内容[root@nfs1 monice]# cat 2.txt 123456 123456
分支的合并
合并之前首先切换到要合并的分支下,把被合并分支进行合并变更切换到master分支,并把newch分支合并到master分支下,并查看2.txt合并后的内容[root@nfs1 monice]# git checkout masterSwitched to branch 'master'[root@nfs1 monice]# git merge newchAuto-merging 2.txtCONFLICT (add/add): Merge conflict in 2.txtAutomatic merge failed; fix conflicts and then commit the result.[root@nfs1 monice]# cat 2.txt <<<<<<< HEAD123456=======123456 123456 >>>>>>> newch
如果master分支和newch的分支都对2.txt进行了编辑,当合并的时候会提示冲突,需要解决冲突后才能合并分支
解决分支冲突的 方法是在master分支下编辑2.txt,改为对方分支newch中的2.txt相同的内容,然后提交,再合并分支如果更新master分支遇到这个分支是想要的结果,另一个newch分支是不想要的内容,但由于两个分支内容的不同,导致切换不到newch分支当中,这种情况下我们可以在当前master分支修改并提交git,再切换到newch分支下,把master分支合并到newch分支下即可
合并分支有一个原则,那就是把最新的分支合并到旧的分支,也就是说merge后面分支名字一定是最新的分支git branch -d 分支名称 //删除分支
[root@nfs1 monice]# git branch* master newch[root@nfs1 monice]# git branch -d newchDeleted branch newch (was 3bc8b02).[root@nfs1 monice]# git branch* master
强制删除没有合并的分支,创建新分支list,并在分支内创建新的文件及内容。然后使用branch -D 来强制删除
[root@nfs1 monice]# git branch list[root@nfs1 monice]# git checkout listSwitched to branch 'list'[root@nfs1 monice]# echo "1234565778" > 23.txt[root@nfs1 monice]# git checkout masterSwitched to branch 'master'[root@nfs1 monice]# git branch -D listDeleted branch list (was 4f122c3).[root@nfs1 monice]# git branch* master --------------------强制删除后只剩下一个分支
dev分支合并bob分支的命令
git checkout dev 先切换到dev分支,然后合并bobgit merge bob拉取远程分支
本地分支如果不推送到远程,那么自己本地分支是对其他人不可见的查看远程上的分支,可以看到所有分支[root@nfs1 monice]# git ls-remote origin4f122c38f59a2844cad0ecda1397e3d89e15cca9 HEAD4f122c38f59a2844cad0ecda1397e3d89e15cca9 refs/heads/dev4f122c38f59a2844cad0ecda1397e3d89e15cca9 refs/heads/master
拉取远程dev分支并增加一个文件,再推送dev分支的更新
切换到远程的dev分支下,并查看当前所在分支[root@nfs1 monice]# git checkout -b dev origin/devBranch dev set up to track remote branch dev from origin.Switched to a new branch 'dev'[root@nfs1 monice]# git branch* dev master
更新文件到远程新分支下
创建一个文件,并更新至远程dev分支[root@nfs1 monice]# echo "vu==dgfdhg" > linux.txt[root@nfs1 monice]# git add linux.txt [root@nfs1 monice]# git commit -m "ceshi file linux.txt"[dev 0ee9472] ceshi file linux.txt 1 file changed, 1 insertion(+) create mode 100644 linux.txt[root@nfs1 monice]# git pushCounting objects: 7, done.Compressing objects: 100% (4/4), done.Writing objects: 100% (5/5), 514 bytes | 0 bytes/s, done.Total 5 (delta 2), reused 0 (delta 0)remote: Resolving deltas: 100% (2/2), completed with 1 local object.To git@github.com:hanxiang233/monice.git 3314ca3..8d7b993 dev -> dev
当更新遇到错误提示,这是因为远程的github文件变动没有同步到当前本地,需要先git pull拉下所有更新。提示信息如下:
[root@nfs1 monice]# git pushTo git@github.com:hanxiang233/monice.git ! [rejected] dev -> dev (fetch first)error: failed to push some refs to 'git@github.com:hanxiang233/monice.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first merge the remote changes (e.g.,hint: 'git pull') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.
使用git pull更新后就可以继续git push了
----------------------------------git pull下拉更新[root@nfs1 monice]# git pullremote: Enumerating objects: 4, done.remote: Counting objects: 100% (4/4), done.remote: Compressing objects: 100% (2/2), done.Unpacking objects: 100% (3/3), done.remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0From github.com:hanxiang233/monice 4f122c3..3314ca3 dev -> origin/devMerge made by the 'recursive' strategy. monice.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 monice.txt ----------------------------------git push[root@nfs1 monice]# git pushCounting objects: 7, done.Compressing objects: 100% (4/4), done.Writing objects: 100% (5/5), 514 bytes | 0 bytes/s, done.Total 5 (delta 2), reused 0 (delta 0)remote: Resolving deltas: 100% (2/2), completed with 1 local object.To git@github.com:hanxiang233/monice.git 3314ca3..8d7b993 dev -> dev
git push的两种情况
只更新一个分支当本地分支和远程分支一致时git push会把本地分支的变更一同推送到远程,如果想只推送一个分支更新,使用git push origin branch-name当两个本地和远程的两个分支都有变更。这时候使用git pull推送时会更新所有分支,如果想要推送指定分支,则需要指定明确需要推送的分支在dev分支下创建一个文件,然后只推送dev这一个分支更新:[root@nfs1 monice]# touch 123345.txt[root@nfs1 monice]# git add 123345.txt [root@nfs1 monice]# git commit -m "filess"[dev 32af6e6] filess 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 123345.txt[root@nfs1 monice]# git push origin devCounting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.Total 3 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), completed with 1 local object.To git@github.com:hanxiang233/monice.git 8d7b993..32af6e6 dev -> dev
将新本地分支更新到远程上
首先创建一个新分支,创建一个新的文件,然后将新建分支更新至git上。并在github上查看分支更新结果[root@nfs1 monice]# git checkout dev2Switched to branch 'dev2'------------------------创建分支文件[root@nfs1 monice]# echo "12345" > index.txt[root@nfs1 monice]# git add index.txt [root@nfs1 monice]# git commit -m "index"[dev2 1091eb5] index 1 file changed, 1 insertion(+) create mode 100644 index.txt -----------------------push新分支到git[root@nfs1 monice]# git push origin dev2Counting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 266 bytes | 0 bytes/s, done.Total 3 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), completed with 1 local object.remote: remote: Create a pull request for 'dev2' on GitHub by visiting:remote: https://github.com/hanxiang233/monice/pull/new/dev2remote: To git@github.com:hanxiang233/monice.git * [new branch] dev2 -> dev2
查看github上更新上传的分支
标签管理类似于快照功能,可以给版本库打上一个标签,记录某个时刻库的状态,也可以随时恢复到该状态
标签是为了方便区分版本,方便版本管理给一个分支打上标签给分支打上标签并查看所有标签,和某个标签的详细信息----------------切换到分支中,然后给当前分支打上标签[root@nfs1 monice]# git checkout masterSwitched to branch 'master'[root@nfs1 monice]# git tag v1----------------查看所有标签[root@nfs1 monice]# git tagv1-----------------查看标签详细信息[root@nfs1 monice]# git show v1commit 4f122c38f59a2844cad0ecda1397e3d89e15cca9Author: Your NameDate: Sat Nov 24 16:11:08 2018 +0800 newchdiff --git a/2.txt b/2.txtindex a29acaf..28d9555 100644--- a/2.txt+++ b/2.txt@@ -1,7 +1,8 @@ <<<<<<< HEAD 123456 222222-=======+======+3545 = 123456 123456 >>>>>>> newch
针对commit打标签
tag是针对commit打标签的,所以可以针对历史commit来打标签首先查看历史commit记录(每次commit的记录),再针对某个历史commit打上标签历史commit[root@nfs1 monice]# git log --pretty=oneline4f122c38f59a2844cad0ecda1397e3d89e15cca9 newch5d2b10e87a8a32f8e28f648528e39419f5b785f6 22226dbc773b363afd438e8e7155647a2ce2f1efdf8f 123453bc8b0298eba9b921fe0c25631e423456cbf5c8e 1234567b6954b358a3a3b0ea45eca870da005c7b980bb3 1234562390d8e468434a02895d4dbdf9e59a47239c9245 new shell5ac93e949773f681ee78b1f32d27da9db2f98a3d first commit
将某个commit打上标签
[root@nfs1 monice]# git tag v0.8 3bc8b0298eba
对标签添加描述信息
对一个标签添加描述信息,并测试删除该标签git tag -d tagID 删除标签[root@nfs1 monice]# git tag -a v0.1 -m "tag just 0.1" 2390d8e[root@nfs1 monice]# git tag -d v0.1Deleted tag 'v0.1' (was a55a8c5)[root@nfs1 monice]# git tagv0.8v1
对远程的git推送标签
推送指定标签[root@nfs1 monice]# git push origin v0.8Total 0 (delta 0), reused 0 (delta 0)To git@github.com:hanxiang233/monice.git * [new tag] v0.8 -> v0.8
查看远程的git标签
对远程推送所有标签
将本地创建的所有标签都push到git远程上[root@nfs1 monice]# git push --tag originCounting objects: 3, done.Compressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 416 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:hanxiang233/monice.git * [new tag] v0.2 -> v0.2 * [new tag] v0.3 -> v0.3 * [new tag] v0.6 -> v0.6
再查看远程标签
删除本地标签
首先查看所有标签,再删除本地的标签---------删除本地标签[root@nfs1 monice]# git tagv0.2v0.3v0.6v0.8v1[root@nfs1 monice]# git tag v0.2 -dDeleted tag 'v0.2' (was 6e95f24)[root@nfs1 monice]# git tag v0.3 -dDeleted tag 'v0.3' (was 0e55601)[root@nfs1 monice]# git tag v0.6 -dDeleted tag 'v0.6' (was 2e64833)[root@nfs1 monice]# git tag v0.8 -dDeleted tag 'v0.8' (was 3bc8b02)[root@nfs1 monice]# git tag v1 -dDeleted tag 'v1' (was 4f122c3)
删除远程标签
首先查看远程标签并查看删除后的远程git状态
[root@nfs1 monice]# git push origin :refs/tags/v1To git@github.com:hanxiang233/monice.git - [deleted] v1[root@nfs1 monice]# git push origin :refs/tags/v0.8To git@github.com:hanxiang233/monice.git - [deleted] v0.8[root@nfs1 monice]# git push origin :refs/tags/v0.6To git@github.com:hanxiang233/monice.git - [deleted] v0.6[root@nfs1 monice]# git push origin :refs/tags/v0.3To git@github.com:hanxiang233/monice.git - [deleted] v0.3[root@nfs1 monice]# git push origin :refs/tags/v0.2To git@github.com:hanxiang233/monice.git - [deleted] v0.2
删除后远程git标签没有了
使用别名可以提高工作效率
定义git命令别名定义commit、checkout和branch的命令别名,操作过程如下:[root@nfs1 monice]# git config --global alias.ci commit[root@nfs1 monice]# git config --global alias.co checkout[root@nfs1 monice]# git config --global alias.br branch
查看git别名使用的命令
不使用grep过滤出别名配置的话,会输出所有的git配置[root@nfs1 monice]# git config --list |grep aliasalias.ci=commitalias.co=checkoutalias.br=branch
取消git别名
git config --global --unset alias.br[root@nfs1 monice]# git config --global --unset alias.br[root@nfs1 monice]# git config --list |grep aliasalias.ci=commitalias.co=checkout
配置文件
这些配置都是存储在gitconfig配置文件中的,配置文件再root目录下用cat查看配置文件[root@nfs1 monice]# cat /root/.gitconfig [user] email = you@example.com name = Your Name[push] default = simple[alias] ci = commit co = checkout
转载于:https://blog.51cto.com/8844414/2321694