3

Scenario: I commit, push to the remote server, and then commit something else with --amend. If I try to push again, I'll get an error because I changed history that was already pushed.

  1. Lets say I don't care about the amended changes, how do I undo this so my history looks like the remote history (discard the --amend changes)?
  2. Lets say I do care about the amended changes, how do I turn the amended commit into a stand alone commit so history looks like this:

           commit 1            <-           commit 2
    (already pushed to server) <- (originally from amended commit)
    

I'm trying to avoid having to use a push -f.


This is a very similar question but there is a key difference: In that question, he hasn't pushed commit 1 to a remote repo yet. In my question, I have.

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

1 Answers1

7

I'll assume your remote is called origin, and your branch is called master. Adjust as needed.

Lets say I don't care about the amended changes

Then you can use git reset --hard origin/master. This sets your current branch to exactly what is on the remote, and updates your index and worktree to match.

Lets say I do care about the amended changes

Then you can use git reset --soft origin/master. This sets your current branch to exactly what is on the remote, but does not update your index or worktree to match. You can then use git commit to create a new commit containing your added changes.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • So is the second part kind of like saying, "Check out this branch but don't modify anything on the file system"? That way, you can commit the differences? – Daniel Kaplan Jul 16 '14 at 21:39
  • Looking at your answer, it looks like it's essentially the same answer as the question I linked to. But I didn't understand what the `--soft` was doing until your explanation. Thanks – Daniel Kaplan Jul 16 '14 at 21:42
  • @tieTYT Right, I see now that it is indeed exactly what's in the other question's accepted answer already. If I had noticed that before, I wouldn't have posted this as an answer (but I would have commented so on your question). Regardless, I'm glad it helped. –  Jul 16 '14 at 22:12