# Git Command Cheatsheet A comprehensive reference for all Git commands covered in this workshop. --- ## Essentials ### Repository Initialization ```bash git init ``` Initialize a new Git repository in the current directory. ### Staging & Committing ```bash git status ``` Show the current state of your working directory and staging area. ```bash git add ``` Add a file to the staging area (prepare it for commit). ```bash git add . ``` Add all changed files in the current directory to the staging area. ```bash git commit -m "message" ``` Create a new commit with the staged changes and a descriptive message. ```bash git commit -am "message" ``` Stage all modified tracked files and commit in one step (doesn't include new files). ### Viewing History ```bash git log ``` Show the commit history for the current branch. ```bash git log --oneline ``` Show commit history in compact one-line format. ```bash git log --oneline --graph --all ``` Show commit history as a graph with all branches. ```bash git log --stat ``` Show commit history with statistics about which files changed. ```bash git show ``` Display detailed information about a specific commit. ```bash git show : ``` View the contents of a file from a specific commit. ### Comparing Changes ```bash git diff ``` Show unstaged changes in your working directory. ```bash git diff --staged ``` Show staged changes (what will be committed). ```bash git diff ``` Compare two commits. ```bash git diff ``` 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 ```bash git branch ``` List all local branches (current branch marked with *). ```bash git branch ``` Create a new branch. ```bash git branch -d ``` Delete a branch (only if it's been merged). ```bash git branch -D ``` Force delete a branch (even if not merged). ```bash git switch ``` Switch to a different branch. ```bash git switch -c ``` Create a new branch and switch to it in one command. ```bash git switch - ``` Switch back to the previous branch. ### Merging ```bash git merge ``` Merge the specified branch into your current branch. ```bash git merge --abort ``` Abort a merge in progress and return to the pre-merge state. ### Merge Conflict Resolution ```bash git status ``` During a merge conflict, shows which files have conflicts. ```bash # After resolving conflicts manually: git add git commit ``` Stage resolved files and complete the merge. ### Restoring Files ```bash git restore ``` Discard changes in working directory (restore from last commit). ```bash git restore --staged ``` Unstage a file (remove from staging area but keep changes). ```bash git restore --source= ``` Restore a file to its state in a specific commit. --- ## Advanced ### Rebasing ```bash git rebase ``` Reapply your commits on top of another branch (creates linear history). ```bash git rebase -i ``` Interactive rebase - edit, reorder, squash, or drop commits. ```bash git rebase --continue ``` Continue rebasing after resolving conflicts. ```bash git rebase --abort ``` Abort a rebase and return to the original state. ### Reset (History Manipulation) ```bash git reset --soft HEAD~ ``` Undo last n commits but keep changes staged. ```bash git reset --mixed HEAD~ ``` Undo last n commits and unstage changes (default mode). ```bash git reset --hard HEAD~ ``` Undo last n commits and discard all changes (DESTRUCTIVE). ```bash git reset --hard ``` Reset your branch to a specific commit, discarding all changes. ### Revert (Safe Undo) ```bash git revert ``` Create a new commit that undoes changes from a specific commit (safe for shared branches). ```bash git revert --no-commit ``` Revert changes but don't create the commit yet (allows editing). ```bash git revert --abort ``` Abort a revert in progress. ### Cherry-Pick ```bash git cherry-pick ``` Apply the changes from a specific commit to your current branch. ```bash git cherry-pick ``` Cherry-pick multiple commits. ```bash git cherry-pick --continue ``` Continue cherry-picking after resolving conflicts. ```bash git cherry-pick --abort ``` Abort a cherry-pick in progress. ### Stash (Temporary Storage) ```bash git stash ``` Save your uncommitted changes temporarily and revert to a clean working directory. ```bash git stash save "description" ``` Stash changes with a descriptive message. ```bash git stash list ``` Show all stashed changes. ```bash git stash pop ``` Apply the most recent stash and remove it from the stash list. ```bash git stash apply ``` Apply the most recent stash but keep it in the stash list. ```bash git stash apply stash@{n} ``` Apply a specific stash by index. ```bash git stash drop ``` Delete the most recent stash. ```bash git stash drop stash@{n} ``` Delete a specific stash. ```bash git stash clear ``` Delete all stashes. ### Working with Remotes ```bash git clone ``` Create a local copy of a remote repository. ```bash git remote ``` List all remote repositories. ```bash git remote -v ``` List all remote repositories with their URLs. ```bash git remote add ``` Add a new remote repository. ```bash git remote remove ``` Remove a remote repository. ```bash git fetch ``` Download changes from remote but don't merge them. ```bash git pull ``` Fetch and merge changes from remote branch to current branch. ```bash git pull ``` Fetch and merge from the tracked remote branch. ```bash git push ``` Upload your commits to a remote branch. ```bash git push -u ``` Push and set up tracking relationship (use -u first time pushing a branch). ```bash git push ``` Push to the tracked remote branch. ### Worktrees (Multiple Working Directories) ```bash git worktree add ``` Create a new working directory linked to your repository for a specific branch. ```bash git worktree add -b ``` Create a new branch and working directory for it. ```bash git worktree list ``` Show all worktrees associated with the repository. ```bash git worktree remove ``` Remove a worktree. ```bash git worktree prune ``` Clean up stale worktree administrative data. ### Bisect (Binary Search for Bugs) ```bash git bisect start ``` Start a bisect session to find which commit introduced a bug. ```bash git bisect bad ``` Mark the current commit as bad (contains the bug). ```bash git bisect good ``` Mark a commit as good (doesn't contain the bug). ```bash git bisect reset ``` End the bisect session and return to the original branch. ```bash git bisect run