Skip to content

Git cheatsheet

Configuring Git

Show Git Configuration

git config -l

Set user name

git config --global user.name "Your Name"

Set email

git config --global user.email "user@example.com"

Enable colorized output

git config --global color.ui auto

Initializing or Cloning a Repository

Initializing a Repository

git init

Cloning a Repository

git clone https://github.com/learntheshell/repository.git

Show Remote Repositories

git remote -v

Staging Files

Add specific file

git add filename.txt

Add all changes

git add .

Checking Status

Display status

git status

Status in short format

git status -s

Committing Changes

Commit

git commit -m "commit message"

Commit as other author (not defined in global config)

git commit --author "test <test@example.com>"

Amending Commits

Change commit message in local repository, that has not been pushed yet

git commit --amend

Changing message of already pushed commit

git commit --amend
git push --force-with-lease origin branch-name

Pushing Changes

Push to the main branch

git push origin main

or shorter version

git push

Push specified branch

git push origin branch-name

Viewing Commit Log

Display commit log

git log

Display commit log for a branch

git log branch-name

Viewing a Graph of Commits

git log --oneline --graph --decorate --all

Pulling Updates

Pull from main

git pull origin main

or

git pull

Pull changes from specific branch

git pull origin branch-name

Viewing Differences

Show changes between working directory and staging area

git diff file.txt

Show changes between the staging area and the repository

git diff --staged file.txt

Comparing Branches

git diff main feature-branch

Branching and Merging

Creating a Branch

git branch new-feature

Switching Branches

git checkout new-feature

Creating and Switching to a Branch

git checkout -b new-feature

Merging a Branch

git checkout main
git merge new-feature

Deleting a Branch

git branch -d new-feature

List Branches

git branch

or with -l parameter

git branch -l

list branches with a pattern

git branch -l feature*

Show a List of All Branches

git branch -a

Rebasing

Rebase

git checkout feature-branch
git rebase main

Interactive Rebase

git rebase -i HEAD~3

Stashing Changes

Stash current changes

git stash

Apply the most recent stash

git stash apply

List stashed changes

git stash list

Drop a specific stash

git stash drop stash@{0}

Cherry-picking a Commit

Apply specific commit from another branch

git checkout main
git cherry-pick abc123

Resetting Commits

Discard all local changes

git reset --

Discard Changes in a File

git checkout -- filename.txt

Soft reset (preserves changes)

git reset --soft HEAD~1

Unstaging a File

git reset HEAD filename.txt

Mixed reset (default, unstages changes)

git reset HEAD~1

Hard reset (discards all changes)

git reset --hard HEAD~1

Reverting a Commit (Create a new commit to undo changes)

git revert abc123

Resolving Merge Conflicts

git status                 # View conflicted files
# Manually edit the conflicting file
git add file_with_conflict.txt
git commit

Working with Tags

Creating a Tag

git tag -a v1.0 -m "Version 1.0"

Listing Tags

git tag

Pushing Tags

Push a specific tag

git push origin v1.0

Push all tags

git push --tags

Worktrees

Checkout main branch in ../main-worktree directory

git worktree add ../main-worktree main

Remove a worktree:

git worktree remove ../main-worktree

Sparse Checkout

  1. Clone the repository, without checkout
git clone --no-checkout https://github.com/example/monorepo.git
cd monorepo
  1. Specify the directories or files you want to check out
git sparse-checkout set src/ include/ docs/
  1. Use checkout command, to fetch specified directories
git checkout

Reflog

  1. Use reflog to find the commit
git reflog
  1. This will show you the history of all changes
d5f5e7f HEAD@{0}: reset: moving to HEAD^
a72c0e4 HEAD@{1}: commit: Added new feature
  1. Recover lost commit
git reset --hard a72c0e4

Hooks

pre-commit hook to check for files larger than 1MB and prevent them from going into the commit

#!/bin/sh
# .git/hooks/pre-commit
# Prevent committing files larger than 1MB
max_size=1000000  # 1MB in bytes

echo "Checking for large files..."

for file in $(git diff --cached --name-only); do
  if [ -f "$file" ]; then
    file_size=$(wc -c <"$file")
    if [ "$file_size" -ge "$max_size" ]; then
      echo "$file is too large ($file_size bytes). Maximum allowed size is 1MB."
      exit 1
    fi
  fi
done

commit-msg hook to ensure commit message format (for example length)

#!/bin/sh
# .git/hooks/commit-msg
# Enforce a commit message length of 10 characters or more

min_length=10
commit_message=$(cat "$1")

if [ ${#commit_message} -lt $min_length ]; then
  echo "Error: Commit message is too short."
  exit 1
fi

Remember to make each hook executable

chmod +x .git/hooks/pre-commit

Submodules

Add submodule

git submodule add git://github.com/jquery/jquery.git externals/jquery
git submodule update --init --recursive

Update submodules

git submodule update --remote

Clone repository and pull submodules

git clone project-with-submodules
git submodule init
git config -l
git submodule update --remote

References