Here is my personal cheat sheet about GIT commands. It contains an explanation on how to set up an SSH key, GIT sheet and an introduction to GIT hooks.
Thursday, February 10, 2022
Here are my cheat sheet about most used GIT command !
All the commands below are picked from the official GIT documentation here https://git-scm.com/docs
Install git
For Windows : https://git-scm.com/download/win
For Linux : (usually it is installed by default)
apt install git # On Debian distros
Initialize git
First, we need to initialize git with some values
git config --global user.name "<firstname> <lastname>"
git config --global user.email "<email>"
git config --global core.editor "vim" # Or use "nano" if you prefer !
git config --global color.ui auto
Create an SSH key
To use git, you will have to create an SSH key:
# The command to generate an SSH key may change over the time ; please look on this github blog link :
ssh-keygen -t ed25519 -C "your_email@example.com"
See more information here: https://docs.github.com
Create a new GIT project
Into your root project directory
Download an existing project
git clone <projet_url> <path_directory>
git clone <projet_url> --depth=1 # Clone only the last commit
Note: you can clone a project using HTTPS or SSH:
git clone https://github.com/torvalds/linux.git --depth=1
git clone git@github.com:torvalds/linux.git linux --depth=1
Basic git commands (add/commit/logβ¦)
git diff --color-words # Diff line by line
Do modifications
git add . # To add everything
# Undo a file modification
git checkout . # Undo for every file
git commit -m "<my message>"
git commit -am "<my_message>" # Commit modified or deleted files (not added files !)
git log --graph --oneline --decorate
git reset --soft <commit_id> # Remove last commit but keep the modification
git reset --hard HEAD^ # Remove and clean last commit
# "HEAD" represents the last commit, "HEAD^" represents the commit before "HEAD"
# "HEAD^^" represents the commit before "HEAD^", etc...
git revert <commit_id> # Undo this commit
git diff <commit_id> <commit_id>
git diff <commit_id>^ <commit_id> # "^" means "current commit -1"
git diff <commit_id>^^ <commit_id> # "^^" means "current commit -2"
Branches
A GIT branch is an independent copy of a project. Inside a branch, you can do tests before merging in the main
or master
branch.
git checkout -b <my_branch> # Create a new branch and go there
git branch -a # List the branches
git checkout <my_branch> # Change branch
git branch -d <my_branch> # Delete a (local) branch
git checkout - # Go to previous branch
# Merge/Rebase modifications from another branch in the current branch
git rebase <my_branch> --interactive
Push modification online
git remote add origin <git-repository-url>
git push -u origin <my_branch> # For the first time you push a branch
git push -u origin <my_local_branch>:<my_remote_branch>
git push -f # Force push a branch
git fetch # Fetch the git information from the distant repository
git pull # = "git fetch && git merge"
To automatically exclude files from the working directory, you can create a .gitignore
file (in the root directory, for example). The syntax of the file is simple:
Here, all these files will never be committed. Very useful to not commit files who contain passwords or API keys !
Stash the changes
git stash # Put your modifications in a stash
git stash pop # Pop your modification from the stash
Find bugs
git bisect start # Find a commit who broke the application
git bisect good <commit_id>
Aliases
We can define aliases like this :
git config --global alias.ac "commit -am"
Hooks
Hooks are small scripts executed when an event is triggered in GIT.
Hooks can be created in the .git/hooks/
or $HOME/.git/hooks/
folder.
You can specify a custom GIT hook folder in your configuration (in $HOME/.gitconfig
):
hooksPath = /home/ $HOME /git/hooks
The hook list is:
applypatch-msg
pre-commit
prepare-commit-msg
commit-msg
post-commit
post-checkout
pre-receive
update
post-update
If we want to add a commit convention to your GIT project, you can add a commit-msg hook in .git/hooks/commit-msg
like:
regex = "^(revert: )?(feat|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)(\(.+\))?: .{1,50}"
if [[ ! $msg =~ $regex ]]; then
echo "Regex does not match"
Donβt forget to add permissions with this command:
An another use case would be to add emojis in your commit messages when a regex is typed with:
msg = ` echo " $msg " | sed 's/π/π/'`
msg = ` echo " $msg " | sed 's/:revert:/β³/'`
msg = ` echo " $msg " | sed 's/:build:/π¦/'`
msg = ` echo " $msg " | sed 's/:ci:/π€/'`
msg = ` echo " $msg " | sed 's/:docs:/π/'`
msg = ` echo " $msg " | sed 's/:feat:/π/'`
msg = ` echo " $msg " | sed 's/:fix:/π/'`
msg = ` echo " $msg " | sed 's/:perf:/β‘/'`
msg = ` echo " $msg " | sed 's/:refactor:/π§/'`
msg = ` echo " $msg " | sed 's/:style:/π/'`
msg = ` echo " $msg " | sed 's/:test:/β
/'`
Clean GIT
git clean -df # Clean untracked files
git gc # Speed up the repository (it is made up automatically by git)
Cleaning up old remote git branches
Sometimes you may need to clean the branches in your local git repository.
# remotes/origin/HEAD -> origin/main
# remotes/origin/tmp/generateSummary
# remotes/origin/tmp/lastModified
# remotes/origin/tmp/reorg
# remotes/origin/tmp/revealjs
# remotes/origin/tmp/storybook
# remotes/origin/tmp/useTranslation
git branch -r -d origin/<branch_name>
git remote prune origin --dry-run
# you can remove --dry-run once you are sure about this action
git fetch origin --prune --dry-run
# you can remove --dry-run once you are sure about this action
# URL: git@github.com:xandermann/<repo>.git
# * [would prune] origin/tmp/generateSummary
# * [would prune] origin/tmp/revealjs
# * [would prune] origin/tmp/storybook
# * [would prune] origin/tmp/useTranslation