A 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:
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>}
Branch
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
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>
Rebase
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:
https://github.com/:username/:repo-name/branches
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
Contributing to an open-source project
To contribute to an open-source project, follow the steps below:
-
Use the GitHub UI to fork the
original_owner
repo into your account, so that you have a copy of the repository in your account; -
Clone your fork on your local machine:
git clone https://github.com/your_username/repo.git
- Create a new branch (
your_feature
) and make changes:
git checkout -b <your_feature>
- Commit and push the changes to the remote
your_feature
branch of your fork:
git add .
git commit -m "your commit message"
git push origin <your_feature>
-
Create a pull request to the
main
branch of theoriginal_owner/repo
from theyour_feature
branch of theyour_username/repo
; -
This allows the original owner to review the changes made;
-
If they like them, they can merge the changes directly from your fork to their repository.