继续上篇Git基础教程(一),在开篇之前,先回顾一下上篇中的基本命令。
1 配置命令:git config --global * 2 版本库初始化:git init 3 向版本库添加文件:git add * 4 提交文件:git commit * 5 查看状态:git status 6 查看修改详情:git diff * 7 查看日志:git log (*) 8 恢复版本:git reset * 9 撤销:git checkout -- * 10 删除:git rm * 11 12 配置github:ssh-keygen -t rsa -C "******@163.com" 13 添加远程库:git remote add * 14 调试:ssh -T -v git@github.com 15 查询远程库:git remote -v 16 删除远程库文件: 17 git rm -f gitReadme.md 18 git commit -m "delete file Gitreadme.md" --先删除本地库 19 git push origin master 20 删除远程库:git remote remove origin 21 克隆远程库:git clone *
(续)4,分支管理
1)查看当前分支
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git branch 3 * master
2)创建并切换
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git branch dev 3 4 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 5 $ git checkout dev 6 Switched to branch 'dev'
等价语句:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 2 $ git checkout -b working 3 Switched to a new branch 'working'
git branch
命令会列出所有分支,当前分支前面会标一个*
号。
3)合并分支工作成果
在working分支添加一个readme.md
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (working) 2 $ git add readme.md 3 4 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (working) 5 $ git commit -m 'add readme.md' 6 [working 59dc0fe] add readme.md 7 1 file changed,16 insertions(+) 8 create mode 100644 readme.md 9
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (working) 2 $ cat readme.md 3 ---------- 4 # 数据结构学习笔记 # 5 >**本笔记来自学习《数据结构与算法.严蔚敏(48集)》视频,[数据结构(C语言版)].严蔚敏_吴伟民.扫描版.pdf。** 6 7 8 ---------- 9 # 第01章 绪论 # 10 ## 学习要点 ## 11 1.熟悉各名词、术语的含义,掌握基本概念; 12 2.理解算法五个要素的确切含义; 13 3.掌握计算语句频度和估算算法时间复杂度的方法。 14 ## 代码块 ## 15 -矩阵乘法 16 -选择排序 17 -冒泡排序 18 19 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 20 $ cat readme.md 21 cat: readme.md:No such file or directory
把working上的工作成果合并到master上来:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git merge working 3 Updating b460b3f..59dc0fe 4 Fast-forward 5 readme.md |16++++++++++++++++ 6 1 file changed,16 insertions(+) 7 create mode 100644 readme.md
删除分支:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git branch -d dev 3 Deleted branch dev (was b460b3f).
强行删除分支:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git branch -d working 3 error:The branch 'working' is not fully merged. 4 If you are sure you want to delete it, run 'git branch -D working'.
遇到问题如下:
分析:
任务未完成。解决:
小结:往远程库添加一个文件的步骤如下:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git add readme.md 3 4 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 5 $ git commit -m "add a reame.md file." 6 [master b6fc772] add a reame.md file. 7 1 file changed,17 insertions(+) 8 create mode 100644 readme.md 9 10 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 11 $ git pull --rebase origin master 12 remote:Counting objects:2,done. 13 remote:Compressing objects:100%(2/2),done. 14 remote:Total2(delta 1), reused 0(delta 0), pack-reused 0 15 Unpacking objects:100%(2/2),done. 16 From github.com:zhangbc/dataStructure 17 * branch master -> FETCH_HEAD 18 6fb284c..8366817 master -> origin/master 19 First, rewinding head to replay your work on top of it... 20 Applying: add a reame.md file. 21 Applying:数据结构学习笔记 by zhangbc 22 Applying: add a reame.md file. 23 24 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 25 $ git push origin master 26 Counting objects:6,done. 27 Delta compression using up to 4 threads. 28 Compressing objects:100%(6/6),done. 29 Writing objects:100%(6/6),604 bytes |0 bytes/s,done. 30 Total6(delta 4), reused 0(delta 0) 31 remote:Resolving deltas:100%(4/4), completed with 2local objects. 32 To github.com:zhangbc/dataStructure.git 33 8366817..da420b4 master -> master
4)解决冲突
在分支dev提交gitReadme.md:
zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) $ cat gitReadme.md # gitReadme # ---------- Creating a new branch is quick and simple.(Dev) zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) $ git add gitReadme.md zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) $ git commit -m "Add gitReadme.md" [dev 366500d]Add gitReadme.md 1 file changed,4 insertions(+) create mode 100644 gitReadme.md
切换到主分支master上:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 2 $ git checkout master 3 Switched to branch 'master' 4 M readme.md 5 Your branch is ahead of 'origin/master' by 3 commits. 6 (use "git push" to publish your local commits) 7 8 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 9 $
在分支master修改并提交gitReadme.md:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ cat gitReadme.md 3 # gitReadme # 4 ---------- 5 Change a main branch of master is quick and simple.(Master) 6 7 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 8 $ git add gitReadme.md 9 10 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 11 $ git commit -m "Add gitReadme.md" 12 [master b466603]Add gitReadme.md 13 1 file changed,4 insertions(+) 14 create mode 100644 gitReadme.md
合并:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git merge dev 3 Auto-merging gitReadme.md 4 CONFLICT (add/add):Merge conflict in gitReadme.md 5 Automatic merge failed; fix conflicts and then commit the result.
查看文档:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master|MERGING) 2 $ cat gitReadme.md 3 # gitReadme # 4 5 ---------- 6 <<<<<<< HEAD 7 Change a main branch of master is quick and simple.(Master) 8 ======= 9 10 Creating a new branch is quick and simple.(Dev) 11 >>>>>>> dev
手动解决:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master|MERGING) 2 $ vi gitReadme.md 3 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master|MERGING) 4 $ cat gitReadme.md 5 # gitReadme # 6 ---------- 7 8 Change a main branch of master is quick and simple.(Master) 9 ---------- 10 Creating a new branch is quick and simple.(Dev) 11 12 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master|MERGING) 13 $ git add gitReadme.md 14 15 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master|MERGING) 16 $ git commit -m "Add gitReadme.md confict fixed." 17 [master 8e59a4d]Add gitReadme.md confict fixed. 18 19 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 20 $
查看日志:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git log --graph --pretty=oneline --abbrev-commit
5)分支管理策略
通常,合并分支时,如果可能,Git会用
Fast forward
模式,但这种模式下,删除分支后,会丢掉分支信息。如果要强制禁用Fast forward
模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。
创建分支dev并切换,添加文件:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git checkout -b dev 3 Switched to a new branch 'dev' 4 5 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 6 $ cat gitReadme.md 7 # gitReadme # 8 ---------- 9 Creating a new branch with --no-ff is quick and simple.(Dev) 10 11 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 12 $ git add gitReadme.md 13 14 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 15 $ git commit -m 'Add merge with --no-ff.' 16 [dev 49afeab]Add merge with --no-ff. 17 1 file changed,1 insertion(+),4 deletions(-)
切换到主分支master上,并合并:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 2 $ git checkout master 3 Switched to branch 'master' 4 Your branch is ahead of 'origin/master' by 6 commits. 5 (use "git push" to publish your local commits) 6 7 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 8 $ git merge --no-ff -m "Merge with no-ff." dev 9 Merge made by the 'recursive' strategy. 10 gitReadme.md |5+---- 11 1 file changed,1 insertion(+),4 deletions(-)
查看日志:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git log --graph --pretty=oneline --abbrev-commit
在实际开发中,我们应该按照几个基本原则进行分支管理:
首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上,在master分支发布1.0版本;每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev分支上合并即可。
5,多人协作
1)bug分支
Git还提供了一个
stash
功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作。
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 2 $ git branch 3 * dev 4 master 5 6 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 7 $ git status 8 On branch dev 9 Changes not staged for commit: 10 (use "git add <file>..." to update what will be committed) 11 (use "git checkout -- <file>..." to discard changes in working directory) 12 13 modified: readme.md 14 15 no changes added to commit (use "git add" and/or "git commit -a") 16 17 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 18 $ git stash 19 Saved working directory and index state WIP on dev:49afeabAdd merge with --no-ff. 20 HEAD is now at 49afeabAdd merge with --no-ff. 21 22 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 23 $ git checkout master 24 Switched to branch 'master' 25 Your branch is ahead of 'origin/master' by 10 commits. 26 (use "git push" to publish your local commits) 27 28 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 29 $ git checkout -b issue-101 30 Switched to a new branch 'issue-101' 31 32 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (issue-101) 33 $ git add readme.md 34 35 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (issue-101) 36 $ git commit -m "fix bug 101" 37 On branch issue-101 38 nothing to commit, working tree clean 39 40 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (issue-101) 41 $ git checkout master 42 Switched to branch 'master' 43 Your branch is ahead of 'origin/master' by 10 commits. 44 (use "git push" to publish your local commits) 45 46 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 47 $ git merge --no-ff -m "merge bug fix 101" issue-101 48 Already up-to-date. 49 50 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 51 $ git branch -d issue-101 52 Deleted branch issue-101(was c182956). 53 54 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 55 $ git checkout dev 56 Switched to branch 'dev' 57 58 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 59 $ git status 60 On branch dev 61 nothing to commit, working tree clean 62 63 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 64 $ git stash list 65 stash@{0}: WIP on dev:49afeabAdd merge with --no-ff. 66 67 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 68 $ git stash pop 69 On branch dev 70 Changes not staged for commit: 71 (use "git add <file>..." to update what will be committed) 72 (use "git checkout -- <file>..." to discard changes in working directory) 73 74 modified: readme.md 75 76 no changes added to commit (use "git add" and/or "git commit -a") 77 Dropped refs/stash@{0}(ab63aa01e37e908d49ab921656884b29b17aeac1) 78 79 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 80 $ git stash list 81 82 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev)
2)feature分支
开发一个新feature,最好新建一个分支;如果要丢弃一个没有被合并过的分支,可以通过
git branch -D <name>
强行删除。
3)多人协作
查看远程库信息:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 2 $ git remote 3 origin 4 5 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 6 $ git remote -v 7 origin git@github.com:zhangbc/dataStructure.git (fetch) 8 origin git@github.com:zhangbc/dataStructure.git (push)
推送分支:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 2 $ git push origin dev 3 Counting objects:20,done. 4 Delta compression using up to 4 threads. 5 Compressing objects:100%(20/20),done. 6 Writing objects:100%(20/20),1.81KiB|0 bytes/s,done. 7 Total20(delta 12), reused 0(delta 0) 8 remote:Resolving deltas:100%(12/12), completed with 2local objects. 9 To github.com:zhangbc/dataStructure.git 10 *[new branch] dev -> dev
推送原则:
(1)master分支是主分支,因此要时刻与远程同步;
(2)dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;(3)bug分支只用于在本地修复bug;(4)feature分支是否推到远程,取决于合作开发。
抓取分支:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (dev) 2 $ git clone git@github.com:zhangbc/PythonWorking.git 3 Cloning into 'PythonWorking'... 4 remote:Counting objects:90,done. 5 remote:Compressing objects:100%(2/2),done. 6 remote:Total90(delta 0), reused 0(delta 0), pack-reused 88 7 Receiving objects:100%(90/90),18.55KiB|6.00KiB/s,done. 8 Resolving deltas:100%(47/47),done.
6,标签管理
发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本。将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来。所以,标签也是版本库的一个快照。
创建标签:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git tag V1.0
查看标签:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git tag 3 V1.0
补打标签,先查找commit id:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git log --pretty=oneline --abbrev-commit 3 c182956 merge bug fix 101 4 2fd9cb4 fix bug 101 5 ffb5db6 Merge with no-ff. 6 49afeabAdd merge with --no-ff. 7 8e59a4dAdd gitReadme.md confict fixed. 8 b466603 Add gitReadme.md 9 366500dAdd gitReadme.md 10 63b712aMerge branch 'dev' 11 4d28e6d an example 12 6da4fcd an example 13 da420b4 add a reame.md file. 14 cfce135 数据结构学习笔记 by zhangbc 15 5433fd9 add a reame.md file. 16 8366817Delete README.md 17 6fb284c数据结构学习笔记 by zhangbc 18 ac69d7d 数据结构学习笔记 by zhangbc 19 bde6080 add readme.md 20 39013c9Create README.md 21 b460b3f 数据结构学习笔记 by zhangbc 22 23 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 24 $ git tag v0.1 da420b4
查看标签详情:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git show V1.0 3 commit c182956d8cf80b393b18027cc6bb010481aeae40 4 Merge: ffb5db6 2fd9cb4 5 Author: zhangbc <zhangbochengcheng189@163.com> 6 Date:MonMar616:47:202017+0800 7 8 merge bug fix 101
创建带有说明的标签:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git tag -a V0.2-m "Version 0.1 released"5433fd9
另外,创建带私钥签名的标签(配置略过):
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git tag -s -a V0.3-m "Version 0.3 released"
删除标签:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git tag -d show 3 Deleted tag 'show'(was c182956)
推送标签:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git push origin V0.2 3 Counting objects:1,done. 4 Writing objects:100%(1/1),168 bytes |0 bytes/s,done. 5 Total1(delta 0), reused 0(delta 0) 6 To github.com:zhangbc/dataStructure.git 7 *[new tag] V0.2-> V0.2 8 9 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 10 $ git push origin --tag 11 Total0(delta 0), reused 0(delta 0) 12 To github.com:zhangbc/dataStructure.git 13 *[new tag] V1.0-> V1.0 14 *[new tag] v0.1-> v0.1
删除远程库标签:
1 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 2 $ git tag -d V0.2 3 Deleted tag 'V0.2'(was 30a96b6) 4 5 zhangbc@working MINGW64 /d/BaiduYunDownload/dataStructure (master) 6 $ git push origin :refs/tags/V0.2 7 To github.com:zhangbc/dataStructure.git 8 -[deleted]V0.2
7,自定义Git
让Git显示不同的颜色:
1 Administrator@WIN-9S4D59CISAA MINGW64 ~(master) 2 $ git config --global color.ui true
在Git工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件。
配置别名:
1 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master) 2 $ git config --global alias.st status 3 4 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master) 5 $ git st 6 On branch master 7 nothing to commit, working tree clean
1 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master) 2 $ 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" 3 4 Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master) 5 $ git lg 6 *53aad04-(HEAD -> master, origin/master) C++ ignore file.(21 minutes ago)<zhangbc> 7 * c28d67e -本笔记系统说明文件 by zhangbc (35 minutes ago)<zhangbc> 8 * b797aab - delete file readmeBug.md (38 minutes ago)<zhangbc> 9 *20828c2- delete file Gitreadme.md (44 minutes ago)<zhangbc>
配置Git的时候,加上--global是针对当前用户起作用的,如果不加,那只针对当前仓库起作用。
每个仓库的配置文件:
Administrator@WIN-9S4D59CISAA MINGW64 /e/DataStructure/dataStructure (master) $ cat .git/config [core] repositoryformatversion =0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] url = git@github.com:zhangbc/dataStructure.git fetch =+refs/heads/*:refs/remotes/origin/*
当前用户的配置文件:
1 Administrator@WIN-9S4D59CISAA MINGW64 ~(master) 2 $ cat .gitconfig 3 [user] 4 name = ***** 5 email = **********@163.com 6 [color] 7 ui = true 8 [alias] 9 st = status 10 g = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'--abbrev-commit