Undoing merge or pull in Git

Undo pull and recover to previous HEAD commit

If you want to undo git pull action which has been executed and go back to the previous state, use :

# Undo pull action
# --hard, not only reset the current branch and staging area, but also working directory
# ORIG_HEAD, the old HEAD, here it is the HEAD before pull action
$ git reset --hard ORIG_HEAD

Undo pull but keep unstaged changes in working directory

Note the command above will discard changes you have made in working directory. If you have not staged them and want to keep, use --merge option instead :

# Undo pull action but keep unstaged changes in working directory
$ git reset --merge ORIG_HEAD  

Discarding changes in working directory in Git

To discard changes in working tree to make it clean, a safer way is to use git stash command. git stash takes the changes in working directory and index away and you can reapply them back later.

# Stash changes in working directory
$ git stash

# Apply the latest stashed changes
$ git stash apply

Read stashing changes for more about how to use git stash.

If you just want to drop the changes directly, you can run git reset command (Be careful to use it for changes can not be recovered):

# Discard changes in working directory and index.
# Be aware that changes can not be recovered.
# --hard, discard changes in index and working tree
$ git reset --hard

If you want to make your working directory totally clean, which means removing those untracked files. You can continue to run git clean to remove untracked files or even including ignored files. See deleting untracked files for how to do that.

Undoing add in Git

If you add a file to staging area and want to undo the action, use below command :

# Undo add action for a file
# --, the following parameter will be parsed as a file path
$ git reset HEAD -- <file-path>

# Examples:

# Undo add action for index.php
$ git reset -- index.php

If you want to undo add action for all files added to staging area :

$ git reset HEAD

If fact, git status tells you how to unstage a file :

$ git add index.php
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:    index.php

Undoing a commit in Git

Here you will learn how to undo a commit (no matter it has been pushed or not have been pushed yet) in a safe and simple way. Changing the last commit gives more choices if you want to change the remote commit history and what you should do in various situations.

Undo a commit that hasn’t been pushed yet

If you made some mistakes in a commit, you can redo it in a new commit using --amend option. Just fix your mistakes, stage (add) them and make a new commit as below:

# Redo the last commit.
# --amend, replace the last commit with a new one.
$ git commit --amend

If above solution does not suit you, reset can be used to just undo the commit (be careful to use reset):

# Discard the last commit. The commit's modifications in working tree are kept.
$ git reset HEAD~

# Discard the last commit. The commit's modifications in staging area are kept.
$ git reset --soft HEAD~

# Discard the last commit. The commit's modifications in working tree are not kept.
$ git reset --hard HEAD~

Undo a commit that has been pushed

If a commit has been pushed to the remote repository, changing the history in the remote repository is not a good idea. You can let Git to make a new commit which reverses the modifications in the last commit.

git revert command works for you :

# Reverse a commit.
$ git revert -m 1 HEAD

Afterwards, push it to the remote repository again.