在平常的開發過程當中,你不免會將不應提交的代碼提交了,不應push到遠程分支的代碼push到了遠程分支,或者解決代碼衝突時,覆蓋別人代碼並提交。java
這些突發情況在初次遇到的時候都會讓你手足無措,非常慌亂,沒事,看完這篇文章,就能夠完全治癒好你慌亂。git
正常開發流程以下:首先建立咱們須要的文件--->對新建的文件進行版本控制,此時文件添加到暫存區域--->提交暫存區域的文件到本地倉庫--->推送本地倉庫的文件到遠程倉庫web
➜ java-primary-example git:(master) ✗ git add a.txt
➜ java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
複製代碼
➜ java-primary-example git:(master) ✗ git reset HEAD a.txt
➜ java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
nothing added to commit but untracked files present (use "git add" to track)
複製代碼
注意:執行步驟 4.1 和 4.2 中的操做服務器
➜ java-primary-example git:(master) ✗ git commit -m 'commit a.txt file'
[master 9487c06] commit a.txt file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
➜ java-primary-example git:(master) git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
複製代碼
➜ java-primary-example git:(master) git reset --soft HEAD~
➜ java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: a.txt
複製代碼
如上能夠看到,文件已經從本地倉庫回退到了暫存區中,達到了撤回已提交文件的目的編輯器
➜ java-primary-example git:(master) git reset --mixed HEAD~
➜ java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
nothing added to commit but untracked files present (use "git add" to track)
複製代碼
如上能夠看到,文件已經從本地倉庫回退到了工做區中,也達到了撤回已提交文件的目的ui
➜ java-primary-example git:(master) git reset --hard HEAD~
HEAD is now at f165d1a add ignore file
➜ java-primary-example git:(master) git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
複製代碼
如上能夠看到此模式的回退是最乾淨的回退,連文件都沒有了spa
序號 | 模式 | 效果 |
---|---|---|
1 | --soft | 從名字能夠得知此模式是一種柔軟模式,柔軟到只是從本地倉庫撤回到暫存區 |
2 | --mixed | 從名字能夠得知此模式是一種混合模式,混合的意思就是從本地倉庫回退到工做區 |
3 | --hard | 從名次能夠得知此模式是一種比較硬漢的模式,直接從本地倉庫撤回而且刪除文件 |
以上 3 種模式能夠根據需求進行選擇,若是不當心多提交了文件,想回退從新選擇提交,能夠採用--soft 模式;若是不想提交文件卻不當心執行了提交命令,能夠選擇--mixed 模式;若是錯誤提交而且提交的文件通通都不想要了,那麼就選擇最硬氣的--hard 模式。3d
注意:執行步驟 4 中的操做,新增一個 push 操做,保證文件被推送到遠程服務器版本控制
➜ java-primary-example git:(master) ✗ git log
複製代碼
➜ java-primary-example git:(master) ✗ git revert 9aa858
➜ java-primary-example git:(master) git push
複製代碼
⚠️git revert 以後必定要記得執行 git push 命令日誌