consolelog.tools logo

Git Command Cheatsheet

Quick reference guide for common Git commands with examples

Setup & Config

git config --global user.name "[name]"

Set your name for commit transactions

Example:

git config --global user.name "John Doe"
git config --global user.email "[email]"

Set your email for commit transactions

Example:

git config --global user.email "john@example.com"
git config --list

List all configuration settings

git config --global color.ui auto

Enable colorization of command line output

Getting Started

git init

Initialize a new Git repository

git clone [url]

Clone a repository from a remote URL

Example:

git clone https://github.com/user/repo.git

Basic Snapshotting

git status

Show the working tree status

git add [file]

Add file contents to the staging area

Example:

git add index.html
git add .

Add all changes to the staging area

git add -A

Add all changes (new, modified, deleted) to the staging area

git commit -m "[message]"

Commit staged changes with a message

Example:

git commit -m "Add new feature"
git commit -am "[message]"

Add and commit all tracked files with a message

Example:

git commit -am "Update readme"
git commit --amend

Modify the last commit

git reset [file]

Unstage a file while retaining changes

Example:

git reset index.html
git reset --hard

Reset the working directory and staging area to match the most recent commit

git rm [file]

Remove a file from the working directory and staging area

Example:

git rm old-file.txt
git mv [file-original] [file-renamed]

Move or rename a file

Example:

git mv old.txt new.txt

Branching & Merging

git branch

List all local branches

git branch [branch-name]

Create a new branch

Example:

git branch feature/new-feature
git branch -d [branch-name]

Delete a branch

Example:

git branch -d feature/old-feature
git branch -D [branch-name]

Force delete a branch

Example:

git branch -D feature/abandoned
git checkout [branch-name]

Switch to a branch

Example:

git checkout develop
git checkout -b [branch-name]

Create and switch to a new branch

Example:

git checkout -b feature/new-feature
git switch [branch-name]

Switch to a branch (newer alternative to checkout)

Example:

git switch main
git switch -c [branch-name]

Create and switch to a new branch

Example:

git switch -c feature/new-feature
git merge [branch]

Merge a branch into the current branch

Example:

git merge feature/new-feature
git merge --no-ff [branch]

Merge a branch with a merge commit

Example:

git merge --no-ff feature/new-feature

Sharing & Updating

git remote add [name] [url]

Add a remote repository

Example:

git remote add origin https://github.com/user/repo.git
git remote -v

List all remote repositories

git fetch [remote]

Download objects and refs from a remote repository

Example:

git fetch origin
git pull

Fetch from and integrate with another repository

git pull --rebase

Fetch and rebase the current branch

git push

Update remote refs along with associated objects

git push [remote] [branch]

Push a branch to a remote repository

Example:

git push origin main
git push -u [remote] [branch]

Push and set upstream branch

Example:

git push -u origin main
git push --tags

Push all tags to remote repository

Inspection & Comparison

git log

Show commit logs

git log --oneline

Show commit logs in condensed format

git log --graph

Show commit logs with ASCII graph

git log --all --decorate --oneline --graph

Show visual commit history (dog = decorations, oneline, graph)

git diff

Show changes between commits, commit and working tree, etc.

git diff --staged

Show changes staged for commit

git diff [branch1] [branch2]

Show differences between two branches

Example:

git diff main develop
git show [commit]

Show various types of objects

Example:

git show HEAD

Stashing

git stash

Stash the changes in a dirty working directory

git stash save "[message]"

Stash changes with a message

Example:

git stash save "Work in progress"
git stash list

List all stashes

git stash pop

Apply and remove the most recent stash

git stash apply

Apply the most recent stash without removing it

git stash drop

Remove the most recent stash

git stash clear

Remove all stashes

Tagging

git tag

List all tags

git tag [tag-name]

Create a lightweight tag

Example:

git tag v1.0.0
git tag -a [tag-name] -m "[message]"

Create an annotated tag

Example:

git tag -a v1.0.0 -m "Release version 1.0.0"
git tag -d [tag-name]

Delete a tag

Example:

git tag -d v0.9.0
git push [remote] [tag-name]

Push a tag to remote

Example:

git push origin v1.0.0

Undoing Changes

git checkout -- [file]

Discard changes in working directory

Example:

git checkout -- index.html
git restore [file]

Restore working tree files (newer alternative)

Example:

git restore index.html
git restore --staged [file]

Unstage a file

Example:

git restore --staged index.html
git revert [commit]

Create a new commit that undoes changes from a previous commit

Example:

git revert abc123
git reset [commit]

Reset current HEAD to the specified state

Example:

git reset HEAD~1
git reset --soft [commit]

Reset HEAD but keep changes staged

Example:

git reset --soft HEAD~1
git reset --hard [commit]

Reset HEAD and discard all changes

Example:

git reset --hard HEAD~1

Advanced

git rebase [branch]

Reapply commits on top of another base tip

Example:

git rebase main
git rebase -i [commit]

Interactive rebase

Example:

git rebase -i HEAD~3
git cherry-pick [commit]

Apply the changes from a specific commit

Example:

git cherry-pick abc123
git reflog

Show reference logs (useful for recovering lost commits)

git bisect start

Start binary search to find the commit that introduced a bug

git clean -fd

Remove untracked files and directories

Git Tips

  • Use git status frequently to check your working directory state
  • Always review changes with git diff before committing
  • Write clear, descriptive commit messages that explain the "why" not just the "what"
  • Use branches to isolate features and bug fixes
  • Pull before you push to avoid merge conflicts
  • Use git reflog to recover "lost" commits

Was this tool helpful?

Share Your Experience

Help others discover this tool!

Frequently Asked Questions - Git Command Cheatsheet