Git Cheatsheet
A comprehensive Git commands quick reference guide. Find common Git commands organized by scenario including getting started, branching, merging, remote operations, undo, stash, and logs. Click any command to copy it to your clipboard.
git initInitialize a new Git repository in the current directory
Example:
git initgit clone <repository>Clone a remote repository to your local machine
Common Flags:
--depth- Shallow clone with limited history-b <branch>- Clone specific branchExample:
git clone https://github.com/user/repo.gitgit config --global user.name "<name>"Set your global username for commits
Example:
git config --global user.name "John Doe"git config --global user.email "<email>"Set your global email for commits
Example:
git config --global user.email "john@example.com"git add <file>Stage changes for the next commit
Common Flags:
-A- Stage all changes (new, modified, deleted)-p- Interactively select hunks to stage.- Stage all changes in current directoryExample:
git add index.htmlgit commit -m "<message>"Create a commit with the staged changes
Common Flags:
-m- Provide commit message-a- Automatically stage modified files--amend- Modify the last commitExample:
git commit -m "Add new feature"git statusShow the current state of your working directory
Common Flags:
-s- Short format output-b- Show branch info in short formatExample:
git statusgit diffShow unstaged changes between working directory and staging area
Common Flags:
--staged- Show staged changes--stat- Show diffstat summaryExample:
git diffgit rm <file>Remove a file from Git tracking and delete from disk
Common Flags:
-f- Force removal (required if file is modified)--cached- Remove only from Git, keep file on diskExample:
git rm temp.txtgit mv <source> <destination>Rename or move a file
Example:
git mv old-name.txt new-name.txtgit branchList all local branches
Common Flags:
-a- List all branches (local and remote)-d <branch>- Delete a merged branch-D <branch>- Force delete a branch-m <old> <new>- Rename a branchExample:
git branchgit checkout <branch>Switch to a different branch
Common Flags:
-b <branch>- Create and switch to new branchExample:
git checkout maingit switch <branch>Switch to a different branch (modern Git)
Common Flags:
-c <branch>- Create and switch to new branch-- Switch to previous branchExample:
git switch developgit checkout -b <branch>Create a new branch and switch to it
Example:
git checkout -b feature/logingit branch <name>Create a new branch without switching
Example:
git branch hotfix/bug-123git merge <branch>Merge a branch into your current branch
Common Flags:
--no-ff- Create merge commit even for fast-forward--squash- Squash all commits into one--abort- Abort the current mergeExample:
git merge feature/logingit rebase <branch>Reapply commits on top of another branch
Common Flags:
-i- Interactive rebase--onto <new>- Rebase onto a different branch--abort- Abort the current rebase--continue- Continue after resolving conflictsExample:
git rebase maingit mergetoolOpen merge conflict resolution tool
Example:
git mergetoolgit remote -vList all configured remote repositories
Example:
git remote -vgit remote add <name> <url>Add a new remote repository
Example:
git remote add origin https://github.com/user/repo.gitgit fetch <remote>Download objects and refs from a remote
Common Flags:
--all- Fetch all remotes--prune- Remove stale remote-tracking branchesExample:
git fetch origingit pull <remote> <branch>Fetch and merge changes from a remote branch
Common Flags:
--rebase- Rebase instead of merge--no-commit- Fetch and merge without committingExample:
git pull origin maingit push <remote> <branch>Upload local branch commits to remote repository
Common Flags:
-u- Set upstream for the branch--force- Force push (overwrite remote)--tags- Push all tags--delete- Delete a remote branchExample:
git push -u origin feature/logingit reset <file>Unstage a file while keeping changes
Common Flags:
--soft- Keep changes staged--hard- Discard all changes (dangerous)--mixed- Unstage changes (default)Example:
git reset index.htmlgit checkout -- <file>Discard local changes to a file
Example:
git checkout -- index.htmlgit restore <file>Restore a file to last committed state (modern Git)
Common Flags:
-S- Restore and unstage--staged- Restore from staging areaExample:
git restore index.htmlgit revert <commit>Create a new commit that undoes a previous commit
Example:
git revert abc123git reset --hard <commit>Reset to a specific commit and discard all changes
Example:
git reset --hard abc123git stashTemporarily store uncommitted changes
Common Flags:
push- Push changes to stash stack (default)-u- Include untracked files-a- Include all files (including ignored)Example:
git stashgit stash listList all stashed changes
Example:
git stash listgit stash popApply and remove the latest stash
Example:
git stash popgit stash applyApply changes from stash without removing it
Common Flags:
stash@{n}- Apply specific stash by nameExample:
git stash apply stash@{0}git stash dropRemove a stash without applying it
Example:
git stash drop stash@{0}git stash clearRemove all stashes
Example:
git stash cleargit logShow commit history
Common Flags:
--oneline- Show one line per commit--graph- Show ASCII graph of branch structure-n <num>- Limit number of commits shown--author=<name>- Filter by author-p- Show patches (diff) for each commitExample:
git log --oneline -10git show <commit>Show details of a specific commit
Common Flags:
--stat- Show file change statistics--name-only- Show only changed file namesExample:
git show abc123git blame <file>Show who changed each line of a file
Common Flags:
-L <start>,<end>- Show only specific line rangeExample:
git blame index.htmlgit reflogShow a log of all reference updates
Example:
git refloggit shortlogShow commits grouped by author
Common Flags:
-sn- Show number of commits per author--all- Show all branchesExample:
git shortlog -sn