建议先阅读 Git 工作区、暂存区和版本库以了解 Git 命令执行时涉及的不同区域。
一、新建代码库
| |
| $ git init |
| |
| |
| $ git init [project-name] |
| |
| |
| $ git clone [url] |
二、配置
| |
| $ git config -l |
| |
| |
| $ git config --show-origin -l |
| |
| |
| $ git config --system -l |
| |
| |
| $ git config --global -l |
| |
| |
| $ git config --local -l |
| |
| |
| $ git config -e [--global] |
| |
| |
| $ git config [--global] user.name "[name]" |
| $ git config [--global] user.email "[email]" |
三、增加 / 删除文件
| |
| $ git add [file] |
| |
| |
| $ git add [dir] |
| |
| |
| $ git add . |
| |
| |
| $ git rm [file] |
| |
| |
| $ git rm -r [dir] |
| |
| |
| $ git rm --cached [file] |
| |
| |
| $ git rm -r --cached [dir] |
| |
| |
| $ git rm -r --cached . |
| |
| |
| $ git mv [old-name] [new-name] |
四、代码提交
| |
| $ git commit -m "[message]" |
| |
| |
| $ git commit [file] -m "[message]" |
| |
| |
| $ git commit -a |
| |
| |
| |
| $ git commit --amend -m "[message]" |
五、分支
| |
| $ git branch |
| |
| |
| $ git branch -r |
| |
| |
| $ git branch -a |
| |
| |
| $ git branch -v |
| |
| |
| $ git branch [branch-name] |
| |
| |
| $ git checkout -b [branch-name] |
| |
| |
| $ git branch [branch-name] [commit] |
| |
| |
| $ git checkout [branch-name] |
| |
| |
| $ git checkout - |
| |
| |
| $ git merge [branch-name] |
| |
| |
| $ git cherry-pick [commit] |
| |
| |
| $ git branch -d [branch-name] |
| |
| |
| $ git push [remote] --delete [branch-name] |
| $ git push [remote] :[branch-name] |
| |
| |
| $ git branch -m [old-branch-name] [new-branch-name] |
| |
| |
| $ git branch -vv |
| |
| |
| $ git checkout -b [local-branch] [remote]/[remote-branch] |
| |
| |
| $ git checkout --track [remote]/[branch-name] |
| |
| |
| $ git branch -u [remote]/[remote-branch] [local-branch] |
| |
| |
| $ git branch -u [remote]/[remote-branch] |
六、标签
| |
| $ git tag |
| |
| |
| $ git tag -l "v1.8.5*" |
| |
| |
| $ git tag [tag-name] |
| |
| |
| $ git tag [tag-name] [commit] |
| |
| |
| $ git tag -a [tag-name] -m "[message]" |
| |
| |
| $ git tag -a [tag-name] [commit] -m "[message]" |
| |
| |
| $ git show [tag-name] |
| |
| |
| $ git push [remote] [tag-name] |
| |
| |
| $ git push [remote] --tags |
| |
| |
| $ git tag -d [tag-name] |
| |
| |
| $ git push [remote] --delete [tag-name] |
| $ git push [remote] :refs/tags/[tag-name] |
| |
| |
| $ git checkout -b [branch] [tag-name] |
七、查看信息
| |
| $ git status |
| |
| |
| $ git status -s |
| |
| |
| $ git log |
| |
| |
| $ git log [commit] |
| |
| |
| $ git log -p [file] |
| |
| |
| $ git log --stat |
| |
| |
| $ git log --pretty=oneline |
| |
| |
| $ git log --oneline |
| |
| |
| $ git log --oneline --graph |
| |
| |
| $ git log --oneline --decorate --graph --all |
| |
| |
| $ git log -5 |
| |
| |
| $ git log --after="2022-07-01" |
| |
| |
| $ git log --before="2022-07-01" |
| |
| |
| $ git log --author="HL" |
| |
| |
| $ git log --grep [keyword] |
| |
| |
| $ git log -S [keyword] |
| |
| |
| $ git log --follow [file] |
| $ git whatchanged [file] |
| |
| |
| $ git shortlog -sn |
| |
| |
| $ git blame [file] |
| |
| |
| $ git diff [first-commit]...[second-commit] |
| |
| |
| $ git diff [first-branch] [second-branch] |
| |
| |
| $ git diff --stat [first-branch] [second-branch] |
| |
| |
| git diff [first-branch] [second-branch] [file] |
| |
| |
| $ git show [commit] |
| |
| |
| $ git show --name-only [commit] |
| |
| |
| $ git show [commit]:[filename] |
| |
| |
| $ git reflog |
八、远程同步
| |
| $ git remote -v |
| |
| |
| $ git remote show [remote] |
| |
| |
| $ git remote add [name] [url] |
| |
| |
| $ git fetch [remote] |
| |
| |
| $ git fetch [remote] [branch] |
| |
| |
| $ git pull [remote] [remote-branch]:[local-branch] |
| |
| |
| $ git pull [remote] [branch] |
| |
| |
| $ git push [remote] [branch] |
| |
| |
| $ git push -u [remote] [remote-branch] |
| |
| |
| $ git push [remote] --all |
九、撤销
| |
| $ git checkout [file] |
| |
| |
| $ git checkout [commit] [file] |
| |
| |
| $ git checkout . |
| |
| |
| |
| $ git reset [--mixed] [HEAD] |
| |
| |
| |
| $ git reset [--mixed] [file] [commit] |
| |
| |
| $ git reset --hard [HEAD] |
| |
| |
| $ git reset --hard [commit] |
| |
| |
| $ git reset --soft [commit] |
| |
| |
| $ git revert [commit] |
| |
| |
| $ git stash |
| $ git stash pop |
ref: 常用 Git 命令清单、《Pro Git》
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律