Git Commands Cheatsheet
A quick reference of essential Git commands for everyday development. From initial setup to advanced workflows, find the command you need with clear explanations.
Setup & Configuration
| Command | Description |
|---|---|
| git init | Initialize a new Git repository in the current directory. |
| git clone <url> | Clone a remote repository to your local machine. |
| git config --global user.name "Name" | Set the name attached to your commits globally. |
| git config --global user.email "email" | Set the email attached to your commits globally. |
| git config --list | List all Git configuration settings. |
Basic Workflow
| Command | Description |
|---|---|
| git status | Show the working tree status: modified, staged, and untracked files. |
| git add <file> | Stage a specific file for the next commit. |
| git add . | Stage all changes in the current directory and subdirectories. |
| git add -p | Interactively stage hunks of changes, letting you review each one. |
| git commit -m "message" | Create a new commit with staged changes and the given message. |
| git commit --amend | Modify the most recent commit (message and/or staged changes). |
| git diff | Show unstaged changes between working directory and the index. |
| git diff --staged | Show changes staged for the next commit. |
| git diff HEAD | Show all changes (staged and unstaged) since the last commit. |
Branching
| Command | Description |
|---|---|
| git branch | List all local branches. The current branch is marked with an asterisk. |
| git branch <name> | Create a new branch at the current commit. |
| git branch -d <name> | Delete a branch that has been fully merged. |
| git branch -D <name> | Force delete a branch, even if it has not been merged. |
| git branch -a | List all branches, including remote-tracking branches. |
| git checkout <branch> | Switch to an existing branch. |
| git checkout -b <branch> | Create a new branch and switch to it immediately. |
| git switch <branch> | Switch to an existing branch (modern alternative to checkout). |
| git switch -c <branch> | Create and switch to a new branch (modern alternative). |
Merging & Rebasing
| Command | Description |
|---|---|
| git merge <branch> | Merge the specified branch into the current branch. |
| git merge --no-ff <branch> | Merge with a merge commit even if fast-forward is possible. |
| git merge --abort | Abort a merge that has conflicts and restore the previous state. |
| git rebase <branch> | Reapply commits from the current branch on top of the specified branch. |
| git rebase --abort | Abort an in-progress rebase and restore the original branch. |
| git rebase --continue | Continue a rebase after resolving conflicts. |
| git cherry-pick <commit> | Apply the changes from a specific commit onto the current branch. |
Remote Repositories
| Command | Description |
|---|---|
| git remote -v | List all configured remote repositories with their URLs. |
| git remote add <name> <url> | Add a new remote repository. |
| git fetch | Download objects and refs from a remote without merging. |
| git fetch --all | Fetch from all configured remotes. |
| git pull | Fetch from remote and merge into the current branch. |
| git pull --rebase | Fetch from remote and rebase the current branch on top. |
| git push | Push committed changes to the remote repository. |
| git push -u origin <branch> | Push a branch and set up tracking for future pushes/pulls. |
| git push --force-with-lease | Force push, but only if the remote has not been updated by others. |
| git push origin --delete <branch> | Delete a remote branch. |
Stashing
| Command | Description |
|---|---|
| git stash | Temporarily save uncommitted changes and revert to a clean working directory. |
| git stash push -m "message" | Stash changes with a descriptive message. |
| git stash list | List all stashed changesets. |
| 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 drop | Remove the most recent stash entry. |
| git stash clear | Remove all stashed entries. |
History & Inspection
| Command | Description |
|---|---|
| git log | Show the commit history for the current branch. |
| git log --oneline | Show commit history in a condensed one-line format. |
| git log --graph --oneline | Show commit history as a text-based graph. |
| git log -p <file> | Show commit history with diffs for a specific file. |
| git log --author="name" | Filter commit history by author. |
| git show <commit> | Show details and diff of a specific commit. |
| git blame <file> | Show who last modified each line of a file and when. |
| git reflog | Show a log of all reference updates (useful for recovering lost commits). |
Undoing Changes
| Command | Description |
|---|---|
| git restore <file> | Discard changes in the working directory for a file. |
| git restore --staged <file> | Unstage a file while keeping changes in the working directory. |
| git reset HEAD~1 | Undo the last commit, keeping changes staged. |
| git reset --soft HEAD~1 | Undo the last commit, keeping changes staged. |
| git reset --hard HEAD~1 | Undo the last commit and discard all changes. Destructive. |
| git revert <commit> | Create a new commit that undoes the changes from a specific commit. |
| git clean -fd | Remove untracked files and directories. Destructive. |
Common Workflows
Feature Branch Workflow
git checkout -b feature/name- Make changes and commit
git push -u origin feature/name- Open a pull request
git checkout main && git pull
Fix a Mistake
git log --onelineto find the commitgit revert <commit>to undo safely- Or
git reset --soft HEAD~1to uncommit - Fix the issue and commit again
Sync with Upstream
git remote add upstream <url>git fetch upstreamgit rebase upstream/maingit push --force-with-lease
Resolve Merge Conflicts
git merge feature-branch- Edit conflicted files (look for <<<<< markers)
git add <resolved-files>git commit