Git之2——基本操作

来源:互联网 发布:杭州seo外包服务 编辑:程序博客网 时间:2024/05/27 20:52
添加索引--add

    可以使用git add命令将文件添加至索引中,这与svn的add命令基本相似,个人觉得可以理解为svn的add操作。

  1. [root@localhost project_test]# git add t1.txt t2.txt

很多版本控制系统都提供了一个 "add" 命令:告诉系统开始去跟踪某一个文件的改动。但是Git里的 ”add” 命令从某种程度上讲更为简单和强大. git add 不但是用来添加不在版本控制中的新文件,也用于添加已在版本控制中但是刚修改过的文件; 在这两种情况下, Git都会获得当前文件的快照并且把内容暂存(stage)到索引中,为下一次commit做好准备。

查看提交文件--diff

    git diff命令可以查看哪些文件将被提交,可以使用cached参数来指定已做修改并加入到索引中的文件。我们可以对之前创建的t1.txt进行修改。然后哦使用diff命令。

  1. [root@localhost project_test]# vim t1.txt

  2. [root@localhost project_test]# git diff

  3. diff --git a/t1.txt b/t1.txt

  4. index e69de29..3d8bd3a 100644

  5. --- a/t1.txt

  6. +++ b/t1.txt

  7. @@ -0,0 +1,3 @@

  8. +a

  9. +b

  10. +   c


  1. [root@localhost project_test]# git diff --cached

  2. fatal: No HEAD commit to compare with (yet)


当前项目状态--status

    git status命令可以获取当前项目状态。

  1. [root@localhost project_test]# git status

  2. # On branch master

  3. #

  4. # Initial commit

  5. #

  6. # Changes to be committed:

  7. #   (use "git rm --cached <file>..." to unstage)

  8. #

  9. #new file:   t1.txt

  10. #new file:   t2.txt

  11. #

  12. # Changed but not updated:

  13. #   (use "git add <file>..." to update what will be committed)

  14. #   (use "git checkout -- <file>..." to discard changes in working directory)

  15. #

  16. #modified:   t1.txt

  17. #


提交--commit

    修改完成后,将修改文件加入索引并提交,会提示添加注释,我们添加对应的注释即可。

  1. [root@localhost project_test]# git add t1.txt

  2. root@localhost project_test]# git commit

  3. [master (root-commit) 263118b] test save

  4. 1 files changed, 3 insertions(+), 0 deletions(-)

  5. create mode 100644 t1.txt

  6. create mode 100644 t2.txt

  7. [root@localhost project_test]# ls

  8. t1.txt  t2.txt

分支合并基础
创建分支

    一个git仓库可以有多个分支。首先我们可以创建一个分支。

  1. [root@localhost project_test]# git branch project_test2
    查看分支
  1. [root@localhost project_test]# git branch

  2. * master

  3.  project_test2

    *标示当前工作的分支,在分支中间我们可以进行切换。

    现在我们可以切换到project_test2分支下,然后对文件进行操作。

    分支切换
  1. [root@localhost project_test]# git checkout project_test2

  2. Switched to branch 'project_test2'

对t1.txt修改,提交,并切换回master分支。

  1. [root@localhost project_test]# vim t1.txt

  2. [root@localhost project_test]# git commit -a

  3. [project_test2 4c897c7] 测试分支

  4. 1 files changed, 1 insertions(+), 0 deletions(-)

  5. [root@localhost project_test]# git checkout master

  6. Switched to branch 'master'

查看t1.txt文件,并对其修改。

  1. [root@localhost project_test]# vim t1.txt

这样子我们两个分支分别都对文件进行了修改(diverged),我们可以对分支进行合并。如果不存在冲突则会完成合并,但是因为我们之前的操作,在这里造成了冲突。

  1. [root@localhost project_test]# git merge project_test2
  2. Updating 263118b..4c897c7
  3. error: Your local changes to 't1.txt' would be overwritten by merge.  Aborting.
  4. Please, commit your changes or stash them before you can merge.
使用git diff命令来查看冲突的文件
  1. [root@localhost project_test]# git diff
  2. diff --git a/t1.txt b/t1.txt
  3. index 3d8bd3a..49341cb 100644
  4. --- a/t1.txt
  5. +++ b/t1.txt
  6. @@ -1,3 +1,4 @@
  7. a
  8. b
  9.    c
  10. +分支master的修改

编辑冲突的文件,解决冲突。

  1. root@localhost project_test]# git merge project_test2
  2. Updating 263118b..4c897c7
  3. Fast-forward
  4. t1.txt |    1 +
  5. 1 files changed, 1 insertions(+), 0 deletions(-)

合并后,我们可以使用gitk查看项目的历史。这个插件需要单独安装。

删除分支

    在某个分支完成工作后,我们可以删除对应的分支。

  1. [root@localhost project_test]# git branch -d project_test2
  2. Deleted branch project_test2 (was 4c897c7).
使用-D来强制删除某个分支。
  1. [root@localhost project_test]# git branch branch_2
  2. [root@localhost project_test]# git branch -D branch_2
  3. Deleted branch branch_2 (was 4c897c7).
撤销合并

    如果我们觉得某个合并不太对,那么我们可以撤销合并。

  1. [root@localhost project_test]# git reset --hard HEAD

    如果已经把合并后的代码提交,可以使用如下命令。

  1. [root@localhost project_test]# git reset --hard ORIG_HEAD
在此条命令下,如果分支已经被删除的话,那么撤销操作可能会导致代码丢失。


Git 日志

    git log可以查看所有的提交日志。

  1. [root@localhost project_test]# git log
  2. commit 4c897c74dac839ff38f0b0117f1b6abf6faa9a35
  3. Author: lj <lj@test.com>
  4. Date:   Sat Aug 8 02:12:02 2015 -0700
  5.    测试分支
  6. commit 263118bccf0f043f8f150d7bb1576178eea1f17e
  7. Author: lj <lj@test.com>
  8. Date:   Sat Aug 8 01:56:48 2015 -0700
  9.    test save

    可以添加--stat参数,在日志中显示那些文件被修改。

  1. [root@localhost project_test]# git log --stat
  2. commit 4c897c74dac839ff38f0b0117f1b6abf6faa9a35
  3. Author: lj <lj@test.com>
  4. Date:   Sat Aug 8 02:12:02 2015 -0700
  5.    测试分支
  6. t1.txt |    1 +
  7. 1 files changed, 1 insertions(+), 0 deletions(-)
  8. commit 263118bccf0f043f8f150d7bb1576178eea1f17e
  9. Author: lj <lj@test.com>
  10. Date:   Sat Aug 8 01:56:48 2015 -0700
  11.    test save
  12. t1.txt |    3 +++
  13. 1 files changed, 3 insertions(+), 0 deletions(-)

比较提交——git diff

    可以使用git diff比较任意两个版本之间的差异。

    新建test分支,并修改文件,然后使用此命令。

  1. [root@localhost project_test]# git branch
  2. * master
  3. [root@localhost project_test]# git branch test
  4. [root@localhost project_test]# git checkout test
  5. Switched to branch 'test'
  6. [root@localhost project_test]# ls
  7. t1.txt  t2.txt
  8. [root@localhost project_test]# vim t1.txt
  9. [root@localhost project_test]# git diff master..test
  10. [root@localhost project_test]# git diff master...test
  11. [root@localhost project_test]# git diff master
  12. diff --git a/t1.txt b/t1.txt
  13. index 2379f05..781ec2e 100644
  14. --- a/t1.txt
  15. +++ b/t1.txt
  16. @@ -1,4 +1,5 @@
  17. a
  18. b
  19.    c
  20. -测试分支2的修改
  21. +测试分支2的修改
  22. +分支test修改


本文参考:http://gitbook.liuhui998.com/3_2.html



0 0