Git 命令速查表
基础配置
在 ~/.gitconfig 添加别名使命令更简单:
1 2 3 4 5 6 7 8 9 10 11 12
| [alias] co = checkout ci = commit st = status pl = pull ps = push dt = difftool l = log --stat cp = cherry-pick ca = commit -am b = branch lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
|
查看、添加、提交、删除、找回,重置修改文件
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
| git help <command>
git show git show $id
git checkout -- <file> git checkout .
git add <file> git add . git add -p <file>
git rm <file> git rm <file> --cached
git reset <file> git reset -- . git reset --hard git reset --hard HEAD~1
git commit <file> git commit . git commit -a git commit -am "some comments" git commit --amend
git revert <$id> git revert HEAD
|
查看文件差异
1 2 3 4 5 6 7 8
| git diff <file> git diff git diff <$id1> <$id2> git diff <branch1>..<branch2> git diff --staged git diff --cached git diff --stat git diff HEAD
|
查看提交记录
1 2 3 4 5 6 7 8 9 10
| git log git log <file> git log -p <file> git log -p -2 git log --stat git log --oneline git log --graph git log --since="2 weeks ago" git log --author="neoyin" git log --grep="fix"
|
Git 本地分支管理
查看、切换、创建和删除分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git branch -r git branch -a git branch <new_branch> git branch -v git branch --merged git branch --no-merged
git checkout <branch> git checkout -b <new_branch> git checkout -b <new_branch> <branch>
git checkout $id git checkout $id -b <new_branch>
git branch -d <branch> git branch -D <branch>
|
分支合并和rebase
1 2 3 4 5 6 7 8
| git merge <branch> git merge origin/master --no-ff git merge --abort
git rebase master <branch> git checkout <branch> && git rebase master && git checkout master && git merge <branch> git rebase --abort git rebase --continue
|
Git 补丁管理
1 2 3 4 5
| git diff > ../sync.patch git apply ../sync.patch git apply --check ../sync.patch git format-patch -1 <commit> git am <patch-file>
|
Git 暂存管理
1 2 3 4 5 6 7 8 9
| git stash git stash list git stash apply git stash apply stash@{1} git stash drop git stash drop stash@{1} git stash pop git stash -u git stash -m "message"
|
Git 远程分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git pull git pull --no-ff git pull --rebase git fetch origin git merge origin/master git checkout --track origin/branch git checkout -b <local_branch> origin/<remote_branch>
git push git push origin master git push -u origin master git push origin <local_branch> git push origin <local_branch>:<remote_branch> git push origin :<remote_branch> git push --force
|
Git 远程仓库管理
1 2 3 4 5
| git remote -v git remote show origin git remote add origin git@github:robbin/robbin_site.git git remote set-url origin git@github.com:robbin/robbin_site.git git remote rm <repository>
|
创建远程仓库
1 2 3 4 5 6 7 8 9
| git clone --bare robbin_site robbin_site.git scp -r my_project.git git@git.csdn.net:~
mkdir robbin_site.git && cd robbin_site.git && git --bare init git remote add origin git@github.com:robbin/robbin_site.git git push -u origin master git push -u origin develop
git remote set-head origin master
|
设置跟踪远程库和本地库
1 2 3 4
| git branch --set-upstream master origin/master git branch --set-upstream develop origin/develop
git branch -u origin/master
|
Git 高级功能
Git Hooks
Git Hooks 是 Git 事件触发时执行的脚本,位于 .git/hooks 目录:
pre-commit:提交前执行
post-commit:提交后执行
pre-push:推送前执行
post-receive:接收推送后执行
示例:创建一个 pre-commit 钩子来检查代码风格:
1 2 3 4 5 6 7 8
| #!/bin/sh
npm run lint if [ $? -ne 0 ]; then echo "代码风格检查失败,请修复后再提交" exit 1 fi exit 0
|
Git LFS (Large File Storage)
用于管理大文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git lfs install
git lfs track "*.zip" git lfs track "*.tar.gz"
git add .gitattributes git commit -m "Add Git LFS tracking"
git add large-file.zip git commit -m "Add large file" git push
|
Git 工作流最佳实践
Git Flow
- master:主分支,保持稳定版本
- develop:开发分支,集成所有功能开发
- feature/:功能分支,从 develop 分支创建
- release/:发布分支,从 develop 分支创建
- hotfix/:热修复分支,从 master 分支创建
GitHub Flow
- main:主分支,保持可部署状态
- feature/:功能分支,从 main 分支创建
- 提交 Pull Request
- 代码审查
- 合并到 main 分支
- 部署
常见问题解决方案
解决合并冲突
- 查看冲突文件:
git status
- 编辑冲突文件,解决冲突
- 标记冲突已解决:
git add <conflicted-file>
- 完成合并:
git commit
撤销错误的提交
1 2 3 4 5 6 7 8
| git reset --soft HEAD~1
git reset --hard HEAD~1
git push -f origin <branch>
|
清理未跟踪的文件
1 2 3 4 5 6 7 8
| git clean -n
git clean -f
git clean -fd
|
查找提交历史中的大文件
1
| git rev-list --objects --all | grep "^\w*\s*\w*\.\w*$" | sort -k 2 -n | tail -20
|
参考资料