Git Commands
Updated: January 30, 2023A list of useful Git commands for everyday use.
Stash
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:
Save all changes:
List all stashes:
Delete a specific stash:
Delete all stashes:
Apply a stash and delete it:
Apply the most recent stash:
Apply a specific stash:
Branch
It is useful when you want to work on a new feature and test it without affecting the main branch.
List all branches:
Create a new branch and switch to it:
Switch back to the previous local branch you have been working on:
Rename a branch:
Delete a branch:
Add a branch to upstream repository:
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:
Stage a specific file:
Unstage all changes:
Commit your work:
Overrides
Overrides are useful when you want to undo your work.
Amend a commit message:
If you want to keep your work and undo just the last commit:
If you want to override a file in your local repo with the version in the remote repo:
Restore previous work
Restore your work if you accidentally deleted it.
Undo all uncommitted changes:
Restore a deleted file:
Remove last commit by keeping all changes staged:
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:
Rebase
Bring the changes from the "main" branch to the "staging" branch. Then, push the changes to the existing repository:
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:
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: