These are some git command cheat sheet that I am mainly writing them down to me, yes I could just keep them in my Notion repository but I find myself sending it and sharing it with a lot of my colleagues so yeah why not post it here too.
Git commands Cheat Sheet
Initialize a new git repository:
git init
Set configuration values for your username and email:
git config --global [user.name](http://user.name/)
git config --global [user.email](http://user.email)
Clone a repository:
git clone
Add a file to the staging area:
git add
Add all files changes to the staging area:
git add .
Check the un-staged changes:
git diff
Reset un staged changes to the last commit:
git restore [. for all or add a file name]
Reset staging area to the last commit:
git reset [. for all or add a file name]
Check the state of the working directory and the staging area:
git status
Remove a file from the index and working directory:
git rm
List the commit history:
git log
Check the metadata and content changes of the commit:
git show
Lists all local branches:
git branch
Create a new branch:
git branch
Rename the current branch:
git branch -m
Delete a branch:
git branch -d
Switch to another branch:
git checkout
Switch to another branch on remote:
git fetch origin
git checkout -b
Merge specified branch into the current branch :
git merge
Create a new connection to a remote repository:
git remote add
Push the committed changes to a remote repository:
git push
Download the content from a remote repository:
git pull
Cleanup unnecessary files and optimize the local repository:
git gc
Temporarily remove uncommitted changes and save them for later use:
git stash
Reapply previously stashed changes:
git stash apply
Delete all merged local branches
Linux shell version
git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d
Poweshell version
git branch --merged | %{$*.trim()} | ?{$* -notmatch 'develop' -and $_ -notmatch 'master'} | %{git branch -d $_}
Revert commits made to the wrong branch
# first reset the commit
git reset --soft HEAD^
# checkout the branch you want to commit to
git checkout branch
# commit
git commit
Rebase Current branch onto develop
# either update your develop local branch by call
git rebase develop
# or
git rebase origin/develop
# Conflicts may occur you should resolve them all and add your changes by running 'git add' command, you should not commit your changes:
git add .
# then continue your rebase, conflicts still can occur fix them and then git add . and then continue
git rebase --continue
# once you are done, you just need to update your remote branch
git push origin HEAD -f
# I urge you to push your rebase to the remote server like that you wont be surprised if you see that your branch need to download some commit from the old pushed commits.
# PS: If you want to see the files with conflicts
git diff --name-only --diff-filter=U
Git Submodules
git clone --recurse-submodules git@git.case-tunisia.com:Bosch/box-control.git
git submodule update --init --recursive
Git Alias
Add an alias via command line:
git config --global alias.co checkout
update your git experience with these aliases:
add them all to your .gitconfig file
...
[alias]
s = status
br = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate
co = checkout
cob = checkout -b
ci = commit
cam = commit -am
done = !git push origin HEAD
last = log -1 HEAD
hist = log --graph --pretty=format:'%C(bold)%h%Creset%C(magenta)%d%Creset %s %C(yellow)<%an> %C(cyan)(%cr)%Creset' --abbrev-commit --date=relative
hists = log --graph --pretty=format:'%C(bold)%h%Creset%C(magenta)%d%Creset %s %C(yellow)<%an> %C(cyan)(%cr)%Creset' --abbrev-commit --date=relative --stat
ls = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cgreen\\[%cn]" --decorate
fp = fetch --all --prune
mnff = merge --no-ff
undo = reset HEAD~1 --mixed
res = !git reset --hard
rh1 = reset HEAD^ --hard
rh2 = reset HEAD^^ --hard
bra = branch -a
up = pull --rebase --autostash
alias = "!git config -l | grep alias | cut -c 7-"
Rebase on Forked project
1️⃣ Add the original repo as upstream
If you haven’t done this yet:
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
You can verify with:
git remote -v
You should see both:
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
2️⃣ Fetch the latest changes from upstream
git fetch upstream
3️⃣ Checkout your feature branch
git checkout your-feature-branch
4️⃣ Rebase on top of upstream/main
git rebase upstream/main
If there are conflicts, Git will pause and let you fix them.
After fixing each conflict:
git add <file> git rebase --continueIf you want to cancel:
git rebase --abort
5️⃣ Push the re-based branch to your fork
Since re-basing rewrites history, you’ll need to force push:
git push origin your-feature-branch --force
💡 Tip:
If you want to make this easier in the future, you can regularly run:
git fetch upstream
git rebase upstream/main
before starting any new work.
so if we have configured the upstream branch already we can easily do:
git fetch upstream && git rebase upstream/main
