Git Commands

Updated: January 30, 2023

A list of useful Git commands for everyday use.


Stash is a way to save your work without committing it. It is useful when you want to change branches or pull the latest changes from a remote repository.

Save a specific file:

git stash push <file>

Save all changes:

git stash

List all stashes:

git stash list

Delete a specific stash:

git stash drop stash@{<number>}

Delete all stashes:

git stash drop

Apply a stash and delete it:

git stash pop

Apply the most recent stash:

git stash apply

Apply a specific stash:

git stash apply stash@{<number>}


It is useful when you want to work on a new feature and test it without affecting the main branch.

List all branches:

git branch

Create a new branch and switch to it:

git checkout -b <name>

Switch back to the previous local branch you have been working on:

git checkout -

Rename a branch:

git branch -m <oldName> <newName>

Delete a branch:

git branch -d <name>

Add a branch to upstream repository:

git push --set-upstream origin <name>

Stage and commit

Stage is a way to prepare your work for a commit. It allows you to commit your work in smaller parts.

Stage all changes:

git add .

Stage a specific file:

git add <file>

Unstage all changes:

git reset .

Commit your work:

git commit -m "your commit message"


Overrides are useful when you want to undo your work.

Amend a commit message:

git commit --amend

If you want to keep your work and undo just the last commit:

git reset HEAD~

If you want to override a file in your local repo with the version in the remote repo:

git reset --hard origin/<current-branch>

Restore previous work

Restore your work if you accidentally deleted it.

Undo all uncommitted changes:

git checkout .

Restore a deleted file:

git checkout HEAD <file>

Remove last commit by keeping all changes staged:

git reset --soft HEAD~1

Squash commits

First, check the number of commits you want to squash. Then, use that number to tell git how many commits to squash into one:

git rev-list HEAD --count
git rebase -i HEAD~<number-of-commits>


Bring the changes from the "main" branch to the "staging" branch. Then, push the changes to the existing repository:

git checkout staging
git rebase main
git push

Sync feature branch with main branch

If you are in the "feature-branch" and need to pull in the latest changes from the remote main branch:

git checkout main
git pull
git checkout feature-branch
git pull --all
git merge main

Rename master branch to main in GitHub

First, make sure to change the name of the branch on GitHub:

Then, change the name of the branch locally:

git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a