Git的一些小技巧

git 是我接触比较多的一个源代码管理工具,个人感觉非常灵活,虽然命令体系很庞大,但常用的就那么几个。

现将工作中常常遇到的一些问题的解决技巧贴上。算是备忘吧。

先上几个常用的配套工具:

gitk / tig : gitk是一个图形化的git,可以看到代码的整个分支树,查看每个分支的DIFF等详细信息,tig实现同样功能,只是能在终端里直观地显示,对终端控来说是个不错的选择!

tree: 查看当前目录的子目录树。


× 如何将最近的几个COMMIT压缩成一个?

很多时候都会这样,提交了一次,发现有个单词拼错了,改一下又提交一次,发现代码有小漏洞改一下又提交一次。。。

久而久之积攒下来的鸡毛蒜皮的commit就会很多。类似这种情况,其实可以很简单将这个几小提交压成一个: 回到之前的某个一次commit,然后重新提交一次就好了。

[plain] view plain copy
  1. git reset --soft <commit>  
<commit>为你要返回的提交,--soft选项很重要,它确保只是当前的HEAD指向<commit>,index和working directory里的

代码和信息还是目前最新的。将然后再执行:

[plain] view plain copy
  1. git commit -a  

输入新的提交信息就行了。这样就把<commit>之后的所有提交压缩为你现在提交的这个。

另一个方法是用git rebase,这个可以压缩任意多个提交。

× 如何修改某次commit 的信息?

我知道的只有下面这个方法 :

[plain] view plain copy
  1. git rebase -i <commit>  
<commit> 是你要修改信息的commit之前的任意一个commit。这个命令首先会将<commit>之后所有commit信息列出来,类似这样
[plain] view plain copy
  1. pick 780a327 master6  
  2. pick c7be6fe master7  
  3.   
  4. # Rebase f9436a1..c7be6fe onto f9436a1  
  5. #  
  6. # Commands:  
  7. #  p, pick = use commit  
  8. #  r, reword = use commit, but edit the commit message  
  9. #  e, edit = use commit, but stop for amending  
  10. #  s, squash = use commit, but meld into previous commit  
  11. #  f, fixup = like "squash", but discard this commit's log message  
  12. #  x, exec = run command (the rest of the line) using shell  
  13. #  
  14. # If you remove a line here THAT COMMIT WILL BE LOST.  
  15. # However, if you remove everything, the rebase will be aborted.  

最上面两行是每行从左到右分别是命令,commit的SHA码和提交信息,将你要修改提交信息的commit前将pick改为edit或reword,保存退出,然后就会出现对应的commit信息,修改之后保存就可以了。

你可能感兴趣的:(Git的一些小技巧)