GIT cheat sheet
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
git init
Download an existing project
git clone <projet_url>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=1git clone git@github.com:torvalds/linux.git linux --depth=1
Basic git commands (add/commit/logβ¦)
git status
git diffgit diff --stagedgit diff --color-words # Diff line by line
Do modifications
git add <filename>git add . # To add everything
# Unadd filegit reset <filename>
# Undo a file modificationgit checkout <filename>git checkout . # Undo for every file
git commitgit commit -m "<my message>"git commit -am "<my_message>" # Commit modified or deleted files (not added files !)
# Modify last commitgit commit --amend
# Check the historygit loggit log --onelinegit log --graph --oneline --decorate
# Remove last commitgit reset --soft <commit_id> # Remove last commit but keep the modificationgit reset --soft HEAD^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 diffgit 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"# etc...
git diff HEAD^ HEAD
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 theregit branch -a # List the branchesgit checkout <my_branch> # Change branchgit branch -d <my_branch> # Delete a (local) branch
git checkout - # Go to previous branch
# Merge/Rebase modifications from another branch in the current branchgit merge <my_branch>git rebase <my_branch>
# Squash commitsgit 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 branchgit push -u origin <my_local_branch>:<my_remote_branch>git push
git push -f # Force push a branch
git fetch # Fetch the git information from the distant repositorygit 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:
/node_modules//vendor//public//.vscode//resources//.env
*.log*~a.out
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 stashgit stash pop # Pop your modification from the stashgit stash listgit stash dropgit stash clear
Find bugs
git bisect start # Find a commit who broke the applicationgit bisect badgit bisect good <commit_id>
Aliases
We can define aliases like this :
git config --global alias.ac "commit -am"git ac "commit !"
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
):
[core] 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:
#!/bin/bash
msg=`cat $1`
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" echo "$regex" exit 1fi
echo "$msg" > $1
Donβt forget to add permissions with this command:
chmod ug+x .git/hooks/*
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.
# Example:
git branch -a
# * main# tmp/reorg# remotes/origin/HEAD -> origin/main# remotes/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>
# or
git remote prune origin --dry-run# you can remove --dry-run once you are sure about this action
# or
git fetch origin --prune --dry-run# you can remove --dry-run once you are sure about this action
## Pruning origin# 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#
Kubernetes (CKA / CKAD) cheat sheet
Here is my personal cheat sheet about Kubernetes for the CKA and CKAD certification
Practice code with the "Quick Sort" algorithm
Enhance your coding skills by learning how the Quick Sort algorithm works!
How I built and organized my blog with Astro JS
In this article, Iβll share my insights on building and organizing my blog using Astro
Atomic design - Technological watch
Learn what is the atomic design in less than 5 minutesΒ !