Skip to content

Rare & useful git commands

Git commands

  • git restore . - restores the files to the previous commit/ undos all the local changes that haven’t been commited.
  • git restore index.html - restores only that particular file to the recent commit/ undos all the local/uncommited changes for that file.
  • git reset --hard <hash code of the commit> - removes commits and goes back to the commit for that hash code
  • git reset --source <hash code> index.html>removes commits and goes back to the commit for that hash code only for that particular file.
  • git commit --amend -m 'Your message'helps re-write messages
  • git revert <hash code>helps to roll back to a previous commit by creating a new commit for it. Doesn’t removes those commits from the log like git reset does.
  • git reflogthis can be useful to bring back deleted commits/files/changes. Use git reset <hash code of lost commit from reflog> to bring back rolled changes.
  • git reset HEAD~2Helps roll back by 2 commits and unstage all the changes in those 2 removed commits.
  • git reset HEAD~2 --hard
  • git rebase (most useful command)- Reapply commits on top of another base tip. ex. git rebase master sets the branch at the tip of master branch



  1. License under CC BY-NC 4.0
  2. Copyright issue feedback, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try iOS App