git小技巧
- 2020 年 4 月 1 日
- 筆記

git stash
應用場景
- 當正在某個分支A上開發某個項目,這時項目中出現一個bug需要緊急修復,但是正在開發的內容只是完成一半還不想提交,這時
git stash命令可以將修改的內容保存至堆棧區,等修復完成後,再次切回到dev分支,從堆棧中恢復剛剛保存的內容。 - 想比較修改文件部分的性能進些比較,不想再拷貝一份源碼,可以利用
git stash進些多個版本的切換而不必提交。 - 本應該在dev分支開發的內容,卻在master上進行了開發,需要重新切回到dev分支上進行開發,可以用
git stash將內容保存至堆棧中,切回到dev分支後,再次恢復內容即可。
命令
將所有未提交的修改(工作區和暫存區)保存至堆棧中,可用於後續恢復當前工作目錄。
試驗項目目錄:
LICENSE test
1. 把當前修改添加到臨時堆棧中:
$ git stash Saved working directory and index state WIP on master: 454104b Initial commit HEAD is now at 454104b Initial commit
註:如何該文件是新添加的,需要git add先添加到暫存區。
2. 把當前修改添加到臨時堆棧中,並可以通過save命名:
$ git stash save "fix test field" Saved working directory and index state On master: fix test field HEAD is now at 454104b Initial commit
3. 查看當前stash中的內容:
$ git stash list stash@{0}: On master: fix test field4. 將當前stash中的內容彈出(彈出內容會刪除),並應用到當前分支對應的工作目錄上:
$ git stash pop
pop採用的是先進後出:
$ git stash list stash@{0}: On master: add test2 stash@{1}: On master: add test1 stash@{2}: On master: add test $ git stash pop # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: LICENSE # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (ea8db073e65ab6d9cad25609838a7f2e49c011a9) $ git stash list stash@{0}: On master: add test1 stash@{1}: On master: add test5. 堆棧中的內容應用到當前目錄,而不刪除:
使用apply:
$ git stash apply stash@{1} # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: LICENSE # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test no changes added to commit (use "git add" and/or "git commit -a")6. 清除堆棧中的所有內容:
$ git stash clear
7. 從堆棧中移除某個指定的stash:
$ git stash list stash@{0}: WIP on master: 454104b Initial commit stash@{1}: On master: add test1 stash@{2}: On master: add test $ git stash drop stash@{1} Dropped stash@{1} (e998224cb579b6a8ae44795abf40b7658a90d487) $ git stash list stash@{0}: WIP on master: 454104b Initial commit stash@{1}: On master: add test- 查看堆棧中最新保存的stash和當前目錄的差異:$ git stash show LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
git commit –amend
應用場景
在commit之後發現發現漏掉了幾個文件沒有添加,或者提交資訊寫錯,可以用--amend修改並重新提交。
命令
比如:
$ git commit -m 'initial commit' $ git add forgotten_file $ git commit --amend
註:當你在修補最後的提交時,並不是通過用改進後的提交 原位替換 掉舊有提交的方式來修復的, 理解這一點非常重要。從效果上來說,就像是舊有的提交從未存在過一樣,它並不會出現在倉庫的歷史中。在 Git 中任何 commit 的東西幾乎總是可以恢復的,那些被刪除的分支中的提交或使用 –amend 選項覆蓋的提交也可以恢復
場景:
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: test #
如果添加錯了一個修改到暫存區,我們可以通過git reset HEAD的方式重置:
$ git reset HEAD test $ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test nothing added to commit but untracked files present (use "git add" to track)
注意:請務必記得 git checkout -- <file> 是一個危險的命令。 你對那個文件在本地的任何修改都會消失——Git 會用最近提交的版本覆蓋掉它。 除非你確實清楚不想要對那個文件的本地修改了,否則請不要使用這個命令。
