Master Git: Essential Commands, Workflows, and Best Practices
This comprehensive guide covers Git fundamentals, including workspace concepts, configuration, common commands for adding, committing, branching, merging, rebasing, stashing, submodules, hook setup, branch management strategies, and solutions to frequent Git issues, providing developers with a complete reference for effective version control.
Git Flow Diagram
Workspace: 工作区 Index / Stage: 暂存区 Repository: 本地仓库 Remote: 远程仓库
Configure Git
# Configure global user
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
# Configure aliases
$ git config --global alias.co checkout
$ git config --global alias.ss status
$ git config --global alias.cm commit
$ git config --global alias.br branch
$ git config --global alias.rg reflog
$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Unset global configuration
$ git config --global --unset alias.xxx
$ git config --global --unset user.xxxView Git Information
# List system configuration
$ git config --list
# Show user configuration file
$ cat ~/.gitconfig
# Show repository configuration
$ cat .git/config
# List files in the index (staging area)
$ git ls-files
# Show reflog (command history)
$ git reflog
# Show all git commands
$ git --help -a
# Show HEAD reference
$ cat .git/HEAD
# Show commit history with various options
$ git log --oneline --grep="keyword" --graph --all --author "username" --reverse -n --p --before="date" --after="date" --stat --abbrev-commit --pretty=format:"xxx"git reflog
Shows a list of HEAD changes caused by branch switches, commits, and resets; it cannot recover changes undone by checkout or stash because those were never committed.
Git periodically cleans up reflog entries, so old entries may disappear.
git log graph symbols
Branch pointer : a branch is a pointer; creating a new branch creates a new pointer.
HEAD switch : switching to a branch moves HEAD to that branch pointer.
Commit switch : switching to a commit moves HEAD directly to that commit (detached HEAD).
Symbol explanation:
* commit
| branch forward
/ branch split
\ merge
|/ new branchCommon Git Commands
# Show status of working directory and staging area
$ git status
# Add files to staging area
$ git add .
# Commit staged changes
$ git commit -m "Commit message"
# Shortcut: add and commit (tracked files only)
$ git commit -am "Commit message"
# Push local branch to remote and set upstream
$ git push -u origin branchName
# Pull remote changes
$ git pull origin branchName
# Merge branches
$ git merge branchName
# List local branches
$ git branch
# List all branches (local + remote)
$ git branch -a
# Switch branches
$ git checkout branchName
# Stash changes
$ git stash
$ git stash pop
# View differences
$ git diff
$ git diff --cached
# Remote operations
$ git remote -v
$ git remote rm origin
$ git remote set-url origin <new-url>
# Tag operations
$ git tag v1.0
$ git push origin --tags
# Delete files
$ git rm filename
$ git rm --cached filename
# Reset operations
$ git reset --hard HEAD~1
$ git reset --soft HEAD~1
# Revert a commit (creates a new commit that undoes changes)
$ git revert HEAD
# Cherry-pick a commit
$ git cherry-pick <commit-id>
# Submodule commands
$ git submodule add <url> <path>
$ git submodule update --init --recursiveBranch Management Guidelines
develop : development branch, daily updates.
test : testing branch, used after feature testing.
release : pre‑release branch, for final verification before production.
master : production branch, receives code after release.
Typical workflow: developers pull from develop, finish features, merge into test, then into release, and finally into master with tags. Bug fixes on production are branched from release (or master if no release branch) and merged back after verification.
Git Hooks (Pre‑commit Example)
# Install pre‑commit package
npm install pre-commit --save-dev
# Add hook script (if not present)
node ./node_modules/pre-commit/install.js
# Configure package.json
"scripts": {
"build": "tsc",
"eslint": "eslint src --ext .ts",
"eslint:fix": "eslint src --ext .ts --fix"
},
"pre-commit": ["eslint"]To skip the hook during commit:
$ git commit --no-verifyCommon Issues and Solutions
Unrelated histories : use git pull --allow-unrelated-histories or git merge --allow-unrelated-histories.
Merge conflicts : resolve files, then git add and commit, or abort with git merge --abort.
Removing accidentally pushed files : git rm --cached filename and commit.
Connecting via SSH : generate SSH key with ssh-keygen -t rsa -C "[email protected]", add public key to remote, test with ssh -T [email protected], and set remote URL to SSH.
Credential issues : store credentials with git config --global credential.helper store or update Windows Credential Manager.
Detached HEAD : occurs when checking out a specific commit; create a new branch to retain work.
Empty folder tracking : add a .gitkeep file.
Force push : use git push --force only when necessary.
References
Git Book, Pro Git (Chinese), git‑recipes, "How to Use Git Elegantly", submodule development guides, GitHub API v3 documentation.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
