都2020年了還在爲錯誤提交代碼犯愁?

1.前言

在平常的開發過程當中,你不免會將不應提交的代碼提交了,不應push到遠程分支的代碼push到了遠程分支,或者解決代碼衝突時,覆蓋別人代碼並提交。java

這些突發情況在初次遇到的時候都會讓你手足無措,非常慌亂,沒事,看完這篇文章,就能夠完全治癒好你慌亂。git

2.術語介紹

  • 工做區:開發者開發使用的區域
  • 暫存區:將新建的文件進行版本控制後,就添加到暫存區中
  • 本地倉庫:部署在本機存放文件的倉庫
  • 遠程倉庫:部署在遠程存放文件的倉庫

3.操做流程

正常開發流程以下:首先建立咱們須要的文件--->對新建的文件進行版本控制,此時文件添加到暫存區域--->提交暫存區域的文件到本地倉庫--->推送本地倉庫的文件到遠程倉庫web

4.撤回暫存區的文件到工做區

4.1 新建一個名爲 a.txt 的文件

4.2 將 a.txt 文件進行暫存操做

➜  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
複製代碼

4.3 將 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)
複製代碼

5.撤回已經 commit 未 push 的文件

注意:執行步驟 4.1 和 4.2 中的操做服務器

5.1 提交 a.txt 文件

➜  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
複製代碼

5.2 使用--soft 模式進行撤回

➜  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
複製代碼

如上能夠看到,文件已經從本地倉庫回退到了暫存區中,達到了撤回已提交文件的目的編輯器

5.3 使用--mixed 模式進行撤回

➜  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

5.4 使用--hard 模式進行撤回

➜  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

5.5 三種模式比較

序號 模式 效果
1 --soft 從名字能夠得知此模式是一種柔軟模式,柔軟到只是從本地倉庫撤回到暫存區
2 --mixed 從名字能夠得知此模式是一種混合模式,混合的意思就是從本地倉庫回退到工做區
3 --hard 從名次能夠得知此模式是一種比較硬漢的模式,直接從本地倉庫撤回而且刪除文件

以上 3 種模式能夠根據需求進行選擇,若是不當心多提交了文件,想回退從新選擇提交,能夠採用--soft 模式;若是不想提交文件卻不當心執行了提交命令,能夠選擇--mixed 模式;若是錯誤提交而且提交的文件通通都不想要了,那麼就選擇最硬氣的--hard 模式。3d

6.撤回已經 push 的文件

注意:執行步驟 4 中的操做,新增一個 push 操做,保證文件被推送到遠程服務器版本控制

6.1 查看提交日誌信息

➜  java-primary-example git:(master) ✗ git log
複製代碼

6.2 使用 git revert 命令撤回文件

➜  java-primary-example git:(master) ✗ git revert  9aa858
➜  java-primary-example git:(master) git push
複製代碼

⚠️git revert 以後必定要記得執行 git push 命令日誌

相關文章
相關標籤/搜索