Fundamentals 12 min read

Master Essential Git Commands: A Complete Cheat‑Sheet for Daily Use

This article provides a comprehensive list of the most commonly used Git commands, organized by tasks such as repository creation, configuration, file handling, committing, branching, tagging, inspection, remote synchronization, and undoing changes, helping developers work efficiently with version control.

21CTO
21CTO
21CTO
Master Essential Git Commands: A Complete Cheat‑Sheet for Daily Use

I use Git daily but often forget many commands.

Generally, remembering six core commands covers everyday use, while proficient usage may require 60‑100 commands.

Git commands illustration
Git commands illustration

Below is a curated list of common Git commands with translations for key terms.

·Workspace: 工作区 ·Index / Stage: 暂存区 ·Repository: 仓库区(或本地仓库) ·Remote: 远程仓库

1. Create Repository

# In the current directory, create a new Git repository</code>
<code>$ git init</code>
<code># Create a new directory and initialize it as a Git repository</code>
<code>$ git init [project-name]</code>
<code># Clone a project with its full history</code>
<code>$ git clone [url]

2. Configuration

Git settings are stored in .gitconfig, which can be global (user home) or local (project directory).

# Show current Git configuration</code>
<code>$ git config --list</code>
<code># Edit Git configuration file</code>
<code>$ git config -e [--global]</code>
<code># Set user name for commits</code>
<code>$ git config [--global] user.name "[name]"</code>
<code># Set user email for commits</code>
<code>$ git config [--global] user.email "[email address]"

3. Add / Delete Files

# Add specific files to the staging area</code>
<code>$ git add [file1] [file2] ...</code>
<code># Add a directory (including subdirectories) to the staging area</code>
<code>$ git add [dir]</code>
<code># Add all files in the current directory to the staging area</code>
<code>$ git add .</code>
<code># Delete files from the working directory and stage the deletion</code>
<code>$ git rm [file1] [file2] ...</code>
<code># Stop tracking a file but keep it in the working directory</code>
<code>$ git rm --cached [file]</code>
<code># Rename a file and stage the rename</code>
<code>$ git mv [file-original] [file-renamed]

4. Commit Changes

# Commit staged changes with a message</code>
<code>$ git commit -m [message]</code>
<code># Commit specific staged files with a message</code>
<code>$ git commit [file1] [file2] ... -m [message]</code>
<code># Commit all changes in the working directory directly</code>
<code>$ git commit -a</code>
<code># Show diff information with the commit</code>
<code>$ git commit -v</code>
<code># Amend the previous commit (useful when no new changes)</code>
<code>$ git commit --amend -m [message]</code>
<code># Amend the previous commit and include new changes from specified files</code>
<code>$ git commit --amend [file1] [file2] ...

5. Branch Management

# List all local branches</code>
<code>$ git branch</code>
<code># List all remote branches</code>
<code>$ git branch -r</code>
<code># List both local and remote branches</code>
<code>$ git branch -a</code>
<code># Create a new branch (stay on current branch)</code>
<code>$ git branch [branch-name]</code>
<code># Create and switch to a new branch</code>
<code>$ git checkout -b [branch]</code>
<code># Create a branch pointing to a specific commit</code>
<code>$ git branch [branch] [commit]</code>
<code># Create a branch that tracks a remote branch</code>
<code>$ git branch --track [branch] [remote-branch]</code>
<code># Switch to a branch and update the working directory</code>
<code>$ git checkout [branch-name]</code>
<code># Set upstream tracking for an existing branch</code>
<code>$ git branch --set-upstream [branch] [remote-branch]</code>
<code># Merge a branch into the current branch</code>
<code>$ git merge [branch]</code>
<code># Cherry‑pick a specific commit</code>
<code>$ git cherry-pick [commit]</code>
<code># Delete a local branch</code>
<code>$ git branch -d [branch-name]</code>
<code># Delete a remote branch</code>
<code>$ git push origin --delete [branch-name]</code>
<code># Delete a remote branch using shorthand</code>
<code>$ git branch -dr [remote/branch]

6. Tag Operations

# List all tags</code>
<code>$ git tag</code>
<code># Create a tag on the current commit</code>
<code>$ git tag [tag]</code>
<code># Create a tag on a specific commit</code>
<code>$ git tag [tag] [commit]</code>
<code># Show tag information</code>
<code>$ git show [tag]</code>
<code># Push a specific tag</code>
<code>$ git push [remote] [tag]</code>
<code># Push all tags</code>
<code>$ git push [remote] --tags</code>
<code># Create a branch from a tag</code>
<code>$ git checkout -b [branch] [tag]

7. Inspect Information

# Show changed files</code>
<code>$ git status</code>
<code># Show commit history of the current branch</code>
<code>$ git log</code>
<code># Show commit history with file change stats</code>
<code>$ git log --stat</code>
<code># Show file history, including renames</code>
<code>$ git log --follow [file]</code>
<code>$ git whatchanged [file]</code>
<code># Show diffs for a specific file</code>
<code>$ git log -p [file]</code>
<code># Show who modified a file and when</code>
<code>$ git blame [file]</code>
<code># Show diff between staging area and working directory</code>
<code>$ git diff</code>
<code># Show diff between staging area and last commit</code>
<code>$ git diff --cached [file]</code>
<code># Show diff between working directory and latest commit</code>
<code>$ git diff HEAD</code>
<code># Show diff between two commits/branches</code>
<code>$ git diff [first-branch]...[second-branch]</code>
<code># Show metadata and changes of a commit</code>
<code>$ git show [commit]</code>
<code># Show only changed file names of a commit</code>
<code>$ git show --name-only [commit]</code>
<code># Show file content at a specific commit</code>
<code>$ git show [commit]:[filename]</code>
<code># Show recent commits on the current branch</code>
<code>$ git reflog

8. Remote Synchronization

# Fetch all changes from a remote repository</code>
<code>$ git fetch [remote]</code>
<code># List all remote repositories</code>
<code>$ git remote -v</code>
<code># Show information about a specific remote</code>
<code>$ git remote show [remote]</code>
<code># Add a new remote repository with a name</code>
<code>$ git remote add [shortname] [url]</code>
<code># Pull changes from a remote branch and merge</code>
<code>$ git pull [remote] [branch]</code>
<code># Push a local branch to a remote repository</code>
<code>$ git push [remote] [branch]</code>
<code># Force push the current branch, overwriting conflicts</code>
<code>$ git push [remote] --force</code>
<code># Push all branches to the remote</code>
<code>$ git push [remote] --all

9. Undo Changes

# Restore a specific file from the staging area to the working directory</code>
<code>$ git checkout [file]</code>
<code># Restore a file from a specific commit</code>
<code>$ git checkout [commit] [file]</code>
<code># Restore all files from the previous commit</code>
<code>$ git checkout .</code>
<code># Reset a specific file in the staging area to match the last commit (working directory unchanged)</code>
<code>$ git reset [file]</code>
<code># Reset staging area and working directory to last commit</code>
<code>$ git reset --hard</code>
<code># Reset current branch pointer to a specific commit (staging area reset, working directory unchanged)</code>
<code>$ git reset [commit]</code>
<code># Reset current branch HEAD to a specific commit, resetting both staging area and working directory</code>
<code>$ git reset --hard [commit]</code>
<code># Reset HEAD to a commit while keeping staging area and working directory</code>
<code>$ git reset --keep [commit]</code>
<code># Create a new commit that reverts a specific commit</code>
<code>$ git revert [commit]

10. Other

# Create an archive (e.g., zip) for publishing</code>
<code>$ git archive
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.

software developmentTutorial
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

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.