Useful Git Commands
This page provides a reference guide for commonly used Git commands that will help you manage your repositories effectively.
| Command | Explanation |
|---|---|
git status | Shows the status of the current working directory. It shows what tracked/untracked changes you have made and whether your current local branch is up to date with the remote branch |
git checkout <branch name> | Checkout an already created branch so you can make any changes |
git checkout -b <branch name> | Create a new branch based on the branch you’re already on |
git add . | Adds everything in the current directory to the staging area |
git commit -m "description of changes" | Commits files that have been staged (i.e. added using git add). It basically takes a snapshot of the currently staged changes. If you don’t use the -m option, git will open your default text editor for you to write a commit message. |
git push -u origin <branch name> | Pushes the specified local branch up to the remote repository. Once pushed, the branch can be accessed by anyone who has access to that repository |
git merge <branch name> -m "description of changes" | Merges the specified branch name into the branch you’ve currently got checked out |
git branch -d <branch name> | Deletes the specified branch. You can’t delete the branch you’ve currently got checked out so you’ll have to switch to another branch first before deleting. -d will only work if the branch has already been pushed and merged with the remote branch. Otherwise you’ll have to use -D to forcefully delete. |
git branch \| grep "<criteria>" \| xargs git branch -d | Deletes any branch that meets the specified criteria. For example git branch \| grep "defect*" \| xargs git branch -d would delete any branch that contains “defect” followed by any numbers of characters. This is useful for deleting multiple branches in one go. |
git branch \| grep -v "main" \| xargs git branch -d | Deletes ALL local branches apart from the main branch. Useful if you’ve not been doing your housekeeping and deleting each branch after pushing to the remote repo. |
git clean -f -n | Removes any untracked files from the current directory. -n performs a dry run so you can view the files which would be removed should you perform the command. -f forcefully deletes the files. You can also use -d to remove untracked directories. |
git reset | Unstage everything but retain any changes made to the files. This is useful if you want to undo a git add. |
git reset --soft HEAD~1 | Undo the last commit whilst keeping the changes you made to the files. –soft ensures any changes you have made are preserved. HEAD~1 will undo the last commit, HEAD~2 will undo the last 2 commits and so on. |
git checkout -- . | Undo changes to all untracked files in the current working directory. To undo changes to a specific file you can use <filename> instead of “.” |
git log | View the commit history. You can use -n1 at the end to show the last commit or -n5 to show the last 5 commits etc. |
git ls-files -z -d \| xargs -0 git checkout -- | Recover all unstaged deletions at once without having to specific individual paths |
git status \| grep 'deleted:' \| awk '{print $2}' \| xargs git checkout -- | Recover all staged deletions at once without having to specific individual paths |
git diff <file one> <file two> | Compare <file one> to <file two>. Additions will be shown in green with a “+” and deletions will be shown in red with a “-“. Can use the –color-words option to show difference between just single lines. |
git commit --amend -m "description of changes" | Amends the most recent commit. |
git revert <commit SHA value> | Reverts the specified commit and removes those changes from the current working directory. e.g. “git revert 6794c13e5203” would revert any changes made in that commit. |
git switch <name-of-remote-branch> | Switches to a remote branch. There’s no need to add “remote” or “origin” before the branch name. |
Page last modified: 30 Jan 2026.