Git++ - 有趣的命令
本文主要介紹一些我在日常開發(fā)中覺得比較有趣的Git基礎(chǔ)命令
差異對比
日常開發(fā)中,差異對比是執(zhí)行比較頻繁的命令
HEAD一般指向當(dāng)前分支的最后一次Commit,下面三種是我最常用的差異對比
git diff
尚未暫存的Working Tree與HEAD的差異
git diff –cached
Index與HEAD的差異
注 : 別名git diff --staged
git diff HEAD
尚未暫存的Working Tree?+?Index與HEAD的差異
操作實例
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 | $ gst -sb## master$ lsREADME.md$ echo 'Hello' >> README.md$ gst -sb## masterM README.md$ git diffdiff --git a/README.md b/README.mdindex e69de29..e965047 100644--- a/README.md+++ b/README.md@@ -0,0 +1 @@+Hello$ git diff --cached # Stdout print nothing$ git diff HEADdiff --git a/README.md b/README.mdindex e69de29..e965047 100644--- a/README.md+++ b/README.md@@ -0,0 +1 @@+Hello-------------------$ git add README.md-------------------$ gst -sb## masterM README.md$ git diff # Stdout print nothing$ git diff --cacheddiff --git a/README.md b/README.mdindex e69de29..e965047 100644--- a/README.md+++ b/README.md@@ -0,0 +1 @@+Hello$ git diff HEADdiff --git a/README.md b/README.mdindex e69de29..e965047 100644--- a/README.md+++ b/README.md@@ -0,0 +1 @@+Hello |
移除版本控制
git rm只會將文件或目錄從版本控制中移除,但并不會從以前的提交記錄中移除文件或目錄
git rm
從Working Tree和Index中移除
必須進行up-to-date檢查,如果文件或目錄在Working Tree或Index中的狀態(tài)與HEAD不一致,則執(zhí)行失敗
| 1234567891011121314151617181920212223242526272829303132333435363738394041 | $ gst -sb## master$ lsREADME.md$ git rm README.mdrm 'README.md'$ gst -sb## masterD README.md$ ls # Stdout print nothing-------------------------------------------------$ git reset --hard HEAD # Reset HEAD , danger opsHEAD is now at 03e27f0 init commit-------------------------------------------------$ echo 'Hello' > README.md$ gst -sb## masterM README.md$ git rm README.mderror: the following file has local modifications:README.md(use --cached to keep the file, or -f to force removal)$ git add README.md$ gst -sb## masterM README.md$ git rm README.mderror: the following file has local modifications:README.md(use --cached to keep the file, or -f to force removal) |
git rm -f
強制從Working Tree和Index中移除,不進行up-to-date檢查
| 12345678910111213141516171819202122 | $ gst -sb## master$ lsREADME.md$ echo 'Hello' > README.md$ git add README.md$ gst -sb## masterM README.md$ git rm -f README.mdrm 'README.md'$ gst -sb## masterD README.md$ ls # Stdout print nothing |
git rm –cached
只從Index中移除,保留Working Tree中的文件狀態(tài)
| 123456789101112131415161718192021222324 | $ gst -sb## master$ lsREADME.md$ echo 'Hello' > README.md$ git add README.md$ gst -sb## masterM README.md$ git rm --cached README.mdrm 'README.md'$ gst -sb # README從Index刪除,但保留在Working Tree中## masterD README.md?? README.md$ cat README.mdHello |
重命名
| 12345678910111213141516171819202122 | $ gstOn branch masternothing to commit, working tree clean$ ls README.md src$ find README.md srcREADME.mdsrcsrc/Hello.javasrc/World.java$ git mv README.md README.md.bak$ git mv src src.bak$ git -sb## masterR README.md -> README.md.bakR src/Hello.java -> src.bak/Hello.javaR src/World.java -> src.bak/World.java |
查看提交歷史
常規(guī)查看(最常用)
--graph:圖形化顯示分支提交歷史
--oneline:一個提交顯示一行
--decorate:顯示分支引用
| 123456789101112 | $ git log --graph --oneline --decorate* 807adc2 (HEAD -> master) C8* 537a716 C7* a272a81 M1|\| * c94d37d (topic) C4| * 25737e7 C3* | 78dd014 C6* | 92ad9ff C5|/* c110877 C2* 3847d71 C1 |
格式化顯示
%h:Commit對象的簡短哈希串
%t:Tree對象的簡短哈希串
%p:父Commit對象的簡短哈希串
%an:作者名字
%ae:作者郵件
%ad:修訂日期
%s:Commit Message
| 123456789101112 | $ git log --pretty=format:"%h %t %p %an %s" --graph* 807adc2 bf5ac3a 537a716 zhongmingmao C8* 537a716 f2e0d63 a272a81 zhongmingmao C7* a272a81 525cbc2 78dd014 c94d37d zhongmingmao M1|\| * c94d37d 99248a5 25737e7 zhongmingmao C4| * 25737e7 cc4ec62 c110877 zhongmingmao C3* | 78dd014 a2622d9 92ad9ff zhongmingmao C6* | 92ad9ff 771d565 c110877 zhongmingmao C5|/* c110877 e7b3299 3847d71 zhongmingmao C2* 3847d71 fd092f0 zhongmingmao C1 |
提交歷史搜索
--grep:搜索提交說明
--author:匹配作者
--committer:匹配提交者
--after:時間起點
--before:時間終點
--:特定路徑
| 123456789101112 | # 查找條件$ git log --oneline --decorate --graph --grep=C --author=zhongmingmao --committer=zhongmingmao \--after=2017-01-01 --before=2018-01-01 -- .* 807adc2 (HEAD -> master) C8* 537a716 C7* c94d37d (topic) C4* 25737e7 C3| * 78dd014 C6| * 92ad9ff C5|/* c110877 C2* 3847d71 C1 |
撤銷操作
撤銷Commit
git commit --amend會重新生成新的Commit對象
修改提交日志
| 123456789101112131415 | $ git log --oneline --decorate --graph* d12ae38 (HEAD -> master) add README.md$ gstOn branch masternothing to commit, working tree clean$ git commit --amend -m 'amend commit msg' # Create new commit object[master 4e4145a] amend commit msgDate: Fri Apr 14 19:39:45 2017 +08001 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 README.md$ git log --oneline --decorate --graph* 4e4145a (HEAD -> master) amend commit msg |
合并修改+修改提交日志
| 123456789101112131415161718 | $ git log --oneline --decorate --graph* 9c9ab11 (HEAD -> master) C2* e1fbcea C1$ gst -sb## masterA C3$ git commit --amend -m 'C3' # Create new commit object , merge last commit(C2) [master de41093] C3Date: Fri Apr 14 19:48:58 2017 +08002 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 C2create mode 100644 C3 $ git log --oneline --decorate --graph* de41093 (HEAD -> master) C3* e1fbcea C1 |
撤銷Index的修改
git reset HEAD?==?git reset --mixed HEAD,用HEAD覆蓋Index
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 | $ gst -sb## masterMM C3$ git diffdiff --git a/C3 b/C3index f70f10e..35d242b 100644--- a/C3+++ b/C3@@ -1 +1,2 @@A+B$ git diff --cacheddiff --git a/C3 b/C3index e69de29..f70f10e 100644--- a/C3+++ b/C3@@ -0,0 +1 @@+A$ git diff HEADdiff --git a/C3 b/C3index e69de29..35d242b 100644--- a/C3+++ b/C3@@ -0,0 +1,2 @@+A+B$ git reset HEAD C3 # Just reset Index , Not Working TreeUnstaged changes after reset:M C3$ gst -sb## masterM C3 $ git diffdiff --git a/C3 b/C3index e69de29..35d242b 100644--- a/C3+++ b/C3@@ -0,0 +1,2 @@+A+B |
撤銷Working Tree的修改
git checkout --,用Index覆蓋Working Tree
| 1234567891011121314151617181920 | $ gst -sb## masterMM C3$ git diffdiff --git a/C3 b/C3index f70f10e..35d242b 100644--- a/C3+++ b/C3@@ -1 +1,2 @@A+B$ git checkout -- C3$ gst -sb## masterM C3$ git diff # Stdout print nothing |
撤銷Index和Working Tree的修改
git reset --hard HEAD,git checkout HEAD [filename]是危險操作,將會丟失上次Commit后的所有修改,用HEAD覆蓋Index和Working Tree
| 1234567891011121314 | $ git log --oneline --decorate --graphde41093 (HEAD -> master) C3* e1fbcea C1$ gst -sb## masterMM C3$ git reset --hard HEADHEAD is now at de41093 C3$ gstOn branch masternothing to commit, working tree clean |
| 123456789 | $ gst -sb## masterMM C3$ git checkout HEAD C3$ gstOn branch masternothing to commit, working tree clean |
總結(jié)
以上是生活随笔為你收集整理的Git++ - 有趣的命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工作以后如何有效学习
- 下一篇: 深度学习入门误区