Git 常用命令速查:从入门到精通
本文整理了 Git 开发中最常用的命令和技巧,涵盖基础操作、分支管理、远程协作、问题排查等场景,是开发者的速查手册。
一、Git 基础配置
1.1 用户信息配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git config --global user.name "John Doe" git config --global user.email "john@example.com"
git config --list git config user.name git config user.email
git config --global --edit
git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status git config --global alias.lg "log --oneline --graph --all"
|
1.2 SSH 密钥配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ssh-keygen -t ed25519 -C "your_email@example.com"
cat ~/.ssh/id_ed25519.pub
ssh-add ~/.ssh/id_ed25519
ssh -T git@github.com
ssh -T git@gitlab.com
|
1.3 常用 Git 配置优化
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 help.autocorrect 1
git config --global pull.rebase true
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 core.filemode false
git config --global core.editor "code --wait"
git config --global diff.tool bc3 git config --global difftool.bc3.trustexitcode true
git config --global merge.tool bc3 git config --global mergetool.bc3.trustexitcode true
|
二、仓库操作
2.1 创建仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git init git init my-project
git clone https://github.com/user/repo.git git clone git@github.com:user/repo.git
git clone -b develop https://github.com/user/repo.git
git clone --depth 1 https://github.com/user/repo.git
git clone --recursive https://github.com/user/repo.git
|
2.2 查看状态
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 status git status -s
git log git log --oneline git log --graph --oneline --all git log -n 5
git log --follow path/to/file
git log --author="John"
git log --since="2 weeks ago" git log --until="2024-01-01"
git log --stat git log --shortstat git log --numstat
git show commit_id git show --stat commit_id
|
2.3 差异比较
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
| git diff
git diff --cached git diff --staged
git diff HEAD
git diff commit1 commit2 git diff commit1..commit2
git diff branch1 branch2
git diff path/to/file
git diff -w
git diff > patch.diff git diff commit1 commit2 > patch.diff
|
三、文件操作
3.1 添加文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git add file.txt git add dir/file.txt
git add . git add -A
git add -u
git add -p git add -i
git diff --cached
|
3.2 忽略文件
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
| cat > .gitignore << EOF # 编译产物 *.class *.o *.so build/ dist/ target/
# 依赖目录 node_modules/ vendor/ __pycache__/
# 配置文件 .env *.local config.local.js
# IDE 文件 .idea/ .vscode/ *.swp *.swo
# 系统文件 .DS_Store Thumbs.db
# 日志文件 *.log logs/
# 临时文件 tmp/ temp/ *.tmp EOF
git check-ignore -v file.txt
git check-ignore *.log
|
3.3 删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git rm file.txt
git rm -r dir/
git rm --cached file.txt
git checkout -- file.txt
git checkout HEAD -- file.txt
|
3.4 移动/重命名文件
1 2 3 4 5 6 7 8 9
| git mv old_name.txt new_name.txt
git mv file.txt dir/file.txt
git add -A git status
|
四、提交操作
4.1 基本提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git commit -m "feat: add user authentication"
git commit -m "feat: add user authentication" -m "Implemented JWT-based authentication"
git commit -am "fix: resolve login bug"
git commit --amend git commit --amend -m "new commit message"
git commit --no-verify
|
4.2 提交规范
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <type>(<scope>): <subject>
feat: 新功能 fix: 修复 bug docs: 文档更新 style: 代码格式(不影响功能) refactor: 重构 test: 测试相关 chore: 构建过程或辅助工具变动
feat(auth): add login functionality fix(api): resolve null pointer exception docs(readme): update installation guide style(format): format code with prettier refactor(core): simplify user service test(unit): add unit tests for auth module chore(deps): upgrade dependencies
|
4.3 撤销提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git checkout -- file.txt git restore file.txt
git reset HEAD file.txt git restore --staged file.txt
git reset --soft HEAD~1 git reset --soft commit_id
git reset --hard HEAD~1 git reset --hard commit_id
git reset --hard HEAD~1 git push --force
git revert commit_id git push
|
4.4 查看提交历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| git log --oneline
git log --graph --oneline --all
git log --stat
git log --grep="bug fix" git log --grep="feat" --oneline
git log --follow -- path/to/file
git log --author="John" --oneline
git log --since="2 weeks ago" --until="yesterday"
|
五、分支管理
5.1 分支基础操作
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 branch git branch -a git branch -v
git branch feature/login git branch feature/login commit_id
git checkout feature/login git switch feature/login
git checkout -b feature/login git switch -c feature/login
git branch -d feature/login git branch -D feature/login
git branch -m old-name new-name git branch -m new-name
|
5.2 分支合并
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| git checkout main git merge feature/login
git merge --ff-only feature/login
git merge --no-ff feature/login
git merge --squash feature/login
git diff --name-only --diff-filter=U git diff
git add file.txt
git merge --abort
|
5.3 分支变基(Rebase)
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
| git checkout feature/login git rebase main
git rebase -i HEAD~3 git rebase -i commit_id
git rebase --continue
git rebase --skip
git rebase --abort
git pull --rebase
|
5.4 分支管理最佳实践
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git branch --merged
git branch --no-merged
git log --oneline --graph --all --decorate
git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d
git branch -r
git fetch -p git remote prune origin
|
六、远程协作
6.1 远程仓库操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git remote git remote -v
git remote add origin https://github.com/user/repo.git git remote add origin git@github.com:user/repo.git
git remote set-url origin https://github.com/user/new-repo.git
git remote remove origin
git remote rename origin upstream
git remote show origin
|
6.2 拉取和推送
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
| git fetch origin git fetch origin feature/login
git pull origin main git pull
git pull --rebase origin main
git push origin feature/login git push
git push -u origin feature/login
git push --force git push --force-with-lease
git push --all origin
git push --tags origin
git push origin --delete feature/login git push origin :feature/login
|
6.3 标签管理
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
| git tag git tag -l "v1.*"
git tag v1.0.0 git tag v1.0.0 commit_id
git tag -a v1.0.0 -m "Release version 1.0.0"
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 push origin :refs/tags/v1.0.0
git checkout v1.0.0
|
6.4 Git Flow 工作流
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git flow init
git flow feature start login git flow feature finish login
git flow release start 1.0.0 git flow release finish 1.0.0
git flow hotstart critical-bug git flow hotfix finish critical-bug
|
七、高级技巧
7.1 暂存(Stash)
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
| git stash git stash save "WIP: login feature"
git stash list
git stash apply git stash apply stash@{1}
git stash pop git stash pop stash@{1}
git stash drop git stash drop stash@{1}
git stash clear
git stash branch feature/login stash@{0}
git stash -u git stash --include-untracked
git stash -a git stash --all
|
7.2 樱桃拣选(Cherry-pick)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git cherry-pick commit_id
git cherry-pick commit1 commit2 commit3
git cherry-pick commit1..commit2
git cherry-pick --no-commit commit_id
git cherry-pick --edit commit_id
git cherry-pick --abort
git cherry-pick --continue
|
7.3 二分查找(Bisect)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git bisect start
git bisect bad
git bisect good v1.0.0
git bisect good
git bisect run ./test-script.sh
git bisect reset
git bisect log
|
7.4 子模块(Submodule)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git submodule add https://github.com/user/lib.git libs/lib
git submodule init
git submodule update git submodule update --init --recursive
git submodule status
git submodule update --remote
git submodule deinit libs/lib git rm libs/lib rm -rf .git/modules/libs/lib
|
7.5 Git Hooks
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
| ls .git/hooks/
pre-commit pre-push commit-msg post-merge
cp .git/hooks/pre-commit.sample .git/hooks/pre-commit chmod +x .git/hooks/pre-commit
cat > .git/hooks/pre-commit << 'EOF'
if git diff --cached | grep -E "console\.log|debugger"; then echo "Error: Found debug code" exit 1 fi
npm run lint EOF
chmod +x .git/hooks/pre-commit
|
八、问题排查
8.1 查看修改历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git blame file.txt git blame -L 10,20 file.txt
git show commit_id
git log --follow -- path/to/file
git bisect start git bisect bad git bisect good v1.0.0
|
8.2 恢复丢失的提交
1 2 3 4 5 6 7 8 9 10 11 12 13
| git reflog git reflog show --all
git reset --hard HEAD@{1} git checkout -b recovery-branch commit_id
git fsck --lost-found
git cherry-pick commit_id
|
8.3 解决常见问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git commit --amend -m "correct message"
git add forgotten_file.txt git commit --amend --no-edit
git reset --hard HEAD~1 git checkout correct-branch git cherry-pick original-branch
git mergetool git diff --name-only --diff-filter=U
git reflog git branch recovered-branch commit_id
git fsck git gc
|
8.4 性能优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git gc git gc --aggressive
git remote prune origin
git count-objects -v git du -sh .git/objects/*
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print substr($0,6)}' | sort -k2 -nr | head -20
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path/to/large-file' --prune-empty --tag-name-filter cat -- --all
|
九、实用脚本
9.1 日常开发脚本
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
| function git-feat() { git checkout -b "feat/$1" }
function git-fix() { git checkout -b "fix/$1" }
function git-commit-push() { git add -A git commit -m "$1" git push -u origin $(git branch --show-current) }
function git-sync-main() { git fetch origin git rebase origin/main }
function git-cleanup() { git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d }
function git-today() { git log --since="today" --author="$(git config user.name)" }
|
9.2 Git 配置模板
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
| [alias] lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit st = status -s co = checkout br = branch ci = commit last = log -1 HEAD unstage = reset HEAD -- who = blame -L -20,-0 [user] name = Your Name email = your.email@example.com [core] editor = code --wait autocrlf = input filemode = false [pull] rebase = true [push] default = simple [merge] tool = vscode [difftool "vscode"] cmd = code --wait --diff $LOCAL $REMOTE
|
十、总结
Git 是开发者必备的工具,掌握常用命令可以大幅提升开发效率。关键要点:
- 基础操作:add、commit、push、pull
- 分支管理:branch、checkout、merge、rebase
- 问题排查:log、diff、blame、reflog
- 高级技巧:stash、cherry-pick、bisect
建议将常用命令配置为别名,并建立适合自己的工作流。
参考资料: