Alexey Korepanov
by Alexey Korepanov
~1 min read

Categories

To rollback the last commit:

git reset HEAD~

It will revert your last commit. Your changes will stay locally so you are safe.

Example:

$ git status
On branch b
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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git add test.txt
$ git commit -m "Some changes"
[b af8119f] Some changes
 1 file changed, 2 insertions(+)
$ git status
On branch b
nothing to commit, working tree clean
$ git reset HEAD~
Unstaged changes after reset:
M	test.txt
$ git status
On branch b
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:   test.txt

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

Use git reset HEAD~2 to rollback last 2 commits, git reset HEAD~3 to rollback last 3 commits, etc.