Useful git commands

  • List all branches: git branch
  • List all remote branches: git branch -aa
  • Create the branch on your local machine and switch to this branch: git checkout -b <branch name>
  • Create a new branch: git branch <branch name>
  • Rename current branch: git branch -m <new branch name>
  • Rename other branch: git branch -m <old branch name> <new branch name>
  • Delete a branch: git branch -d <branch name>
  • List all commits from all branches: git log --all
  • Check the history of a file: git log <filename>
  • Compare two branches by showing which commits from the first branch are missing from the second branch: git log [first branch name]..[second branch name]
  • Show git log based on commit author: git log --author="Author Name"
  • Show git log with date: git log --before="Oct 20"
  • Check the state of the working directory and the staging area: git status
  • Trigger an empty commit to start some jobs : git commit --allow-empty -m "Trigger notification"
  • Pull changes from remote: git pull
  • Add a change in a file to the staging area: git add <file name>
  • Stage everything in the directory: git add .
  • Change the message of the last commit: git commit --amend
  • Undo uncommitted changes to a file: git checkout <filename>
  • Revert all the changes in the current working directory with the checked out branch: git checkout .
  • Revert the previous commit: git revert HEAD
  • Revert the changes from previous 3 commits without making commit: git revert --no-commit HEAD~3.
  • Discard all local changes to all files permanently: git reset --hard
  • Discard all local changes, but save them for later: git stash

Thank you. Share feedback and git commands you find useful in the comments .

5 thoughts on “Useful git commands

Leave a Reply