Git: Undoing Changes

Git Documentation

1. Modifying the last commit

1
$ git commit -- amend

1.1 Changing The last Commit

running git commit --amend, your code editor will open up, and let you edit it.

1.2 Add Forgotten Files To Commit

  • edit the file(s)

  • save the file(s)

  • stage the file(s)

  • and run git commit --amend

2. Reverting a commit

1
$ git revert <SHA-of-commit-to-revert>
  • will undo the changes that were made by the provided commit

  • creates a new commit to record the change

3. Resetting Commits

IMPOTANT: It’s recommended to establish $ git branch backup before reset commits.

1
2
3
4
$ git reset <reference-to-commit>

# EX:
$ git reset --hard HEAD~3
  • erase commits with the --hard flag

  • moves committed changes to the staging index with the --soft flag

  • unstages committed changes --mixed flag


  • ^ – indicates the parent commit

  • ~ – indicates the first parent commit

    EX

  • the parent commit
    • HEAD^
    • HEAD~
    • HEAD~1
  • the great-grandparent commit
    • HEAD^^^
    • HEAD~3
  • HEAD^^^2: ^2 : the merged other parent branch commit

4. restore working tree files

1
$ git checkout