经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
Git 版本控制管理(一)
来源:cnblogs  作者:ThreeCode  时间:2019/3/15 8:45:04  对本文有异议

    Git 是一个分布式版本控制工具,它的作者 Linus Torvalds 是这样给我们介绍 Git  —— The stupid content tracker(傻瓜式的内容跟踪器)

关于 Git 的产生背景在此不做讲解,有兴趣的可以搜索一下。

先介绍一下 Git 的特点,主要有两大特点:

版本控制:可以解决多人同时开发的代码问题,也可以解决找回历史代码的问题。

分 布 式:Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上。首先找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交。可以自己搭建这台服务器,也可以使用GitHub网站。

1. Git 安装

  1. [root@kai ~]# yum install git -y
  2. [root@kai ~]# git --version
  3. git version 1.8.3.1

2.创建一个版本库

新建一个目录git_test,在git_test目录下创建一个版本库,命令如下:

  1. [root@kai ~]# mkdir git_test
  2. [root@kai ~]# cd git_test/
  3. [root@kai git_test]# ll -a
  4. total 0
  5. drwxr-xr-x 2 root root 6 Mar 13 21:43 .
  6. dr-xr-x---. 5 root root 228 Mar 13 21:43 ..
  7. [root@kai git_test]# git init
  8. Initialized empty Git repository in /root/git_test/.git/
  9. [root@kai git_test]# ll -a
  10. total 0
  11. drwxr-xr-x 3 root root 18 Mar 13 21:44 .
  12. dr-xr-x---. 5 root root 228 Mar 13 21:43 ..
  13. drwxr-xr-x 7 root root 119 Mar 13 21:44 .git

可以看到在git_test目录下创建了一个.git隐藏目录,这就是版本库目录。

3.版本创建与回退

3.1 基本使用

在git_test目录下创建一个文件code.txt,编辑内容如下:

  1. [root@kai git_test]# vim code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line

使用如下两条命令可以创建一个版本:

  1. [root@kai git_test]# git commit -m 'version1'
  2.  
  3. *** Please tell me who you are.
  4.  
  5. Run
  6.  
  7. git config --global user.email "you@example.com"
  8. git config --global user.name "Your Name"
  9.  
  10. to set your account's default identity.
  11. Omit --global to set the identity only in this repository.
  12. fatal: unable to auto-detect email address (got 'root@kai.(none)')
  13. # 出现该错误原因是我们没有配置git库的用户名与邮箱,按提示创建即可。
  14.  
  15. [root@kai git_test]# git config --global user.email "kai@qq.com"
  16. [root@kai git_test]# git config --global user.name "kai"
  17.  
  18. # 再次提交
  19. [root@kai git_test]# git commit -m 'version1'
  20. [master (root-commit) 020bf02] version1
  21. 1 file changed, 1 insertion(+)
  22. create mode 100644 code.txt

使用如下命令可以查看版本记录:

  1. [root@kai git_test]# git log
  2. commit 020bf021ec6d1b77836db4e96541d3659251714e
  3. Author: kai <kai@qq.com>
  4. Date: Wed Mar 13 21:57:42 2019 -0400
  5.  
  6. version1

继续编辑code.txt,在里面再增加一行:

  1. [root@kai git_test]# vim code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line
  4. this is the second line

使用如下命令再创建一个版本并查看版本记录:

  1. [root@kai git_test]# git add code.txt
  2. [root@kai git_test]# git commit -m 'version2'
  3. [master 6280fa5] version2
  4. 1 file changed, 1 insertion(+)
  5. [root@kai git_test]# git log
  6. commit 6280fa584403809ac2078a81120acf33e6bec836
  7. Author: kai <kai@qq.com>
  8. Date: Thu Mar 14 00:58:35 2019 -0400
  9.  
  10. version2
  11.  
  12. commit 020bf021ec6d1b77836db4e96541d3659251714e
  13. Author: kai <kai@qq.com>
  14. Date: Wed Mar 13 21:57:42 2019 -0400
  15.  
  16. version1
  17. [root@kai git_test]#

现在若想回到某一个版本,可以使用命令:git reset --hard HEAD^。HEAD 表示当前版本,HEAD^ 表示上一个版本,HEAD^^ 表示上上个版本。也可以使用另一种表示方式:HEAD~n 表示前n个版本。

现在若觉得想回到版本1,可以使用如下命令:

  1. [root@kai git_test]# git reset --hard HEAD^
  2. HEAD is now at 020bf02 version1
  3. [root@kai git_test]# git log
  4. commit 020bf021ec6d1b77836db4e96541d3659251714e
  5. Author: kai <kai@qq.com>
  6. Date: Wed Mar 13 21:57:42 2019 -0400
  7.  
  8. version1
  9. [root@kai git_test]# cat code.txt
  10. this is the first line
  11. [root@kai git_test]#

执行命令后使用git log查看版本记录,发现现在只能看到版本1的记录,cat code.txt查看文件内容,现在只有一行,也就是第一个版本中code.txt的内容。

假如我们现在又想回到版本2,可以使用如下命令:git reset --hard 版本号
从上面记录可以看到版本2的版本号为:6280fa584403809ac2078a81120acf33e6bec836
在终端执行如下命令:(输入版本号前几位即可)

  1. [root@kai git_test]# git reset --hard 6280fa5844
  2. HEAD is now at 6280fa5 version2
  3. [root@kai git_test]# git log
  4. commit 6280fa584403809ac2078a81120acf33e6bec836
  5. Author: kai <kai@qq.com>
  6. Date: Thu Mar 14 00:58:35 2019 -0400
  7.  
  8. version2
  9.  
  10. commit 020bf021ec6d1b77836db4e96541d3659251714e
  11. Author: kai <kai@qq.com>
  12. Date: Wed Mar 13 21:57:42 2019 -0400
  13.  
  14. version1
  15. [root@kai git_test]# cat code.txt
  16. this is the first line
  17. this is the second line
  18. [root@kai git_test]#

现在发现版本2有回来了,cat code.txt查看其里面的内容和原来的相同。

假如说上面的终端已经关了,也就是我们已经不知道版本2的版本号,该怎么回退版本2?
首先我们退回版本1:

  1. [root@kai git_test]# git reset --hard HEAD^
  2. HEAD is now at 020bf02 version1
  3. [root@kai git_test]# cat code.txt
  4. this is the first line
  5. [root@kai git_test]#

那么在不知道版本号情况下怎么再回到版本2呢?其实git reflog命令可以查看我们的操作记录。
查到版本2的版本号,我们再使用如下命令进行版本回退,版本重新回到了版本2。

  1. [root@kai git_test]# git reflog
  2. 020bf02 HEAD@{0}: reset: moving to HEAD^
  3. 6280fa5 HEAD@{1}: reset: moving to 6280fa5844
  4. 020bf02 HEAD@{2}: reset: moving to HEAD^
  5. 6280fa5 HEAD@{3}: commit: version2
  6. 020bf02 HEAD@{4}: commit (initial): version1
  7. [root@kai git_test]# git reset --hard 6280fa5844
  8. HEAD is now at 6280fa5 version2
  9. [root@kai git_test]# git log
  10. commit 6280fa584403809ac2078a81120acf33e6bec836
  11. Author: kai <kai@qq.com>
  12. Date: Thu Mar 14 00:58:35 2019 -0400
  13.  
  14. version2
  15.  
  16. commit 020bf021ec6d1b77836db4e96541d3659251714e
  17. Author: kai <kai@qq.com>
  18. Date: Wed Mar 13 21:57:42 2019 -0400
  19.  
  20. version1
  21. [root@kai git_test]#

3.2 工作区和暂存区 

1)工作区(Working Directory)
电脑中的目录,比如我们的git_test,就是一个工作区。

2)版本库(Repository)
工作区有一个隐藏目录.git,这个不是工作区,而是git的版本库。

      git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。因为我们创建git版本库时,git自动为我们创建了唯一一个master分支,所以,现在 git commit 就是往master分支上提交更改。
可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

下面上图理解:

前面说了我们把文件往git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

下面在git_test目录下再创建一个文件code2.txt,然后编辑内容,同时修改code.txt的内容:

  1. [root@kai git_test]# vim code2.txt
  2. [root@kai git_test]# cat code2.txt
  3. the code2 first line
  4. [root@kai git_test]# vim code.txt
  5. [root@kai git_test]# cat code.txt
  6. this is the first line
  7. this is the second line
  8. this is the third line

使用如下命令查看当前工作树的状态:

  1. [root@kai git_test]# git status
  2. # On branch master
  3. # Changes not staged for commit:
  4. # (use "git add <file>..." to update what will be committed)
  5. # (use "git checkout -- <file>..." to discard changes in working directory)
  6. #
  7. # modified: code.txt
  8. #
  9. # Untracked files:
  10. # (use "git add <file>..." to include in what will be committed)
  11. #
  12. # code2.txt
  13. no changes added to commit (use "git add" and/or "git commit -a")
  14. [root@kai git_test]#

上面提示我们code.txt被修改,而code2.txt没有被跟踪。

我们使用如下命令把code.txt和code2.txt加入到暂存区,然后再执行git status命令,结果如下:

  1. [root@kai git_test]# git add code.txt
  2. [root@kai git_test]# git add code2.txt
  3. [root@kai git_test]# git status
  4. # On branch master
  5. # Changes to be committed:
  6. # (use "git reset HEAD <file>..." to unstage)
  7. #
  8. # modified: code.txt
  9. # new file: code2.txt
  10. #
  11. [root@kai git_test]#

所以git add命令是把所有提交的修改存放到暂存区。

然后,执行git commit就可以一次性把暂存区的所有修改提交到分支创建一个版本。

  1. [root@kai git_test]# git commit -m 'version3'
  2. [master f18f0cc] version3
  3. 2 files changed, 2 insertions(+)
  4. create mode 100644 code2.txt
  5. [root@kai git_test]# git log
  6. commit f18f0ccadc62b83fa4c6e2222956ba2f2a0e5230
  7. Author: kai <kai@qq.com>
  8. Date: Thu Mar 14 05:16:31 2019 -0400
  9.  
  10. version3
  11.  
  12. commit 6280fa584403809ac2078a81120acf33e6bec836
  13. Author: kai <kai@qq.com>
  14. Date: Thu Mar 14 00:58:35 2019 -0400
  15.  
  16. version2
  17.  
  18. commit 020bf021ec6d1b77836db4e96541d3659251714e
  19. Author: kai <kai@qq.com>
  20. Date: Wed Mar 13 21:57:42 2019 -0400
  21.  
  22. version1
  23. [root@kai git_test]#

一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的。执行如下命令可以发现:

  1. [root@kai git_test]# git status
  2. # On branch master
  3. nothing to commit, working directory clean
  4. [root@kai git_test]#

现在我们的版本库变成了这样:

3.3 管理修改

git管理的文件的修改,它只会提交暂存区的修改来创建版本。

编辑code.txt,并使用git add 命令将其添加到暂存区中。

  1. [root@kai git_test]# vim code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line
  4. this is the second line
  5. this is the third line
  6. this is the forth line
  7. [root@kai git_test]# git add code.txt


继续编辑code.txt,并在其中添加一行。

  1. [root@kai git_test]# vim code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line
  4. this is the second line
  5. this is the third line
  6. this is the forth line
  7. this is the new line
  8. [root@kai git_test]#

git commit 创建一个版本,并使用git status查看,发现第二次修改code.txt内容之后,并没有将其添加的工作区,所以创建版本的时候并没有被提交。

  1. [root@kai git_test]# vim code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line
  4. this is the second line
  5. this is the third line
  6. this is the forth line
  7. this is the new line
  8. [root@kai git_test]# git commit -m 'version4'
  9. [master 66a9c99] version4
  10. 1 file changed, 1 insertion(+)
  11. [root@kai git_test]# git status
  12. # On branch master
  13. # Changes not staged for commit:
  14. # (use "git add <file>..." to update what will be committed)
  15. # (use "git checkout -- <file>..." to discard changes in working directory)
  16. #
  17. # modified: code.txt
  18. #
  19. no changes added to commit (use "git add" and/or "git commit -a")
  20. [root@kai git_test]#

3.4 撤销修改

继续上面的操作,提示我们可以使用 git checkout -- <文件> 来丢弃工作区的改动。执行如下命令,发现工作区干净了,第二次的改动内容也没了。

  1. [root@kai git_test]# git checkout -- code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line
  4. this is the second line
  5. this is the third line
  6. this is the forth line
  7. [root@kai git_test]# git status
  8. # On branch master
  9. nothing to commit, working directory clean
  10. [root@kai git_test]#

我们继续编辑code.txt,并在其中添加如下内容,并将其添加的暂存区。

  1. [root@kai git_test]# vim code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line
  4. this is the second line
  5. this is the third line
  6. this is the forth line
  7. the new line
  8. [root@kai git_test]# git add code.txt
  9. [root@kai git_test]# git status
  10. # On branch master
  11. # Changes to be committed:
  12. # (use "git reset HEAD <file>..." to unstage)
  13. #
  14. # modified: code.txt
  15. #
  16. [root@kai git_test]#

git同样告诉我们,用命令 git reset HEAD file 可以把暂存区的修改撤销掉,重新放回工作区。

  1. [root@kai git_test]# git reset HEAD code.txt
  2. Unstaged changes after reset:
  3. M code.txt
  4. [root@kai git_test]# git status
  5. # On branch master
  6. # Changes not staged for commit:
  7. # (use "git add <file>..." to update what will be committed)
  8. # (use "git checkout -- <file>..." to discard changes in working directory)
  9. #
  10. # modified: code.txt
  11. #
  12. no changes added to commit (use "git add" and/or "git commit -a")
  13. [root@kai git_test]#

现在若想丢弃code.txt的修改,执行如下命令即可。

  1. [root@kai git_test]# cat code.txt
  2. this is the first line
  3. this is the second line
  4. this is the third line
  5. this is the forth line
  6. the new line
  7. [root@kai git_test]# git checkout -- code.txt
  8. [root@kai git_test]# cat code.txt
  9. this is the first line
  10. this is the second line
  11. this is the third line
  12. this is the forth line
  13. [root@kai git_test]# git status
  14. # On branch master
  15. nothing to commit, working directory clean
  16. [root@kai git_test]#

现在,如果你不但改错了东西,还从暂存区提交到了版本库,则需要进行版本回退。

回退小结:
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节。

3.5 对比文件不同

对比工作区和某个版本中文件的不同

继续编辑文件code.txt,在最后添加一行 the new line,然后对比工作区中code.txt和HEAD版本中code.txt的不同。使用如下命令:

  1. [root@kai git_test]# echo "the new line" >> code.txt
  2. [root@kai git_test]# cat code.txt
  3. this is the first line
  4. this is the second line
  5. this is the third line
  6. this is the forth line
  7. the new line
  8. [root@kai git_test]# git diff HEAD -- code.txt
  9. diff --git a/code.txt b/code.txt
  10. index 66f9219..324317f 100644
  11. --- a/code.txt # - 代表HEAD版本中的 code.txt 内容
  12. +++ b/code.txt # - 代表工作区中的 code.txt 内容
  13. @@ -2,3 +2,4 @@ this is the first line
  14. this is the second line
  15. this is the third line
  16. this is the forth line
  17. +the new line # 工作区的比HEAD版本中的多了一行
  18. [root@kai git_test]#

丢弃工作区的修改

  1. [root@kai git_test]# git checkout -- code.txt
  2. [root@kai git_test]# git status
  3. # On branch master
  4. nothing to commit, working directory clean

对比两个版本间文件的不同:

  1. [root@kai git_test]# git diff HEAD HEAD^ -- code.txt
  2. diff --git a/code.txt b/code.txt
  3. index 66f9219..01e1274 100644
  4. --- a/code.txt # - 代表 HEAD 版本中的 code.txt 内容
  5. +++ b/code.txt # - 代表 HEAD^ 版本中的 code.txt 内容
  6. @@ -1,4 +1,3 @@
  7. this is the first line
  8. this is the second line
  9. this is the third line
  10. -this is the forth line # HEAD 版本的比 HEAD^ 版本中的多了一行
  11. [root@kai git_test]#

3.6 删除文件

我们把目录中的code2.txt删除。

  1. [root@kai git_test]# rm -f code2.txt

这个时候,git知道删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻提示哪些文件被删除了。

  1. [root@kai git_test]# git status
  2. # On branch master
  3. # Changes not staged for commit:
  4. # (use "git add/rm <file>..." to update what will be committed)
  5. # (use "git checkout -- <file>..." to discard changes in working directory)
  6. #
  7. # deleted: code2.txt
  8. #
  9. no changes added to commit (use "git add" and/or "git commit -a")
  10. [root@kai git_test]#

现在有两个选择,一是确实要从版本库中删除该文件,那就用命令 git rm 删掉,并且 git commit:

  1. [root@kai git_test]# git rm code2.txt
  2. rm 'code2.txt'
  3. [root@kai git_test]# git status
  4. # On branch master
  5. # Changes to be committed:
  6. # (use "git reset HEAD <file>..." to unstage)
  7. #
  8. # deleted: code2.txt
  9. #
  10. [root@kai git_test]# git commit -m 'delete_code2.txt'
  11. [master f25e944] delete_code2.txt
  12. 1 file changed, 1 deletion(-)
  13. delete mode 100644 code2.txt
  14. [root@kai git_test]# git status
  15. # On branch master
  16. nothing to commit, working directory clean
  17. [root@kai git_test]#

另一种情况是删错了,可以直接使用git checkout – code2.txt,这样文件code2.txt又回来了

删除小结:

命令 git rm 用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

原文链接:http://www.cnblogs.com/kaye/p/10529777.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号