Skip to content

Git cheat sheet

Initialization

Init new repo

Terminal window
git init

Configure user

Terminal window
# see current configuration
git config --global --list
# set user name and email
git config --global user.name "Marina Decruyden"
git config --global user.email "Marina.Decruyden@email.com"
# same commands without --global flag to set user name and email for a specific repository
git config --list
git config user.name "Dries Schellenespe"
git config user.email "Dries.Schellenespe@email.com"

Check status

Terminal window
git status

Adding files

Terminal window
# Add specific file
git add file.txt
# Add all files in the current folder (not recommended from the repo root)
git add .
# Add all files from the repo root, regardless of the current folder
git add -A

Commit changes

Terminal window
git commit -m "Commit message"
# Amend last commit without changing the message
git commit --amend --no-edit

History modification

Terminal window
# Undo the last commit (changes history, moves changes back to the working directory)
git reset HEAD~1
# Revert changes from a specific commit with a new commit
git revert COMMIT-SHA

Viewing history

Terminal window
# Detailed history
git log
# Shortened history
git log --oneline

Reset to a clean working tree

Terminal window
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Discard changes in tracked files
git reset --hard
# Discard changes in specific files/folders
git checkout filename.ext
git checkout ./folder
git checkout *.txt

Stashing changes

Terminal window
# Stash changes
git stash
# Stash changes including untracked files
git stash -u
# List stashes
git stash list
# Apply stash and remove it from the list
git stash pop
# Apply stash without removing it from the list
git stash apply

Working with branches

Terminal window
# Create and switch to a new branch
git checkout -b BRANCH-NAME
# Switch to an existing branch
git checkout main
# Merge another branch into the current branch
git merge BRANCH-NAME
# List all branches
git branch --list
# Delete a branch
git branch -D BRANCH-NAME

Working with worktrees

Git worktrees allow you to have multiple working directories from the same repository, each checked out to a different branch.

Terminal window
# List all worktrees
git worktree list
# Create a new worktree for a new branch
git worktree add ../feature-branch new-feature
# Create a new worktree for an existing branch
git worktree add ../hotfix-branch hotfix/critical-bug
# Create a worktree with a specific path and branch name
git worktree add /path/to/worktree branch-name
# Remove a worktree (must be done from outside the worktree)
git worktree remove ../feature-branch
# Remove a worktree forcefully (even if it has uncommitted changes)
git worktree remove --force ../feature-branch
# Prune worktree references for deleted directories
git worktree prune

Worktree workflow example

Terminal window
# From your main repository
git worktree add ../feature-ui feature/new-ui
# Switch to the worktree directory
cd ../feature-ui
# Work on your feature (add, commit, push as normal)
git add .
git commit -m "Add new UI components"
git push -u origin feature/new-ui
# Switch back to main repository
cd ../main-repo
# Clean up when feature is merged
git worktree remove ../feature-ui
git branch -d feature/new-ui

Finding commit that introduced a bug

Use git bisect to find the commit that introduced a bug

Terminal window
# Start bisecting
git bisect start
# mark the current commit as bad
git bisect bad
# mark a specific commit as good
git bisect good COMMIT-SHA
# After marking bad and good commits, git will checkout a commit in the middle of the range
# Test the commit to see if it's good or bad
# Mark it as good or bad
git bisect good/bad
# Repeat the process until the commit that introduced the bug is found
# Finish bisecting and return to the original branch
git bisect reset

Pushing local repo to GitHub

  1. Ensure a local repo is created
  2. Create a repository on GitHub
  3. Create a PAT or SSH key
Terminal window
# Set up the main branch
git checkout main
git remote add origin https://github.com/USER/REPO.git
git branch -M main
git push -u origin main
# Force push (only if no one else is working on the branch)
git push origin -f
  1. Log in with PAT or SSH as prompted.

Remote repositories

Terminal window
# View remote URLs for fetch and push
git remote -v

Pulling changes

Terminal window
# Fetch without merging
git fetch
# Merge remote main into the current branch
git merge origin/main
# Fetch and merge in one step
git pull

Reflog

Terminal window
# Show history including non-commit history (checkout, merge, rebase, etc.)
git reflog

Search for a commit

Terminal window
# search for a commit by hash or message
git log --oneline | grep <commit hash or part of message>
# Check if a commit is included in history up until a specific tag
git log TAG | grep COMMIT-HASH-OR-MESSAGE

Clone a repository

Terminal window
git clone https://github.com/USER/REPO.git

Git configuration and aliases

Terminal window
# Automatically set up a remote branch when pushing
git config --global --add --bool push.autoSetupRemote true
# Aliases
git config --global alias.co "checkout"
git config --global alias.copm '!git checkout main && git pull'

Bash aliases

Open your .bashrc file to add the following aliases:

Terminal window
# Git default branch
alias gdefault="git symbolic-ref refs/remotes/origin/HEAD | cut -d'/' -f4"
# Common Git aliases
alias gas="git add -A && git status"
alias gs="git status"
alias grh="git reset HEAD~"
alias grhh="git reset HEAD --hard"
alias gc="git commit -m"
alias gco="git checkout"
alias gnewbr="gcopm && git checkout -b"
alias gpr="git pull -r && git --no-pager log -15 --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)[%an]%Creset' --abbrev-commit"
alias gpo="git push -u origin HEAD"
alias gac="gas && git commit -m"
alias gca="git commit --amend --no-edit"
alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias gsu="git stash -u"
alias gsp="git stash pop"
alias gbl="git branch --list"

Additional functions:

Terminal window
function gcopm() {
main_branch=`gdefault`
gco $main_branch && gpr
}
function gbs() {
git branch -a | grep "$1"
}
function gstnewbr() {
git stash -u && gnewbr "$1" && gsp
}
function gcpp() {
gas && gc "$1" && gpr && gpo
}
function gcp() {
gas && gc "$1" && gpo
}

Merging strategies

  1. Merge commit: Preserves history with an additional merge commit.
  2. Squash: Combines multiple commits into one for a cleaner history.
  3. Rebase: Applies feature branch commits on top of the target branch, preserving history without creating a merge commit.

GitHub settings to consider

  • Pull Requests

    • Choose a merge strategy
    • Suggest updates for pull requests
    • Enable auto-merge
    • Automatically delete head branches
  • Branch Protection Rules

    • Require pull request reviews before merging
    • Require up-to-date branches before merging
    • Set required status checks (e.g., GitHub Actions) to pass before merging