Master Git: Essential Commands, Branch Strategies, and Commit Best Practices
This guide explains the differences between centralized and distributed version control systems, details Git’s core concepts such as working directory, staging area, local and remote repositories, and provides a comprehensive list of essential Git commands, branch naming conventions, and commit message standards for effective collaboration.
SVN (Centralized Version Control System)
The central server holds the complete repository, and commit actions directly connect to the server.
GIT (Distributed Version Control System)
Git is also a complete system but more powerful, making operations a bit more complex. Git manages a repository locally, allowing modifications and commits locally before pushing to the server.
Other systems like CVS, VSS are also centralized: a single server stores all file revisions, and collaborators connect via clients to fetch or submit updates.
Working Principle
Working Directory : The project files you edit and see in your editor.
Staging Area : Visible only via Git GUI or shell; it is the intermediate station for committing code and resolving conflicts.
Local Repository : Visible only in the Git shell; it links local code with remote code and can store commits when offline.
Remote Repository : The server that stores the code, e.g., a GitLab instance.
Commonly Used Commands in Our Scenario
1. Configuration
# Configure email
git config --global user.email "your_email"
# Configure username
git config --global user.name "your_username"
# Generate SSH key
ssh-keygen -t rsa -C "your_email"
# List all config
git config -l
# System, local, global config
git config --system -l
git config --local -l
git config --global -l
# View remote info
git remote -v
# Remove remote
git remote remove origin
# Add new remote
git remote add origin <remote_path>
# Change remote URL
git remote set-url origin <remote_path>
# Edit config
git config -e
# Cache credentials (default 15 min)
git config --global credential.helper cache
# Set cache timeout
git config credential.helper 'cache --timeout=3600'
# Store credentials permanently
git config --global credential.helper store
# Clear stored credentials
git config --system --unset credential.helper2. Common Commands
# Clone a repository
git clone <repo_url>
# Add files to staging area
git add <files>
# Commit staged changes
git commit -m "message"
# Amend last commit
git commit --amend
# Remove files
git rm <file>
# Force remove
git rm -f <file>
# Remove from staging but keep in working tree
git rm --cached <file>
# Move or rename files
git mv <old> <new>
# Fetch from remote
git fetch
# Pull and merge
git pull
# Pull specific branch
git pull origin master
# Pull remote master into local dev
git pull origin master:dev
# Push to remote
git push <remote> <local_branch>:<remote_branch>
# Force push
git push --force origin master
# Merge branches
git merge <branch>
# Delete local branch
git branch -D <branch>
# Delete remote branch
git push origin --delete <branch>
# Show status
git status
# Show differences
git diff
# Diff specific file
git diff <file>
# Diff staged vs last commit
git diff --cached <file>
# View commit log
git log
# Reflog
git reflog
# Blame a file
git blame <file>
# Reset (soft/hard)
git reset --hard HEAD^
# Reset to specific commit
git reset --hard <commit_hash>
# List branches
git branch
# List remote branches
git branch -r
# List all branches
git branch -a
# Cherry-pick
git cherry-pick <commit>
# Restore deleted file
git checkout -- <file>
# Tagging
git tag <tagName>
# Push tag
git push origin <tagName>
# Push all tags
git push origin --tags
# Stash operations
git stash save "save message"
git stash list
git stash pop
git stash apply
git stash drop <stash@{n}>
git stash clear3. Some Auxiliary Operations
git config --global alias.co checkout
git config --global alias.ci commit
# Custom log format
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %C(green)%cr%Creset %C(bold blue)<%an>%Creset' --abbrev-commit"Create a .gitignore file in the repository root and list files or directories to be ignored.
Branch Design Conventions (Cultivating Good Habits)
Guidelines must fit the team; no rule is immutable.
1. Software Environments
DEV (Development): for developers to debug.
FAT (Feature Acceptance Test): for QA testing.
UAT (User Acceptance Test): for user acceptance testing in production‑like environment.
PRO (Production): the live environment.
2. Branch Naming
Branch
Name
Environment
Accessible
master
Main branch
PRO
Yes
release
Pre‑production branch
UAT
Yes
hotfix
Urgent fix branch
DEV
No
develop
Testing branch
FAT
Yes
feature
Feature development branch
DEV
No
master branch
The master branch is the main line deployed to production. It is merged from release or hotfix branches; direct commits are prohibited.
release branch
The release branch is used for pre‑production deployment, kept in sync with master, and receives merges from develop or hotfix. Direct modifications are discouraged.
hotfix / repair branch
The hotfix branch handles urgent issues, named with the hotfix- prefix, created from release or master, merged back after fixing, then deleted.
develop branch
The develop branch serves testing (FAT) and always contains the latest integrated code. Features may be merged from feature branches or developed directly, but direct commits are not recommended.
feature branch
The feature branch is for individual feature development, named with the feature- prefix, and removed after the feature is released.
Commit Message Conventions
A clear commit message aids log inspection. Use the format <type>(scope): <subject>.
type indicates the action and can be:
fix: bug fix (severity levels can be added, e.g., Blocker, Critical, Major, Normal, Minor, Trivial)
feat: new feature
test: testing code
style: code style or comments
docs: documentation changes
refactor: code refactor
chore: build process or auxiliary tool changes
scope denotes the affected module, library, or method.
subject is a concise description, preferably under 60 characters.
Example:
git commit -m 'fix(cart): correct rounding error in checkout price due to floating‑point issue'Link: https://juejin.cn/post/7197046540039405624
(© Original author, removed upon request)
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.
