Master the Essential Git Commands: A Complete Cheat Sheet
This guide presents a comprehensive, organized list of the most commonly used Git commands—including repository initialization, configuration, file staging, committing, branching, tagging, inspection, remote synchronization, and undo operations—complete with concise examples to help developers work efficiently with version control.
Common Git Commands List
Generally, remembering six commands is enough for daily use, but mastering Git may require 60‑100 commands.
Name Explanations
Workspace: working directory Index / Stage: staging area Repository: local repository Remote: remote repository
1. Initialize 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 for commits
$ git config [--global] user.name "[name]"
# Set user email for commits
$ 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.proxy3. Add / Remove Files
# Add specific files to the staging area
$ git add [file1] [file2] ...
# Add an entire directory (including sub‑directories)
$ git add [dir]
# Add all changes in the current directory
$ git add .
# Interactive add (review each change before staging)
$ git add -p
# Remove files from both working directory and staging area
$ 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 from the staging area
$ git commit [file1] [file2] ... -m [message]
# Commit all changes in the working directory (skip staging)
$ git commit -a
# Show diff while committing
$ git commit -v
# Add and commit in one step
$ git commit -am 'message'
# Amend the previous commit (e.g., to edit the message)
$ git commit --amend -m [message]
# Amend previous commit and include additional file changes
$ 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 pointing to a specific commit
$ git branch [branch] [commit]
# Create a branch that tracks a remote branch
$ git branch --track [branch] [remote-branch]
# Switch to an existing branch
$ git checkout [branch-name]
# Switch to the previous branch
$ git checkout -
# Set upstream tracking for a branch
$ git branch --set-upstream [branch] [remote-branch]
# 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]
$ git branch -dr [remote/branch]
# Checkout a tag or specific version
$ git checkout v2.0
# Checkout a file from a specific commit
$ git checkout -- README6. Tag Operations
# 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/[tagName]
# Show tag details
$ 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. Inspect Repository
# Show changed files
$ git status
# Show commit history
$ git log
# Show log with file statistics
$ git log --stat
# Search commit history by keyword
$ git log -S [keyword]
# Show concise log for a range
$ git log [tag] HEAD --pretty=format:%s
# Show commits affecting a file (including renames)
$ git log --follow [file]
$ git whatchanged [file]
# Show diffs for a file
$ git log -p [file]
# Show last 5 commits in one‑line format
$ git log -5 --oneline
# Show contributors sorted by commit count
$ git shortlog -sn
# Show who changed a file and when
$ git blame [file]
# Show diff between staging area and working directory
$ git diff
# Show diff between staging area and last commit
$ git diff --cached [file]
# Show diff between working directory and HEAD
$ git diff HEAD
# Show diff between two branches
$ git diff [first-branch]...[second-branch]
# Show lines added today
$ git diff --shortstat "@{0 day ago}"
# Show metadata and changes for a specific commit
$ git show [commit]
# Show only changed file names for a commit
$ git show --name-only [commit]
# Show file content at a specific commit
$ git show [commit]:[filename]
# Show recent reflog entries
$ git reflog8. Remote Synchronization
# Fetch all changes from a remote
$ git fetch [remote]
# Show remote URLs
$ git remote -v
# Show information about a remote
$ git remote show [remote]
# Add a new remote
$ git remote add [shortname] [url]
# Pull changes from a remote branch and merge
$ git pull [remote] [branch]
# Push a local branch to a remote
$ git push [remote] [branch]
# Force push (overwrite remote)
$ git push [remote] --force
# Push all branches
$ git push [remote] --all9. Undo Operations
# Restore a file from the staging area to the working directory
$ git checkout [file]
# Restore a file from a specific commit
$ git checkout [commit] [file]
# Restore all files from the staging area
$ git checkout .
# Reset a file in the staging area to match the last commit
$ git reset [file]
# Reset both staging area and working directory to last commit
$ git reset --hard
# Reset current branch to a specific commit (keep working tree)
$ git reset [commit]
# Reset current branch and working tree to a specific commit
$ git reset --hard [commit]
# Keep working tree while moving HEAD
$ git reset --keep [commit]
# Revert a specific commit (create a new commit that undoes it)
$ git revert [commit]
# Stash uncommitted changes
$ git stash
$ git stash pop10. Miscellaneous Commands
# Configure user name and email globally
$ git config --global user.name "xxx"
$ git config --global user.email "[email protected]"
# Show colored status output
$ git config --global color.ui true
# Archive repository into a compressed package
$ git archive
# Other useful commands
$ git gc
$ git fsckSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
