Git cheat sheet
Initialization
Init new repo
git init
Configure user
# see current configurationgit config --global --list# set user name and emailgit 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 repositorygit config --list
git config user.name "Dries Schellenespe"git config user.email "Dries.Schellenespe@email.com"
Check status
git status
Adding files
# Add specific filegit 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 foldergit add -A
Commit changes
git commit -m "Commit message"
# Amend last commit without changing the messagegit commit --amend --no-edit
History modification
# 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 commitgit revert COMMIT-SHA
Viewing history
# Detailed historygit log
# Shortened historygit log --oneline
Reset to a clean working tree
# Remove untracked filesgit clean -f
# Remove untracked files and directoriesgit clean -fd
# Discard changes in tracked filesgit reset --hard
# Discard changes in specific files/foldersgit checkout filename.extgit checkout ./foldergit checkout *.txt
Stashing changes
# Stash changesgit stash# Stash changes including untracked filesgit stash -u
# List stashesgit stash list
# Apply stash and remove it from the listgit stash pop
# Apply stash without removing it from the listgit stash apply
Working with branches
# Create and switch to a new branchgit checkout -b BRANCH-NAME
# Switch to an existing branchgit checkout main
# Merge another branch into the current branchgit merge BRANCH-NAME
# List all branchesgit branch --list
# Delete a branchgit 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.
# List all worktreesgit worktree list
# Create a new worktree for a new branchgit worktree add ../feature-branch new-feature
# Create a new worktree for an existing branchgit worktree add ../hotfix-branch hotfix/critical-bug
# Create a worktree with a specific path and branch namegit 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 directoriesgit worktree prune
Worktree workflow example
# From your main repositorygit worktree add ../feature-ui feature/new-ui
# Switch to the worktree directorycd ../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 repositorycd ../main-repo
# Clean up when feature is mergedgit worktree remove ../feature-uigit branch -d feature/new-ui
Finding commit that introduced a bug
Use git bisect
to find the commit that introduced a bug
# Start bisectinggit bisect start
# mark the current commit as badgit bisect bad
# mark a specific commit as goodgit 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 badgit bisect good/bad
# Repeat the process until the commit that introduced the bug is found# Finish bisecting and return to the original branchgit bisect reset
Pushing local repo to GitHub
- Ensure a local repo is created
- Create a repository on GitHub
- Create a PAT or SSH key
# Set up the main branchgit checkout maingit remote add origin https://github.com/USER/REPO.gitgit branch -M maingit push -u origin main
# Force push (only if no one else is working on the branch)git push origin -f
- Log in with PAT or SSH as prompted.
Remote repositories
# View remote URLs for fetch and pushgit remote -v
Pulling changes
# Fetch without merginggit fetch
# Merge remote main into the current branchgit merge origin/main
# Fetch and merge in one stepgit pull
Reflog
# Show history including non-commit history (checkout, merge, rebase, etc.)git reflog
Search for a commit
# search for a commit by hash or messagegit log --oneline | grep <commit hash or part of message>
# Check if a commit is included in history up until a specific taggit log TAG | grep COMMIT-HASH-OR-MESSAGE
Clone a repository
git clone https://github.com/USER/REPO.git
Git configuration and aliases
# Automatically set up a remote branch when pushinggit config --global --add --bool push.autoSetupRemote true
# Aliasesgit 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:
# Git default branchalias gdefault="git symbolic-ref refs/remotes/origin/HEAD | cut -d'/' -f4"
# Common Git aliasesalias 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:
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
- Merge commit: Preserves history with an additional merge commit.
- Squash: Combines multiple commits into one for a cleaner history.
- 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