2017-03-17 78 views
2

如何修改使用GIT的第二個推送提交,我需要從提到的提交中刪除一些文件,並且我想保留上次提交。GIT:修改第二個推送提交的內容

我做了什麼至今:

git reset --soft HEAD^ 
git reset --soft HEAD^ # two times 

git reset HEAD .idea/ # and the same for other directories 

git add -A 
git commit --amend 
+0

的http://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits –

+0

可能重複的可能的複製[如何修改現有的,unpushed提交?](HTTP:/ /stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits) –

回答

2

你需要做的是rebase你要修改的承諾。

步驟:

  1. 得到承諾的要修改提交ID

    git log -2 // here '2' => will display last 2 commits 
    // lets say the commit you wish to change has ID "ededeac" 
    
  2. 做一個互動再次基於

    git rebase --interactive ededeac // where "ededeac" is the commit to modify 
    

    編輯器會出現,因爲有你給了一個所有提交的清單。 將pick更改爲edit用於需要修改的提交。

    pick 8c27d78 fixed some bug 
    edit ededeac fixed another bug // here, we changed pick to edit 
    

    保存並退出。 Git會重播列出的提交。

    對於您想要編輯的每個提交,Git會將您放入shell。然後你可以用你喜歡的任何方式改變提交。

    // delete/update files 
    git commit --all --amend //here you can change the commit message too 
    // The new changes are added on to the old commit 
    // You can verify that with 'git log' and 'git diff HEAD^' 
    git rebase --continue 
    
  3. 力推原點。

    git push origin --force-with-lease 
    

    需要強制推到原點,因爲你是改寫歷史

+0

在第二步中: 'git rebase --interactive「散列2提交」' 在編輯器中我只有一個提交(這對應於第一次提交,第二次不存在) '選擇8c27d78 blabla' –

+0

@JaouharMbarek這是因爲你可能選擇了一個不正確的提交ID。嘗試'git log -10'來查看你最近的10次提交,然後再次用所需的提交ID進行重試。 –

+0

謝謝,你的建議很有幫助。 –