

So you get a new commit that undoes all the changes. This command basically takes the inversed diff of a target commit and tries to apply it. Also, if youve never pushed great-train-idea (the branch you mistakenly committed to), you can use git log to figure out which commit to revert to instead of.

In that case I'd recommend using the git revert command, which will undo the unwanted changes as a separate commit. There are 2 steps you need to do to get out of this trouble: 1. Note: Once you have pushed changes to a remote repo, it is not recommended to revert commits in this way since you'd be erasing history. It will look something like this: $ git reflogĨ135d07, so only use it if you're wanting to revert back a single commit.
git push -f origin
Step 3: Finally, force push this to the origin branch. This StackOverflow answer does an excellent job of explaining it. You may be wondering the significance of the hard qualifier.
This is the default if you run the command from a terminal. git reset -hardI quote the man page: -edit With this option, git revert will let you edit the commit message prior to committing the revert. The first step would be to use reflog to find the commit right before the merge: $ git reflogĮxecuting this on your repo will return a list of recent commits, including their abbreviated hashes, distance from HEAD, and description. By default, git revert prompts you for a commit message and then commits the results. For this short article I'll explain how you can undo a Git merge that hasn't been pushed yet. The solution to this is simpler if you haven't yet pushed the changes to a remote repo, and if you have then you'll likely have to rely on something like git revert instead of the solution proposed below. If you merge a branch in to another, and for whatever reason decide you want to undo the merge, there are some ways to do this with Git.
