Master Git Command Line, Boost Development Efficiency π Git is an indispensable version control tool in modern software development. This article organizes the most commonly used Git commands, categorized by function, for quick reference and learning.
1. Repository Initialization & Basic Configuration βοΈ Repository Initialization
Command
Function
Common Parameters
Use Case
git init
Initialize new Git repository
--bare create bare repository--template=<template_directory> specify template
Starting new project
git clone <url>
Clone remote repository
-b <branch> specify branch--depth 1 shallow clone--recursive recursively clone submodules
Getting existing project
Configuration Management
Command
Function
Common Parameters
Use Case
git config
Configure Git settings
--global global config--local local config--system system config
Environment initialization
git config --list
View all configurations
--show-origin show config source
Check configuration status
git config --unset
Remove configuration
--global remove global config
Clean incorrect config
Common Configuration Commands 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 git config --global user.name "Your Name" git config --global user.email "your.email@example.com" git config --global core.editor "code --wait" git config --global core.editor "vim" git config --global init.defaultBranch main git config --global core.autocrlf input git config --global core.autocrlf true git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --list git config user.name
2. File Status & Commit Management π File Status Check
Command
Function
Common Parameters
Use Case
git status
Check working directory status
-s short output--porcelain script-friendly format-b show branch info
Check file status
git ls-files
List files in index
--cached staged files--deleted deleted files--modified modified files
View tracked files
git diff
View differences
--cached staged differences--staged same as βcached--name-only show only filenames
Compare file changes
File Operations
Command
Function
Common Parameters
Use Case
git add
Add files to staging area
. add all files-A add all changes-p interactive add-u add only tracked files
Prepare for commit
git rm
Remove files
--cached remove from staging only-r recursively remove directory-f force remove
Remove files
git mv
Move/rename files
No special parameters
Rename files
git restore
Restore files
--staged unstage--worktree restore working tree
Undo changes
Commit Management
Command
Function
Common Parameters
Use Case
git commit
Commit staged content
-m "message" commit message-a auto stage tracked files--amend modify last commit-v show diff
Save changes
git commit --fixup
Create fixup commit
<commit> specify commit to fix
Fix historical commit
git commit --squash
Create squash commit
<commit> specify commit to squash
Prepare commit merge
Practical Scenarios 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 git add . && git commit -m "feat: add new feature" git commit --amend -m "fix: correct commit message" git add forgotten_file.txt git commit --amend --no-edit git restore --staged <file> git reset HEAD <file> git restore <file> git checkout -- <file> git add -p <file>
3. Version History & Rollback π History Viewing
Command
Function
Common Parameters
Use Case
git log
View commit history
--oneline single line display--graph graphical display-n <num> limit display count--since="2 weeks ago" time filter
View project history
git log --follow
Track file history
<file> specify file
View file change history
git show
Show commit details
<commit-id> specify commit--stat show statistics--name-only show filenames only
View specific commit
git blame
View file modification record
-L <start>,<end> specify line range-w ignore whitespace
Track code author
git shortlog
Commit summary grouped by author
-n sort by commit count-s show statistics only
Generate changelog
Advanced History Viewing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit git log --follow -p <file> git log master..feature-branch git log --author="John Doe" git log --grep="bug fix" git log --since="2023-01-01" --until ="2023-12-31"
Version Rollback & Reset
Command
Function
Common Parameters
Use Case
git reset
Reset to specified state
--soft keep working tree and staging--mixed keep working tree--hard complete reset
Undo commits
git revert
Create reverse commit
--no-edit donβt edit commit message-n donβt auto commit
Safe undo
git checkout
Switch branch or restore file
<file> restore file<commit> -- <file> restore to specific version
Restore files
git reflog
View reference log
--all show all references
Recover lost commits
Rollback Scenario Examples 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 git reset --soft HEAD~1 git reset --hard HEAD~1 git reset --hard HEAD~3 git reset --hard <commit-id> git revert HEAD git revert <commit-id> git reflog git reset --hard <commit-id>
4. Branch Management Strategy πΏ Basic Branch Operations
Command
Function
Common Parameters
Use Case
git branch
Branch management
-a show all branches-r show remote branches-d delete branch-D force delete-m rename branch
Manage branches
git checkout
Switch branch
-b create and switch- switch to previous branch-t track remote branch
Branch switching
git switch
Switch branch (new command)
-c create and switch- switch to previous branch
Modern branch switching
git branch --set-upstream-to
Set upstream branch
origin/<branch> specify remote branch
Associate remote branch
Branch Merge & Rebase
Command
Function
Common Parameters
Use Case
git merge
Merge branch
--no-ff disable fast-forward merge--squash squash merge--abort abort merge
Integrate branches
git rebase
Rebase operation
-i interactive rebase--onto specify new base--abort abort rebase--continue continue rebase
Clean commit history
git cherry-pick
Pick commits
-n donβt auto commit-x record original commit info
Selective merge
Branch Workflow Examples 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 git checkout -b feature/user-auth git switch -c feature/user-auth git branch -a git branch -m old-name new-name git branch -d feature/completed-feature git branch -D feature/abandoned-feature git push origin --delete feature/old-feature git branch --set-upstream-to=origin/main main git checkout main git merge --no-ff feature/user-auth git merge --squash feature/user-auth git commit -m "feat: add user authentication feature" git checkout feature/user-auth git rebase main git checkout main git merge feature/user-auth git rebase -i HEAD~3 git cherry-pick <commit-id>
5. Remote Repository Collaboration π Remote Repository Management
Command
Function
Common Parameters
Use Case
git remote
Manage remote repositories
-v show detailed infoadd <name> <url> add remote reporemove <name> remove remote reporename <old> <new> rename
Configure remote repos
git remote show
Show remote repository info
<remote-name> specify remote repo
View remote status
git remote prune
Clean remote branch references
<remote-name> specify remote repo
Clean invalid references
Data Synchronization
Command
Function
Common Parameters
Use Case
git fetch
Fetch remote updates
--all fetch all remote branches--prune clean invalid references--tags fetch tags
Sync remote data
git pull
Pull and merge
--rebase use rebase merge--ff-only fast-forward only--no-ff disable fast-forward
Update local branch
git push
Push to remote
-u set upstream branch--force-with-lease safe force push--tags push tags--delete delete remote branch
Publish changes
Collaboration Scenarios 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 git remote add origin https://github.com/user/repo.git git remote add upstream https://github.com/original/repo.git git remote -v git remote show origin git push -u origin main git fetch origin git fetch --all git pull origin main git pull --rebase origin main git push origin feature-branch git push --force-with-lease origin main git push --tags git push origin v1.0.0 git push origin --delete feature-branch git remote prune origin
6. Tag Management π·οΈ Tag Operations
Command
Function
Common Parameters
Use Case
git tag
Tag management
-a create annotated tag-d delete tag-l list tags-f force create
Version marking
git tag -l
List tags
"v1.*" pattern matching
Find specific tags
git show <tag>
Show tag info
No special parameters
View tag details
git push --tags
Push tags
--follow-tags push annotated tags only
Release versions
Tag Usage Examples 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 git tag v1.0.0 git tag -a v1.0.0 -m "Release version 1.0.0" git tag -a v0.9.0 <commit-id> -m "Version 0.9.0" git tag git tag -l "v1.*" git show v1.0.0 git push origin v1.0.0 git push --tags git tag -d v1.0.0 git push origin --delete v1.0.0 git checkout -b version-1.0.0 v1.0.0
7. Stashing & Recovery πΎ Stash Operations
Command
Function
Common Parameters
Use Case
git stash
Stash current work
push -m "message" add description-u include untracked files-a include all files
Temporarily save work
git stash list
List stash entries
No special parameters
View stash records
git stash pop
Restore and delete stash
stash@{n} specify stash
Restore work state
git stash apply
Restore but keep stash
stash@{n} specify stash
Reuse stash
git stash drop
Delete stash
stash@{n} specify stash
Clean stash
git stash clear
Clear all stashes
No special parameters
Clean all stashes
Stash Usage Scenarios 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 git stash git stash push -m "temporary save: work state before bug fix" git stash -u git stash list git stash pop git stash apply stash@{1} git stash show git stash show -p stash@{1} git stash drop stash@{1} git stash clear git stash branch new-feature stash@{1}
8. Submodule Management π¦ Submodule Operations
Command
Function
Common Parameters
Use Case
git submodule add
Add submodule
<url> <path> specify URL and path
Include external project
git submodule init
Initialize submodule
No special parameters
Initialize configuration
git submodule update
Update submodule
--recursive recursive update--remote update to remote latest
Sync submodules
git submodule foreach
Execute command on all submodules
<command> specify command
Batch operations
Submodule Usage Examples 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 git submodule add https://github.com/user/library.git libs/library git clone --recursive https://github.com/user/project.git git submodule init git submodule update git submodule update --remote git submodule foreach git pull origin main git submodule deinit libs/library git rm libs/library
9. Practical Tips & Alias Configuration β‘ Recommended Alias Configuration 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.unstage 'reset HEAD --' git config --global alias.last 'log -1 HEAD' git config --global alias.visual '!gitk' 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" git config --global alias.ll "log --oneline --graph --decorate --all" git config --global alias.ls "log --pretty=format:'%C(yellow)%h %C(blue)%ad %C(red)%d %C(reset)%s %C(green)[%cn]' --decorate --date=short" git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short" git config --global alias.amend 'commit --amend --no-edit' git config --global alias.uncommit 'reset --soft HEAD~1' git config --global alias.unstage 'reset HEAD --' git config --global alias.discard 'checkout --' git config --global alias.graph 'log --graph --oneline --decorate --all' git config --global alias.aliases "config --get-regexp '^alias\.'"
.gitignore Common Template 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 node_modules/ vendor/ bower_components/ dist/ build/ out/ target/ *.min.js *.min.css *.log logs/ npm-debug.log* yarn-debug.log* yarn-error.log* .DS_Store .DS_Store? ._* .Spotlight-V100 .Trashes ehthumbs.db Thumbs.db .vscode/ .idea/ *.swp *.swo *~ .project .classpath .settings/ .env .env.local .env.development.local .env.test.local .env.production.local .cache/ .parcel-cache/ .next/ .nuxt/ coverage/ *.lcov *.tmp *.temp .tmp/
Git Hook Examples 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 npm run lint if [ $? -ne 0 ]; then echo "Code check failed, please fix before commit" exit 1 fi commit_regex='^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,50}' if ! grep -qE "$commit_regex " "$1 " ; then echo "Wrong commit message format, please use: type(scope): description" exit 1 fi
10. Common Problem Solutions π§ Typical Scenario Handling Undo last commit but keep changes
Modify pushed commit message
1 2 git commit --amend -m "new commit message" git push --force-with-lease
Merge multiple commits
Resolve merge conflicts
1 2 3 4 5 6 7 8 9 git status git add <resolved-file> git commit git merge --abort
Recover mistakenly deleted branch
1 2 3 4 5 git reflog git checkout -b <branch-name> <commit-id>
Clean local branches
1 2 3 4 5 git branch --merged | grep -v "\*\|main\|master" | xargs -n 1 git branch -d git remote prune origin
Undo git add
1 2 3 4 5 git reset HEAD git reset HEAD <file>
Modify historical commits
1 2 3 4 5 6 git rebase -i HEAD~3 git commit --fixup <commit-id> git rebase -i --autosquash HEAD~3
11. Advanced Tips π― Search & Find 1 2 3 4 5 6 7 8 9 10 11 12 13 git log -S "function_name" --source --all git log --grep="bug fix" --oneline git bisect start git bisect bad HEAD git bisect good v1.0.0 git grep "TODO" HEAD~5
1 2 3 4 5 6 7 8 9 git gc --aggressive --prune=now git count-objects -vH git reflog expire --expire=now --all git gc --prune=now
Workflow Integration 1 2 3 4 5 6 7 8 9 10 11 12 git flow init git flow feature start new-feature git flow feature finish new-feature git flow release start 1.0.0 git flow release finish 1.0.0
12. Recommended Learning Resources π Official Resources
Interactive Learning
Quick Reference
Advanced Reading
βPro Gitβ Book - Scott Chacon & Ben Straub
βGit Version Control Managementβ - Jon Loeliger & Matthew McCullough
βGit Authoritative Guideβ - Jiang Xin
π‘ Tips : Itβs recommended to set frequently used commands as aliases and regularly practice branch operations and conflict resolution to greatly improve daily development efficiency!
π Quick Search : Use Ctrl+F to search for specific commands or functional keywords to quickly locate needed content.
β‘ Efficiency Boost : After mastering these commands, consider learning Git Flow workflow and semantic commit conventions to further improve team collaboration efficiency.