2017-03-17 82 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

谢谢,你的建议很有帮助。 –