Git examples: Resetting, Undoing, and Reverting Changes

Git examples: Resetting, Undoing, and Reverting Changes

Last updated:
Table of Contents

Git version used: 2

Reset working directory to remote branch

Warning You will lose unsaved/uncommited work in the working directory!

Revert the working directory to look exactly like origin/another-branch

$ git fetch --all
$ git reset --hard origin/another-branch

Reset working directory to last commit

Warning You will lose any unsaved changes!

Revert your working directory to what it was after the previous commit.

$ git reset --hard HEAD~1

Revert working directory to commit

Warning You will lose any unsaved changes!

Make your working directory mirror what your repository was like after commit <commit_hash>

$ git reset --hard <commit_hash>

Restore file from commit

Revert a file to the way it was on a given commit.

Useful for when you've modified or deleted a file by mistake.

Use git checkout <commit_hash> -- path/to/file

Example: Restore file.txt the way it was in commit f6f207:

$ git checkout f6f207 -- file4.txt

Restore file from previous commit

When you mistakenly delete/modify a file and commit it

Revert a file to how it was in the previous commit.

Use git checkout HEAD~1 -- path/to/file

Example: Revert file.txt the way it was in the previous commit:

$ git checkout HEAD~1 -- file.txt

Undo commit

No files in the working directory will be changed.

The repository will be pointing to the previous commit.

$ git reset --soft HEAD~1

Unstage changes

I.e. un-add all added files:

$ git reset --soft HEAD

Add changes to previous commit

Just add new files and then run git commit --amend

Example: add file file1.txt to the previous commit:

$ git add file1.txt
$ git commit --amend

This will open up the previous commit message for editing (if you want to edit it).

Untrack file

Make git forget about a file (but keep it in the working directory)

Use git rm --cached <path-to-file> and then commit

Example:

  • Remove the file from git

    $ git rm --cached myfile.txt
    rm 'myfile.txt'
    
  • Check it is still there (it's now untracked)

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        deleted:    myfile.txt
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        myfile.txt
    
  • Commit changes

    $ git commit -m "Removed myfile.txt from Git, but kept it in directory"
    1 file changed, 5 deletions(-)
    delete mode 100644 myfile.txt
    

(P.S. it'll always show as untracked unless you add it to .gitignore)

Unstage changes in file

To unstage (or un-add) changes in a file, use git reset HEAD <file>:

Example: add text to some file, stage (add) changes, and then unstage those changes:

  • Change and add the file:
 $ echo "bar" >> my-file.txt
 $ git add my-file.txt
 $ git status
 On branch master
 Changes to be committed:
   (use "git reset HEAD <file>..." to unstage)

     modified:   my-file.txt
  • Now unstage:
 $ git reset HEAD file
 Unstaged changes after reset:
 M    my-file.txt
 $ git status
 On branch master
 Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

      modified:   my-file.txt

 no changes added to commit (use "git add" and/or "git commit -a")

Undo git add

This is the same as unstaging changes in a file.

To undo adding a file to a commit (but keep it tracked) use $ git reset HEAD path/to/file.txt.

Change commit message

git commit --amend will open a text editor for you to change the last commit message

$ git commit --amend

Revert file to previous commit

Warning You will lose any unsaved changes!

Reset a single file to the way it was in the previous commit:

$ git checkout HEAD~1 -- path/to/file

Then you can re-add/re-commit it again into the repo's history.