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

CommandDescription
git initInitialize 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 --listList all Git configuration settings.

Basic Workflow

CommandDescription
git statusShow 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 -pInteractively 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 --amendModify the most recent commit (message and/or staged changes).
git diffShow unstaged changes between working directory and the index.
git diff --stagedShow changes staged for the next commit.
git diff HEADShow all changes (staged and unstaged) since the last commit.

Branching

CommandDescription
git branchList 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 -aList 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

CommandDescription
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 --abortAbort 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 --abortAbort an in-progress rebase and restore the original branch.
git rebase --continueContinue a rebase after resolving conflicts.
git cherry-pick <commit>Apply the changes from a specific commit onto the current branch.

Remote Repositories

CommandDescription
git remote -vList all configured remote repositories with their URLs.
git remote add <name> <url>Add a new remote repository.
git fetchDownload objects and refs from a remote without merging.
git fetch --allFetch from all configured remotes.
git pullFetch from remote and merge into the current branch.
git pull --rebaseFetch from remote and rebase the current branch on top.
git pushPush 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-leaseForce push, but only if the remote has not been updated by others.
git push origin --delete <branch>Delete a remote branch.

Stashing

CommandDescription
git stashTemporarily save uncommitted changes and revert to a clean working directory.
git stash push -m "message"Stash changes with a descriptive message.
git stash listList all stashed changesets.
git stash popApply the most recent stash and remove it from the stash list.
git stash applyApply the most recent stash but keep it in the stash list.
git stash dropRemove the most recent stash entry.
git stash clearRemove all stashed entries.

History & Inspection

CommandDescription
git logShow the commit history for the current branch.
git log --onelineShow commit history in a condensed one-line format.
git log --graph --onelineShow 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 reflogShow a log of all reference updates (useful for recovering lost commits).

Undoing Changes

CommandDescription
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~1Undo the last commit, keeping changes staged.
git reset --soft HEAD~1Undo the last commit, keeping changes staged.
git reset --hard HEAD~1Undo 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 -fdRemove untracked files and directories. Destructive.

Tags

CommandDescription
git tagList all tags.
git tag <name>Create a lightweight tag at the current commit.
git tag -a <name> -m "msg"Create an annotated tag with a message.
git push origin <tag>Push a specific tag to the remote.
git push origin --tagsPush all local tags to the remote.
git tag -d <name>Delete a local tag.

Common Workflows

Feature Branch Workflow

  1. git checkout -b feature/name
  2. Make changes and commit
  3. git push -u origin feature/name
  4. Open a pull request
  5. git checkout main && git pull

Fix a Mistake

  1. git log --oneline to find the commit
  2. git revert <commit> to undo safely
  3. Or git reset --soft HEAD~1 to uncommit
  4. Fix the issue and commit again

Sync with Upstream

  1. git remote add upstream <url>
  2. git fetch upstream
  3. git rebase upstream/main
  4. git push --force-with-lease

Resolve Merge Conflicts

  1. git merge feature-branch
  2. Edit conflicted files (look for <<<<< markers)
  3. git add <resolved-files>
  4. git commit

Related Resources