码迷,mamicode.com
首页 > 其他好文 > 详细

git命令使用总结

时间:2014-11-28 18:39:47      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:git

一. 分支
$ git branch //查看本地分支

* master
$ git branch -r //查看远程分支
 origin/HEAD -> origin/removal
 origin/master
 origin/removal
$ git checkout -b  removal  origin/removal //新建本地分支并切换到远程分支上

$git branch -d //删除分支

二. 克隆
$ git clone ~/workspace/study/tmp.git //克隆本地仓库

三. 补丁
$ git format-patch -1 de4f9255d //生成补丁,参数分别代表生成几个补丁和commit ID
$ git apply --check //检查patch能否被打上
$ git apply 001.patch
$ git am 001.patch //apply & mail

四. Rebase
1. 创建仓库:$ git --bare init tmp.git
$ ls tmp.git
branches config description gitk.cache HEAD hooks info objects refs
2. 克隆这个仓库:
clone-1$ git clone ~/workspace/study/tmp.git
clone-1
$ ls
tmp
3. 再克隆一个:
clone-2$ git clone ~/workspace/study/tmp.git
clone-2
$ ls
tmp
4. 修改clone-1中的文件MAINTAINERS,并commit,再push origin master提交到远程master分支
5.
修改clone-2中的文件MAINTAINERS,并commit,此时push会失败,需进行rebase操作后再push,如下:
$ git fetch origin
$ git rebase origin/master

$ git push origin 
//upstream branch可不填,默认是和本地分支对应的远程分支
$ gitk //图形化查看git log
bubuko.com,布布扣
 

                                  图1-1
看上图1-1,通过对比红线以上和以下的历史信息可充分看出rebase和git pull(merge)的区别, clone…… 的信息,是clone-1分支上的提交,clone2……是clone-2上的提交,无论哪个分支先push进origin master,后面的提交者,先rebase origin/master,然后再push,就形成了红线上方的提交历史,很清晰,只基于一条主线。

6. 注意事项:
修改统一个文件,如果修改的是统一个地方,rebase会conflict,即便你觉得没有修改同一行,但是修改的上下文有重叠也会被认为冲突。

五. git log / gitk
$ git log --author="somebody" //查看作者名为somebody的提交历史
$ git log <文件名>/<目录名> //查看某个文件/目录的提交历史

六. git diff

      $ git diff           (1)
      $ git diff --cached  (2)
      $ git diff HEAD      (3)

1. Changes in the working tree not yet staged for the next commit.
   显示当前目录下修改的但还没有缓存(git add添加到Index中)的文件.
2. Changes between the index and your last commit; what you would be committing if you run "git commit" without "-a" option.
  当前Index中的和上一次commit之间的修改; 也就是你下一次commit将会提交的内容,假设你不在git commit加-a选项(-a表示自动commit所有修改或者删除的文件)
 3. Changes in the working tree since your last commit; what you would be committing if you run "git commit -a"
    
从上一次commit开始,当前目录下的修改(不管修改的文件有没有被缓存,都会列出来)
 
      
 Comparing with arbitrary commits——比较任意的commit之间的修改
              $ git diff test           (1)
              $ git diff HEAD -- ./test (2)
              $ git diff HEAD^ HEAD     (3)

1. Instead of using the tip of the current branch, compare with the tip of "test" branch.
   不与当前branch进行比较,而是与test branch进行比较
2. Instead of comparing with the tip of "test" branch, compare with the tip of the current branch, but limit the comparison to the file "test".
   比较当前branch的上一个commit中的./test文件和当前目录下的./test文件的修改
3. 比较上一个commit和上上一个commit之间的修改.

      Comparing branches——branch之间比较

              $ git diff topic master   (1)
              $ git diff topic..master  (2)
              $ git diff topic...master (3)

1. Changes between the tips of the topic and the master branches.
   比较topic 分支和master分支之间的不同
2. Same as above.
   同上。
3. Changes that occurred on the master branch since when the topic branch was started off it.
   比较topic分支从master分出来之后,topic上有哪些修改

更多git diff例子,参照git diff --help

七. git rm --cache //只从index中删除,不删除本地文件
八. git reset HEAD^ 回退单独的文件,如果已经提交commit,但是想在这个commit中去掉某个文件,可以单独回退这个文件
九.回退后的文件,用git status 查看,显示为已经更改,没有add到index中,如果想撤销更改,可以:
git checkout


九. git rebase HEAD^ --onto v3.18-rc1

将 HEAD^ rebase到 v3.18-rc1,HEAD^后面的commit会基于v3.18-rc1

本文出自 “蓝月的职场人生” 博客,请务必保留此出处http://lyrazhang.blog.51cto.com/8093569/1583977

git命令使用总结

标签:git

原文地址:http://lyrazhang.blog.51cto.com/8093569/1583977

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!