Master Git Quickly: Essential Commands and Tips for Every Developer
This guide explains why version control matters, introduces Git as the leading distributed system, and provides a concise cheat‑sheet of the most important commands—from initializing a repository to branching, merging, and undoing changes—so you can work efficiently and collaborate without losing any history.
Git is currently the most advanced distributed version control system in the world.
What is a version control system? Imagine editing a long document in Microsoft Word, repeatedly saving new copies to track changes, and struggling to locate or merge edits made by collaborators. A version control system automatically records every change, enables collaborative editing, and lets you view any revision instantly.
Below is an example of how file versions might be recorded:
Version 1 – service.doc – Zhang San – Deleted clause 5 – 7/12 10:38
Version 2 – service.doc – Zhang San – Added license limit – 7/12 18:09
Version 3 – service.doc – Li Si – Finance adjusted contract amount – 7/13 9:51
Version 4 – service.doc – Zhang San – Extended free upgrade period – 7/14 15:17
Git commands can be overwhelming, but remembering just six core commands covers daily use; mastering Git may require 60‑100 commands.
1. Initialize a Repository
# Create a new Git repository in the current directory
$ git init
# Create a new directory and initialize it as a Git repository
$ git init [project-name]
# Clone an existing repository with its full history
$ git clone [url]2. Configuration
The Git configuration file is .gitconfig, which can be set globally in the user’s home directory or locally in a project directory.
# Show current Git configuration
$ git config --list
# Edit the 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]"3. 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 files in the current directory
$ git add .
# Interactively add changes
$ git add -p
# Remove a file from the working tree and stage the removal
$ git rm [file1] [file2] ...
# Stop tracking a file but keep it in the working tree
$ 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
$ git commit [file1] [file2] ... -m [message]
# Commit all changes since the last commit
$ git commit -a
# Show diff in the commit output
$ git commit -v
# Amend the previous commit (e.g., change the message)
$ git commit --amend -m [message]
# Amend and include new 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]
# Switch to an existing branch
$ git checkout [branch-name]
# Merge a branch into the current one
$ git merge [branch]
# Delete a local branch
$ git branch -d [branch-name]
# Delete a remote branch
$ git push origin --delete [branch-name]6. Tag Management
# 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] --tags7. Inspect Repository
# Show changed files
$ git status
# Show commit history
$ git log
# Show commit history with stats
$ git log --stat
# Search commit messages
$ git log -S [keyword]
# Show file history (including renames)
$ git log --follow [file]
# Show diff of a file
$ git diff [file]
# Show diff between two commits
$ git diff [first-branch]...[second-branch]
# Show recent commits
$ git log -5 --pretty --oneline
# Show who changed a file and when
$ git blame [file]
# Show recent reflog entries
$ git reflog8. Remote Synchronization
# Fetch changes from a remote
$ git fetch [remote]
# Show remote URLs
$ git remote -v
# Add a new remote
$ git remote add [shortname] [url]
# Pull and merge changes from a remote branch
$ 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 Changes
# Restore a file from the staging area
$ git checkout [file]
# Restore a file from a specific commit
$ git checkout [commit] [file]
# Reset a file to the last commit (keep working tree)
$ git reset [file]
# Reset the entire working tree and index
$ git reset --hard
# Revert a specific commit
$ git revert [commit]
# Stash uncommitted changes
$ git stash
# Apply stashed changes
$ git stash popSource: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
Signed-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.
