天天看点

GIT版本管理

在之前的GIT入门介绍中,我们了解了本地一些GIT操作的命令和原理,在这一节将一起学习一下GIT远程管理和版本管理的相关内容。

远程仓库

如果要在github上管理我们的代码,并在本地进行修改提交就需要了解git 远程仓库的相关内容。github是git官方提供的一个代码托管平台,个人可以申请免费的,但是所上传的内容可以方便大家共享,如果要使用私密仓库,需要付费。

在github上添加SSH KEY:

在github上注册好账户之后,添加本机的SSH公钥,这样在本地在对远程仓库操作时就不用频繁的使用账号密码。

1

<code>ssh</code><code>-keygen -t rsa -C </code><code>"[email protected]"</code>

一路默认回车之后,cat .ssh/id_rsa.pub ,然后将cat的内容添加到账户的SSH Key中。

创建第一个个人仓库

首先在github上注册一个个人账户,在New repository中直接输入新建仓库的名称即可。根据提示使用SSH的方法在本地添加一个个人仓库:

2

<code>git remote add origin [email protected]:YourName</code><code>/gitrepo</code><code>.git</code>

<code>git push -u origin master  </code><code># -u更新所有分支 origin默认远程仓库名称</code>

当第一次同步了所有的本地文件之后,在对文件做了commit,就可以直接使用:

<code>git push origin master</code>

这样就直接将本地更新的文件推到了远程管理库。

从远程仓库克隆

github上有很多很优秀的代码和应用,如果我们要是用别人的代码,直接可以用git clone命令将代码拉到我们本地,如果是自己的代码库可以直接拉取:

 git clone [email protected]:YourName/gitrepo.git

分支管理

在进行开发的过程中,很多情况下我们需要对分支进行管理。例如,如果是本地对一个文件进行修改,那么这个文件就是线性的修改方式,如果要对多个文件进行多次修改,而且不同的修改最终会确认一个最终的版本,最终合并的这个分支就是这个文件的最终版本,需要注意的是,只有当执行了commit 命令之后,本地的master分支才会建立。

创建并切换分支:

3

4

<code>git branche dev    </code><code>#创建dev分支</code>

<code>git checkout dev   </code><code>#切换分支到dev</code>

<code>===============</code>

<code>git checkout -b dev    </code><code># 创建并切换分支, 一条命令搞定</code>

查看分支:

<code>git branch</code>

对分支修改后git add , git commit 之后就可切换到master分支上合并。

合并dev到master分支:

<code>git merge dev</code>

删除分支:

<code>git branch -d dev</code>

当在两个分支上同时修改了文件并且提交后,在git合并时就会出现冲突的报错。这是因为当我们在合并的时候程序也不知道我们到底需要更新哪一个,这就需要我们手动去更新文件,解决冲突。然后再合并。

5

6

7

8

9

10

11

<code>[root@work gitrepo]</code><code># git merge test # 在master和test分支上都commit后,再merge会报错</code>

<code>Auto-merging readme.txt</code>

<code>CONFLICT (content): Merge conflict </code><code>in</code> <code>readme.txt</code>

<code>Automatic merge failed; fix conflicts and </code><code>then</code> <code>commit the result.</code>

<code>[root@work gitrepo]</code><code># cat readme.txt </code>

<code>test</code>

<code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD              </code><code>#查看编辑的文件,系统对我们手动要修改的地方做了标注</code>

<code>this is master </code>

<code>=======</code>

<code>this is </code><code>test</code> 

<code>&gt;&gt;&gt;&gt;&gt;&gt;&gt; </code><code>test</code>

修改文件统一后,再次执行git add 和git commit就可以解决冲突了:

<code>[root@work gitrepo]</code><code># git log --graph  --pretty=oneline --abbrev-commit</code>

<code>*   52ed4df confilt</code>

<code>|\  </code>

<code>| * 310f7e7 IT        </code><code># 显示了一个分支的修改</code>

<code>* | a8fa78b master</code>

<code>|/  </code>

<code>* b040742 </code><code>test</code>

用git log --graph命令可以看到分支合并图。

合并分支时,默认使用的是fast forward模式,但是使用这种模式是不记录其它分支的修改日志的,在实际应用中,为了更加清楚分支上的修改时间,需要加上--no-ff参数,可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。

在一般的开发过程中大豆会有如下流程:

1、leader在远程仓库创建2个分支:master和dev

2、张三和李四克隆远程仓库到本地

3、张三和李四都切换到dev分支

4、张三创建分支z3,李四创建分支l4

5、开发完成后,张三合并z3到dev,李四合并l4到dev

6、张三和李四把本地库的dev分支推送到远程dev

7、leader拉取远程库里的dev和masterd,在本地将dev合并到master 并推送到远程master

BUG分支管理

如果当前正在dev分支上开发,突然线上出了一个BUG,需要立即修复,这时候我需要暂时隐藏当前的工作,也就是保存当前dev分支上的进度 git statsh:

<code>[root@work gitrepo]</code><code># git stash</code>

<code>Saved working directory and index state WIP on dev: f241242 </code><code>test</code>

<code>HEAD is now at f241242 </code><code>test</code>

<code>[root@work gitrepo]</code><code># git status  #隐藏工作区之后,显示的工作目录为空了</code>

<code># On branch dev</code>

<code>nothing to commit, working directory clean</code>

保存了当前的工作后,我们就要去修复线上的bug了,切换到master分支,并创建一个修复的issue分支:

<code>[root@work gitrepo]</code><code># git checkout master</code>

<code>Switched to branch </code><code>'master'</code>

<code>[root@work gitrepo]</code><code># git checkout -b issue</code>

<code>Switched to a new branch </code><code>'issue'</code>

<code>[root@work gitrepo]</code><code># git branch</code>

<code>  </code><code>dev</code>

<code>* issue</code>

<code>  </code><code>master</code>

在issue上完成修复工作后,执行git add ,git commit 提交代码,然后在master上合并issue分支上的代码删除issue分支:

12

13

14

15

16

17

18

<code>[root@work gitrepo]</code><code># git add readme.txt </code>

<code>[root@work gitrepo]</code><code># git commit -m "fix issue"</code>

<code>[issue 8b29da7] fix issue</code>

<code> </code><code>1 </code><code>file</code> <code>changed, 1 insertion(+)</code>

<code> </code> 

<code>[root@work gitrepo]</code><code># git checkout master # 回到master上合并issue分支</code>

<code>[root@work gitrepo]</code><code># git merge --no-ff -m "fix bug issue" issue # 记录分支日志信息</code>

<code>Merge made by the </code><code>'recursive'</code> <code>strategy.</code>

<code> </code><code>readme.txt | 1 +</code>

<code>[root@work gitrepo]</code><code># git branch -d issue  #删除issue分支</code>

<code>Deleted branch issue (was 8b29da7).</code>

bug修复完成之后,我们要回到自己的dev分支继续我们的工作了:

19

20

21

22

23

24

25

26

27

28

29

30

<code>[root@work gitrepo]</code><code># git checkout dev</code>

<code>Switched to branch </code><code>'dev'</code>

<code>[root@work gitrepo]</code><code># git status # 原来的dev分支是空的</code>

<code>[root@work gitrepo]</code><code># git stash list # 查看我们隐藏的分支</code>

<code>stash@{0}: WIP on dev: f241242 </code><code>test</code>

<code>[root@work gitrepo]</code><code># git stash pop # 显示出隐藏的分支,并将隐藏的分支删除</code>

<code># Changes not staged for commit:</code>

<code>#   (use "git add &lt;file&gt;..." to update what will be committed)</code>

<code>#   (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)</code>

<code>#</code>

<code>#       modified:   file.txt</code>

<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>)</code>

<code>Dropped refs</code><code>/stash</code><code>@{0} (5a7f46b8a24f1a557a37b0378ee75c65387e024a)</code>

<code>[root@work gitrepo]</code><code># git status</code>

恢复隐藏的分支有两种方法:

git stash apply   # 恢复隐藏的分支

git stash drop    # 删除隐藏的分支记录

============================

git statsh pop  # 恢复隐藏的分支,并将隐藏的分支删除,用一条命令做上面两条命令的事情。

tips:

如果要开发一个新的特性,最好新建一个分支,如果开发的新分支完成一半需要删除(此时还没有提交)删除一个未提交的分支,可以使用git branch -D BranchName 强制删除。

多人协作分支管理

在进行多人协作开发的团队中,由于每个人都会去不断的修改文件,合并文件,就会出现当你想远程提交自己的代码时,碰巧别人也修改了相同的文件,这样你本地的文件和远程的文件内容就不一样了,需要手动解决冲突再进行提交。

<code>[root@work gitrepo]</code><code># git remote  # 查看远程分支</code>

<code>origin</code>

<code>[root@work gitrepo]</code><code># git remote -v # 查看远程分支详细信息</code>

<code>origin  [email protected]:AndySkyL</code><code>/gitrepo</code><code>.git (fetch)</code>

<code>origin  [email protected]:AndySkyL</code><code>/gitrepo</code><code>.git (push)</code>

提交是出现冲突:

31

32

33

34

35

36

<code>[root@work gitrepo]</code><code># git commit -m "dev2"</code>

<code>[dev 41ad4f8] dev2</code>

<code> </code><code>1 </code><code>file</code> <code>changed, 1 insertion(+), 3 deletions(-)</code>

<code>[root@work gitrepo]</code><code># git push origin dev   # 推送dev分支冲突</code>

<code>▽! [rejected]        dev -&gt; dev (fetch first)</code>

<code>error: failed to push some refs to </code><code>'[email protected]:AndySkyL/gitrepo.git'</code>

<code>hint: Updates were rejected because the remote contains work that you </code><code>do</code>

<code>hint: not have locally. This is usually caused by another repository pushing</code>

<code>hint: to the same ref. You may want to first merge the remote changes (e.g.,</code>

<code>hint: </code><code>'git pull'</code><code>) before pushing again.</code>

<code>hint: See the </code><code>'Note about fast-forwards'</code> <code>in</code> <code>'git push --help'</code> <code>for</code> <code>details.</code>

<code>[root@work gitrepo]</code><code># git pull   # 根据提示使用git pull</code>

<code>remote: Counting objects: 3, </code><code>done</code><code>.</code>

<code>remote: Compressing objects: 100% (2</code><code>/2</code><code>), </code><code>done</code><code>.</code>

<code>remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0</code>

<code>Unpacking objects: 100% (3</code><code>/3</code><code>), </code><code>done</code><code>.</code>

<code>From github.com:AndySkyL</code><code>/gitrepo</code>

<code> </code><code>* [new branch]      dev        -&gt; origin</code><code>/dev</code>

<code>There is no tracking information </code><code>for</code> <code>the current branch.</code>

<code>Please specify </code><code>which</code> <code>branch you want to merge with.</code>

<code>See git-pull(1) </code><code>for</code> <code>details</code>

<code>    </code><code>git pull &lt;remote&gt; &lt;branch&gt;     </code>

<code>If you wish to </code><code>set</code> <code>tracking information </code><code>for</code> <code>this branch you can </code><code>do</code> <code>so with:</code>

<code>    </code><code>git branch --</code><code>set</code><code>-upstream-to=origin/&lt;branch&gt; dev   </code><code># 这里已经给出提示</code>

<code>    </code> 

<code>[root@work gitrepo]</code><code># git branch --set-upstream-to=origin/dev dev </code>

<code>Branch dev </code><code>set</code> <code>up to track remote branch dev from origin.</code>

<code>[root@work gitrepo]</code><code># git pull  #执行此命令之后再按照之前的方式修改文件,解决冲突</code>

解决冲突后提交:

<code>[root@work gitrepo]</code><code># git commit -m "fix m"</code>

<code>[dev 3267ad5] fix m</code>

<code>[root@work gitrepo]</code><code># git push origin dev</code>

<code>Counting objects: 10, </code><code>done</code><code>.</code>

<code>Compressing objects: 100% (4</code><code>/4</code><code>), </code><code>done</code><code>.</code>

<code>Writing objects: 100% (6</code><code>/6</code><code>), 570 bytes | 0 bytes</code><code>/s</code><code>, </code><code>done</code><code>.</code>

<code>Total 6 (delta 0), reused 0 (delta 0)</code>

<code>   </code><code>17a9b60..3267ad5  dev -&gt; dev</code>

从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;

在本地创建和远程分支对应的分支,使用

<code>git checkout -b branch-name origin</code><code>/branch-name</code> 

<code># 本地和远程分支的名称最好一致;</code>

建立本地分支和远程分支的关联,使用:

<code>git branch --</code><code>set</code><code>-upstream-to=origin</code><code>/dev</code> <code>dev</code>

标签管理

标签可以是每一次commit 的标识,类似于给每次commit添加一个别名:

<code>* master</code>

<code>[root@work gitrepo]</code><code># git tag v1.0</code>

<code>[root@work gitrepo]</code><code># git tag</code>

<code>v1.0</code>

默认标签是打在最新提交的commit上的。

如果要对之前的某一次commit打标签后面直接接上commit ID即可:

<code>[root@work gitrepo]</code><code># git log --pretty=oneline --abbrev-commit</code>

<code>e2ce160 fix bug issue</code>

<code>8b29da7 fix issue</code>

<code>881136a fix bug example</code>

<code>[root@work gitrepo]</code><code># git tag v0.9 881136a  #根据log 对对应的commit打标签,可以使用-m</code>

<code>[root@work gitrepo]</code><code># git tag               #添加说明</code>

<code>v0.9                                       </code><code># git标签的排列默认是以标签名的字母顺序排列的</code>

使用tag查看具体信息:

<code>[root@work gitrepo]</code><code># git show v1.0</code>

<code>commit e2ce160ad30d5433c033d9be7dc5dfe23ddbfd6d</code>

<code>Merge: 881136a 8b29da7</code>

<code>Author: trying &lt;[email protected]&gt;</code>

<code>Date:   Wed Dec 14 16:16:32 2016 +0800</code>

<code>    </code><code>fix bug issue</code>

也可以在新建tag时添加说明:

<code> </code><code>git tag -a v2.0 -m </code><code>"version 2.0 released"</code>   <code># -a 指定tag名称 -m 添加说明</code>

删除标签

<code># git tag -d v2.0</code>

<code>Deleted tag </code><code>'v2.0'</code> <code>(was 959f8b1)</code>

推送标签到远程

由于在本地添加的标签不会自动推送到远程,如果需要推送本地的标签到远程,使用git push origin tagname:

<code># git push origin v1.0    # 推送指定的tag</code>

<code># git push origin --tags  # 一次推送所有的tag</code>

删除远程标签

删除远程标签需要先删除本地标签:

<code># git tag</code>

<code>v0.9</code>

<code># git tag -d v0.9    # 删除本地tag</code>

<code>Deleted tag </code><code>'v0.9'</code> <code>(was 881136a)</code>

<code># git push origin :refs/tags/v0.9   # 删除远程标签</code>

<code> </code><code>- [deleted]         v0.9</code>

git push origin :refs/tags/tagname

GIT高亮显示字体颜色:

<code># git config --global color.ui true</code>

搭建GIT服务器

如果自己不想使用github的付费仓库,可以自己搭建一个私有的git 服务器供企业内部使用。

安装git:

<code>yum </code><code>install</code> <code>git -y</code>

创建一个git用户:

<code>useradd</code> <code>git</code>

禁止git 用户登录shell,编辑/etc/passwd文件,将git用户默认的shell改为:

<code>git:x:823:823::</code><code>/home/git</code><code>:</code><code>/usr/bin/git-shell</code>

创建一个用于GIT仓库的目录:

<code>mkdir</code> <code>/gitrepo</code>

<code>cd</code> <code>/gitrepo</code>

初始化git仓库:

<code>git init --bare  </code><code>test</code><code>.git</code>

在/home/git用户的目录下创建密钥认证文件authorized_keys ,将本地的公钥导入服务端的authorized_keys 文件中:

<code>cat</code> <code>id_rsa.pub  &gt; </code><code>/home/git/</code><code>.</code><code>ssh</code><code>/authorized_keys</code>

然回到本地,就可以获取git服务器上的工作目录了:

<code>[root@work ~]</code><code># git clone [email protected]:/gitrepo/test.git</code>

<code>Cloning into </code><code>'test'</code><code>...</code>

<code>warning: You appear to have cloned an empty repository.</code>

这就可以和在github上一样操作了。

 本文转自 酥心糖 51CTO博客,原文链接:http://blog.51cto.com/tryingstuff/1883089