Git Cheatsheet
Searchable reference of common git commands by topic.
git init Create a new empty repository in the current directory.
git clone <url> Clone a remote repository into a new directory.
git config --global user.name "Name" Set the name attached to your commits.
git config --global user.email "[email protected]" Set the email attached to your commits.
git status Show changed, staged and untracked files.
git add <file> Stage a file for the next commit (use . for all).
git commit -m "message" Record staged changes with a message.
git commit -am "message" Stage tracked changes and commit in one step.
git branch List local branches.
git branch <name> Create a new branch.
git switch <name> Switch to an existing branch.
git switch -c <name> Create and switch to a new branch.
git merge <branch> Merge the named branch into the current one.
git branch -d <name> Delete a fully merged branch.
git branch -D <name> Force-delete a branch even if unmerged.
git restore <file> Discard unstaged changes in a file.
git restore --staged <file> Unstage a file but keep its changes.
git reset --soft HEAD~1 Undo last commit, keep changes staged.
git reset --hard HEAD~1 Undo last commit and discard its changes.
git revert <commit> Create a new commit that undoes a given commit.
git commit --amend Replace the last commit (edit message or add files).
git clean -fd Remove untracked files and directories.
git remote -v List configured remotes and their URLs.
git remote add origin <url> Add a remote named origin.
git fetch Download objects and refs without merging.
git pull Fetch and integrate changes from the remote.
git push Upload local commits to the remote.
git push -u origin <branch> Push and set the upstream tracking branch.
git push --force-with-lease Force-push safely, failing if remote advanced.
git stash Save uncommitted changes and clean the working tree.
git stash push -m "msg" Stash changes with a descriptive message.
git stash list List all stashed change sets.
git stash pop Apply the latest stash and remove it.
git stash apply Apply a stash but keep it in the list.
git stash drop Delete a single stash entry.
git log Show commit history.
git log --oneline --graph --all Compact graph of all branches.
git log -p <file> Show change history for a file with diffs.
git diff Show unstaged changes.
git diff --staged Show staged changes that will be committed.
git blame <file> Show who last modified each line of a file.
git show <commit> Show metadata and diff for a commit.
git rebase <branch> Reapply current commits on top of another branch.
git rebase -i HEAD~3 Interactively squash/edit/reorder recent commits.
git rebase --continue Continue a rebase after resolving conflicts.
git rebase --abort Cancel an in-progress rebase.
git cherry-pick <commit> Apply a single commit onto the current branch.