Fundamentals 15 min read

Master the Essential Git Commands: A Complete Cheat Sheet

This article provides a comprehensive cheat sheet of the most frequently used Git commands, covering repository initialization, configuration, file staging, committing, branching, tagging, viewing history, remote synchronization, undoing changes, and miscellaneous tips, helping developers work efficiently with version control.

Open Source Linux
Open Source Linux
Open Source Linux
Master the Essential Git Commands: A Complete Cheat Sheet

Common Git Commands Cheat Sheet

Generally, remembering the six commands in the diagram is enough for daily use, but mastering Git may require knowing 60‑100 commands.

Terminology

Git terminology diagram
Git terminology diagram

The following terms are used throughout the list:

Workspace: working directory

Index / Stage: staging area

Repository: repository (local)

Remote: remote repository

1. Initialize a Repository

# In the current directory, create a new Git repository
$ git init

# Create a new directory and initialize it as a Git repository
$ git init [project-name]

# Clone a project with its full history
$ git clone [url]

2. Configuration

Git configuration is stored in .gitconfig, which can be global (in the user home directory) or local (in the project directory).

# Show current Git configuration
$ git config --list

# Edit Git configuration file
$ git config -e [--global]

# Set user name and email for commits
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

# Enable colored output
$ git config --global color.ui true
$ git config --global color.status auto
$ git config --global color.diff auto
$ git config --global color.branch auto
$ git config --global color.interactive auto

# Remove proxy configuration
$ git config --global --unset http.proxy

3. Add / Remove Files

# Add specific files to the staging area
$ git add [file1] [file2] ...

# Add a directory (including subdirectories) to the staging area
$ git add [dir]

# Add all files in the current directory to the staging area
$ git add .

# Interactive add (prompt before each change)
$ git add -p

# Remove files from the working directory and stage the deletion
$ git rm [file1] [file2] ...

# Stop tracking a file but keep it in the working directory
$ git rm --cached [file]

# Rename a file and stage the rename
$ git mv [file-original] [file-renamed]

4. Commit Changes

# Commit staged changes with a message
$ git commit -m [message]

# Commit specific files directly to the repository
$ git commit [file1] [file2] ... -m [message]

# Commit all changes in the working directory (skip staging)
$ git commit -a

# Show diff information while committing
$ git commit -v

# Add and commit in one step
$ git commit -am 'message'

# Amend the previous commit (change message or add changes)
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...

5. Branch Management

# List local branches
$ git branch

# List remote branches
$ git branch -r

# List all branches
$ git branch -a

# Create a new branch (stay on current branch)
$ git branch [branch-name]

# Create and switch to a new branch
$ git checkout -b [branch]

# Create a branch that tracks a remote branch
$ git branch --track [branch] [remote-branch]

# Switch to a branch
$ git checkout [branch-name]

# Switch to the previous branch
$ git checkout -

# Merge a branch into the current branch
$ git merge [branch]

# Cherry-pick a specific commit
$ git cherry-pick [commit]

# Delete a local branch
$ git branch -d [branch-name]

# Delete a remote branch
$ git push origin --delete [branch-name]

6. Tagging

# List all tags
$ git tag

# Create a tag on the current commit
$ git tag [tag]

# Create a tag on a specific commit
$ git tag [tag] [commit]

# Delete a local tag
$ git tag -d [tag]

# Delete a remote tag
$ git push origin :refs/tags/[tag]

# Show tag information
$ git show [tag]

# Push a tag to a remote
$ git push [remote] [tag]

# Push all tags
$ git push [remote] --tags

# Create a branch from a tag
$ git checkout -b [branch] [tag]

7. Inspecting Repository

# Show changed files
$ git status

# Show commit history
$ git log

# Show log with stats
$ git log --stat

# Search commit history by keyword
$ git log -S [keyword]

# Show log for a specific file (including renames)
$ git log --follow [file]

# Show diff for a file
$ git log -p [file]

# Show recent commits
$ git log -5 --oneline

# Show contributors sorted by commit count
$ git shortlog -sn

# Show who modified a file and when
$ git blame [file]

# Show differences between staging area and working directory
$ git diff

# Show differences between staging area and last commit
$ git diff --cached [file]

# Show differences between working directory and HEAD
$ git diff HEAD

# Show differences between two commits
$ git diff [first-branch]...[second-branch]

# Show commit details
$ git show [commit]

# Show recent reflog entries
$ git reflog

8. Remote Synchronization

# Fetch changes from a remote
$ git fetch [remote]

# Show remote repositories
$ git remote -v

# Show information about a remote
$ git remote show [remote]

# Add a new remote
$ git remote add [shortname] [url]

# Pull changes and merge
$ git pull [remote] [branch]

# Push a branch to a remote
$ git push [remote] [branch]

# Force push current branch
$ git push [remote] --force

# Push all branches
$ git push [remote] --all

9. Undo Operations

# Restore a file from the staging area
$ git checkout [file]

# Restore a file from a specific commit
$ git checkout [commit] [file]

# Reset staging area to match last commit (keep working directory)
$ git reset [file]

# Hard reset to last commit (discard all changes)
$ git reset --hard

# Reset to a specific commit (keep working directory)
$ git reset --keep [commit]

# Revert a commit (create a new commit that undoes it)
$ git revert [commit]

# Stash uncommitted changes
$ git stash
$ git stash pop

10. Miscellaneous Commands

# Configure user name and email globally
$ git config --global user.name "xxx"
$ git config --global user.email "[email protected]"

# Show a compressed archive of the repository
$ git archive

References

Common Git Commands List – Ruan Yifeng’s Blog

“You must know Git these days” – Chinese tech article

Git Recipes Wiki – GitHub

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Gitcommand-lineVersion ControlRepositoryCheat Sheet
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.