GIT cheat sheet

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)

Terminal window
apt install git # On Debian distros

Initialize git

First, we need to initialize git with some values

Terminal window
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:

Terminal window
# 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

Terminal window
git init

Download an existing project

Terminal window
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:

Terminal window
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โ€ฆ)

Terminal window
git status
git diff
git diff --staged
git diff --color-words # Diff line by line

Do modifications

Terminal window
git add <filename>
git add . # To add everything
# Unadd file
git reset <filename>
# Undo a file modification
git checkout <filename>
git checkout . # Undo for every file
git commit
git commit -m "<my message>"
git commit -am "<my_message>" # Commit modified or deleted files (not added files !)
# Modify last commit
git commit --amend
# Check the history
git log
git log --oneline
git log --graph --oneline --decorate
# Remove last commit
git reset --soft <commit_id> # Remove last commit but keep the modification
git 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 diff
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"
# 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.

Terminal window
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 merge <my_branch>
git rebase <my_branch>
# Squash commits
git rebase <my_branch> --interactive

Push modification online

Terminal window
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
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:

.gitignore
/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

Terminal window
git stash # Put your modifications in a stash
git stash pop # Pop your modification from the stash
git stash list
git stash drop
git stash clear

Find bugs

Terminal window
git bisect start # Find a commit who broke the application
git bisect bad
git bisect good <commit_id>

Aliases

We can define aliases like this :

Terminal window
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):

.gitconfig
[core]
hooksPath = /home/$HOME/git/hooks

The hook list is:

  1. applypatch-msg
  2. pre-commit
  3. prepare-commit-msg
  4. commit-msg
  5. post-commit
  6. post-checkout
  7. pre-receive
  8. update
  9. 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:

.git/hooks/commit-msg
#!/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 1
fi
echo "$msg" > $1

Donโ€™t forget to add permissions:

chmod ug+x .git/hooks/*

An another use case would be to add emojis in your commit messages when a regex is typed with:

Terminal window
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

Terminal window
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.

Terminal window
# 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
#

Recommended articles