2009-08-29 57 views
1

我做了3'git commit',但我沒有做'git push'。如何在以前的提交中回滾文件中的變化git

1. commit 1 
2. commit 2 
    touches fileA 
    touches fileB 
    touches fileC 
3. commit 3 

那麼,怎樣才能在我文件B做了我

  1. 回滾更改提交2? (我不能再做'git checkout -fileB'了,因爲我已經'git commit'了,我怎樣才能回滾我的修改?
  2. 在fileC中進行更改並使其成爲commit 2的一部分?我想我可以去改變現在的文件,然後運行 ​​'git的變基-i HEAD〜2' 正確

回答

2

使用git rebase -i HEAD~2和編輯的第二次提交

5

這應該工作:?

1. git rebase -i HEAD~2 
2. in your editor, select the following: 

edit 9b86592 commit 2 
pick f3907cb commit 3 

3. at this point roll back the changes you made in fileB, for example with 
    `git checkout <version_you_want>` or by manually editing the file 
4. make the changes in fileC you want to be part of commit 2 
5. `git add fileB fileC` 
6. `git commit --amend` 
7. `git rebase --continue` 

你可能需要解決合併問題,如果有衝突時,git試圖應用提交3.解決這些問題後,再次運行git rebase --continue

0

假設你是在分支主,有一個乾淨的樹:

# checkout incorrect commit 
git checkout <sha1_of_commit2> 

# revert the changes to fileB by checking out the parent version 
git checkout HEAD^ -- fileB 

# make an amended commit 
git commit --amend 

# go back to master 
git checkout master 

# transplant the changes since the bad commit onto the amended commit 
git rebase --onto [email protected]{1} <sha1_of_commit2> 
+0

'rebase -i'更容易,IMO。 – u0b34a0f6ae

+0

@ kaiser.se:有些人覺得更簡單,我個人覺得解釋需要更長的時間,因爲您必須描述'rebase -i'命令以及除了其他重要步驟之外如何編輯交互式rebase提交列表。描述如何「手動」做事更簡單。大部分提示'rebase -i'的答案要麼比我的配方有更多的步驟,要麼更復雜的步驟或更明確描述的更隱含的步驟。 –

0

這是我會怎麼做。

結帳和老版FILEB的承諾它

git checkout HEAD~3 -- fileB 
git commit -m"fix fileB" fileB 

現在變基與舊的修復壁球犯

git rebase -i HEAD~3 

現在進入您最近提交「修復FILEB」是在提交2之後,並將「pick」指令更改爲「squash」,使用提交重置它來加入提交更改fileB。

相關問題