Files
git-workshop/GIT-CHEATSHEET.md
2026-01-05 13:34:59 +01:00

8.8 KiB

Git Command Cheatsheet

A comprehensive reference for all Git commands covered in this workshop.


Essentials

Repository Initialization

git init

Initialize a new Git repository in the current directory.

Staging & Committing

git status

Show the current state of your working directory and staging area.

git add <file>

Add a file to the staging area (prepare it for commit).

git add .

Add all changed files in the current directory to the staging area.

git commit -m "message"

Create a new commit with the staged changes and a descriptive message.

git commit -am "message"

Stage all modified tracked files and commit in one step (doesn't include new files).

Viewing History

git log

Show the commit history for the current branch.

git log --oneline

Show commit history in compact one-line format.

git log --oneline --graph --all

Show commit history as a graph with all branches.

git log --stat

Show commit history with statistics about which files changed.

git show <commit>

Display detailed information about a specific commit.

git show <commit>:<file>

View the contents of a file from a specific commit.

Comparing Changes

git diff

Show unstaged changes in your working directory.

git diff --staged

Show staged changes (what will be committed).

git diff <commit1> <commit2>

Compare two commits.

git diff <commit1> <commit2> <file>

Compare changes to a specific file between two commits.

Understanding diff output:

  • Lines with + (green) = added
  • Lines with - (red) = removed
  • Lines with (space) = unchanged (context)
  • @@ -1,5 +1,7 @@ = location of changes (old line/count, new line/count)

For a detailed guide on reading diff output, see Module 02 README.md.

Branching

git branch

List all local branches (current branch marked with *).

git branch <branch-name>

Create a new branch.

git branch -d <branch-name>

Delete a branch (only if it's been merged).

git branch -D <branch-name>

Force delete a branch (even if not merged).

git switch <branch-name>

Switch to a different branch.

git switch -c <branch-name>

Create a new branch and switch to it in one command.

git switch -

Switch back to the previous branch.

Merging

git merge <branch-name>

Merge the specified branch into your current branch.

git merge --abort

Abort a merge in progress and return to the pre-merge state.

Merge Conflict Resolution

git status

During a merge conflict, shows which files have conflicts.

# After resolving conflicts manually:
git add <resolved-file>
git commit

Stage resolved files and complete the merge.

Restoring Files

git restore <file>

Discard changes in working directory (restore from last commit).

git restore --staged <file>

Unstage a file (remove from staging area but keep changes).

git restore --source=<commit> <file>

Restore a file to its state in a specific commit.


Advanced

Rebasing

git rebase <branch>

Reapply your commits on top of another branch (creates linear history).

git rebase -i <commit>

Interactive rebase - edit, reorder, squash, or drop commits.

git rebase --continue

Continue rebasing after resolving conflicts.

git rebase --abort

Abort a rebase and return to the original state.

Reset (History Manipulation)

git reset --soft HEAD~<n>

Undo last n commits but keep changes staged.

git reset --mixed HEAD~<n>

Undo last n commits and unstage changes (default mode).

git reset --hard HEAD~<n>

Undo last n commits and discard all changes (DESTRUCTIVE).

git reset --hard <commit>

Reset your branch to a specific commit, discarding all changes.

Revert (Safe Undo)

git revert <commit>

Create a new commit that undoes changes from a specific commit (safe for shared branches).

git revert --no-commit <commit>

Revert changes but don't create the commit yet (allows editing).

git revert --abort

Abort a revert in progress.

Cherry-Pick

git cherry-pick <commit>

Apply the changes from a specific commit to your current branch.

git cherry-pick <commit1> <commit2> <commit3>

Cherry-pick multiple commits.

git cherry-pick --continue

Continue cherry-picking after resolving conflicts.

git cherry-pick --abort

Abort a cherry-pick in progress.

Stash (Temporary Storage)

git stash

Save your uncommitted changes temporarily and revert to a clean working directory.

git stash save "description"

Stash changes with a descriptive message.

git stash list

Show all stashed changes.

git stash pop

Apply the most recent stash and remove it from the stash list.

git stash apply

Apply the most recent stash but keep it in the stash list.

git stash apply stash@{n}

Apply a specific stash by index.

git stash drop

Delete the most recent stash.

git stash drop stash@{n}

Delete a specific stash.

git stash clear

Delete all stashes.

Working with Remotes

git clone <url>

Create a local copy of a remote repository.

git remote

List all remote repositories.

git remote -v

List all remote repositories with their URLs.

git remote add <name> <url>

Add a new remote repository.

git remote remove <name>

Remove a remote repository.

git fetch <remote>

Download changes from remote but don't merge them.

git pull <remote> <branch>

Fetch and merge changes from remote branch to current branch.

git pull

Fetch and merge from the tracked remote branch.

git push <remote> <branch>

Upload your commits to a remote branch.

git push -u <remote> <branch>

Push and set up tracking relationship (use -u first time pushing a branch).

git push

Push to the tracked remote branch.

Worktrees (Multiple Working Directories)

git worktree add <path> <branch>

Create a new working directory linked to your repository for a specific branch.

git worktree add <path> -b <new-branch>

Create a new branch and working directory for it.

git worktree list

Show all worktrees associated with the repository.

git worktree remove <path>

Remove a worktree.

git worktree prune

Clean up stale worktree administrative data.

Bisect (Binary Search for Bugs)

git bisect start

Start a bisect session to find which commit introduced a bug.

git bisect bad

Mark the current commit as bad (contains the bug).

git bisect good <commit>

Mark a commit as good (doesn't contain the bug).

git bisect reset

End the bisect session and return to the original branch.

git bisect run <script>

Automatically test commits using a script to find the bad commit.


Useful Tips

Viewing Changes

  • Use git log --oneline --graph --all to visualize branch structure
  • Use git log -p to show the actual changes (patch) in each commit
  • Use git log --author="name" to filter commits by author
  • Use git log --since="2 weeks ago" to filter commits by date

Undoing Changes

  • Haven't committed yet? Use git restore <file> to discard changes
  • Staged but want to unstage? Use git restore --staged <file>
  • Committed to wrong branch? Use git cherry-pick to copy the commit to the right branch
  • Want to change last commit message? Use git commit --amend
  • Pushed to remote already? Use git revert (not reset) to safely undo

Best Practices

  • Commit often with clear, descriptive messages
  • Pull before you push to avoid conflicts
  • Use branches for new features or experiments
  • Don't rebase or reset commits that have been pushed to shared branches
  • Use git stash when you need to switch contexts quickly
  • Test your changes before committing

Getting Help

git help <command>

Show detailed help for any Git command.

git <command> --help

Alternative way to show help.

git <command> -h

Show brief usage summary.


Module Reference

Each workshop module focuses on specific commands:

  • Module 01: init, add, commit, status
  • Module 02: log, show, diff
  • Module 03: branch, switch
  • Module 04: merge (fast-forward and three-way)
  • Module 05: merge (conflict resolution)
  • Module 06: rebase
  • Module 07: reset (interactive rebase alternative)
  • Module 08: cherry-pick
  • Module 09: reset vs revert
  • Module 10: stash
  • Module 11: clone, remote, push, pull, fetch
  • Module 12: worktree
  • Module 13: bisect