簡體   English   中英

如何返回到先前的提交並在此先前的提交中進行更改?

[英]How to go back to a previous commit and make change in this previous commit?

我正在使用git。 從那以后我有一個師父的分支。

$ git log
commit 412455451515dd5d5dldldld454585fgdgjd      // #7
Author: me
Date:   Fri Mar 03 14:27:55 2015 +0100

    feature7A

commit 95299628812ae425c06accc61a0ef4203cc  // #6
Author: me
Date:   Wed Mar 03 11:20:35 2015 +0100

     feature6A

commit 82f1691fcfd6348ddf5462be42bf9983b2f  // #5
Merge: 052c908 6c2c93d
Author: me
Date:   Tue Mar 02 16:36:01 2015 +0100

   feature5A

commit 6c2c93da125a66f7dbb5b14a96feed819f70671d     // #4
Author: me
Date:   Tue Mar 01 14:30:48 2015 +0100

   feature4A

我回到了82f1691fcfd6348ddf5462be42bf9983b2f // //#5

git checkout 82f1691fcfd6348ddf5462be42bf9983b2f 

我更正了此提交中的錯誤。 我想在提交412455451515dd5d5dldldld454585fgdgjd //#7中恢復此更改。 旺旺是最佳做法:

  1. 存儲更改提交#7使用git stash pop應用更改

  2. 合並提交#5和#7。

謝謝

您將要更改記錄的歷史記錄。 僅當您尚未共享修改時,才執行此操作。 謹防!

如果您共享了此內容,請不要驚慌,也不要執行以下操作。 請與負責該回購的人聯系並獲得適當的幫助,該幫助與您項目中的工作流程一致。

// in the following i'm assuming that you were working on master.
git checkout 82f1691f -b recovery
// This checks out the commit you want to modify and creates a branch there that we will delete afterwards.
//Hack hack hack.

git add my_Modified_File.h  //You can replace this 2 lines by using git gui
git commit --amend          //and selecting the menu commit->fix the last commit

git checkout master
git rebase recovery

最干凈,最好,最誠實的是應用當前的變化。 提交#5有一個bug,就這樣吧。 每個開發人員都編寫錯誤,讓歷史記錄顯示它不是問題。 重寫歷史記錄以隱藏它是沒有意義的。

最佳做法是:

$ git stash
$ git checkout #7
$ git stash apply

檢查您的修復程序是否仍然有效

$ git commit -a

在您的提交消息中可能提到了此問題,該消息已修復了提交5中引入的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM