How do I recover from a commit –amend if I already pushed the original commit?

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:

  3. How do I make AWS CodeDeploy to use AWS CodeCommit as a Git repo?
  4. Gitkraken with SSH to Gitlab
  5. Why various git commands are available only locally?
  6. rebase reverted merged branch
  7. Filter git diff by type of change
  8. Git 1.6.4 beta on Windows (msysgit) - Unix or DOS line termination
  9.        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.

  • How do you manage large git repositories?
  • Cloning a private Github repo
  • Cannot get git extensions to push something to github SSH problems
  • Can a git-controlled file be aware of which branch it belongs to?
  • Ansible and Git Permission denied (publickey) at Git Clone
  • git - changed file summary like svn diff --summarize/svn status from console (no gitk)
  • One Solution collect form web for “How do I recover from a commit –amend if I already pushed the original commit?”

    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.

    Git Baby is a git and github fan, let's start git clone.