简明 Git 命令速查表
本文总结了git常用的命令,以便学习者使用时查阅~
几个专用名词的译名如下
- Workspace:工作区
- Index / Stage:暂存区
- Repository:仓库区(或本地仓库)
- Remote:远程仓库
复制一个已创建的仓库:
git clone ssh://user@domain.com/repo.git
创建一个新的本地仓库:
git init
显示工作路径下已修改的文件:
git status
显示与上次提交版本文件的不同:
git diff
把当前所有修改添加到下次提交中:
git add
把对某个文件的修改添加到下次提交中:
git add -p <file>
提交本地的所有修改:
git commit -a
提交之前已标记的变化:
git commit
附加消息提交:
git commit -m 'message here'
提交,并将提交时间设置为之前的某个日期:
git commit --date="`date --date='n day ago'`" -am "Commit Message"
修改上次提交
请勿修改已发布的提交记录!
git commit --amend
把当前分支中未提交的修改移动到其他分支
git stash
git checkout branch2
git stash pop
列出所有stash
git stash list
查看一个stash
git show stash@{0}
删除一个stash
git stash drop stash@{0}
将指定的stash取出来
git stash apply stash@{1}
将最后一个stash取出来,并删除掉队列中相应的值
git stash pop
清空所有stash
git stash clear
从当前目录的所有文件中查找文本内容:
git grep "Hello"
在某一版本中搜索文本:
git grep "Hello" v2.5
从最新提交开始,显示所有的提交记录(显示hash, 作者信息,提交的标题和时间):
git log
显示所有提交(仅显示提交的hash和message):
git log --oneline
显示某个用户的所有提交:
git log --author="username"
显示某个文件的所有修改:
git log -p <file>
谁,在什么时间,修改了文件的什么内容:
git blame <file>
列出所有的分支:
git branch
列出所有的分支以及其对应的远端分支:
git branch -vv
切换分支:
git checkout <branch>
创建并切换到新分支:
git checkout -b <branch>
基于当前分支创建新分支:
git branch <new-branch>
基于远程分支创建新的可追溯的分支:
git branch --track <new-branch> <remote-branch>
删除本地分支:
git branch -d <branch>
给当前版本打标签:
git tag <tag-name>
获取本地标签:
git tag
删除一个本地标签:
git tag -d <tag-name>
删除一个远端标签:
git push origin :refs/tags/<tag-name>
发布标签:
git push --tags
列出当前配置的远程端:
git remote -v
显示远程端的信息:
git remote show <remote>
添加新的远程端:
git remote add <remote> <url>
下载远程端版本,但不合并到HEAD中:
git fetch <remote>
下载远程端版本,并自动与HEAD版本合并:
git remote pull <remote> <url>
将远程端版本合并到本地版本中:
git pull origin master
将本地版本发布到远程端:
git push remote <remote> <branch>
将本地branch1发布到远程端branch2:
git push origin <branch1>:<branch2>
删除远程端分支:
git push <remote> :<branch> (since Git v1.5.0) git push <remote> --delete <branch> (since Git v1.7.0)
将分支合并到当前HEAD中:
git merge <branch>
将当前HEAD版本重置到分支中:
请勿重置已发布的提交!
git rebase <branch>
退出重置:
git rebase --abort
解决冲突后继续重置:
git rebase --continue
使用配置好的merge tool 解决冲突:
git mergetool
在编辑器中手动解决冲突后,标记文件为已解决冲突
git add <resolved-file> git rm <resolved-file>
两个分支的合并,将other-branch合并到main-branch中
git checkout <other-branch> git rebase <main-branch> git checkout <main-branch> git merge <other-branch>
放弃工作目录下的所有修改:
git reset --hard HEAD
移除缓存区的所有文件(i.e. 撤销上次git add):
git reset HEAD
放弃某个文件的所有本地修改:
git checkout HEAD <file>
重置一个提交(通过创建一个截然不同的新提交)
git revert <commit>
将HEAD重置到指定的版本,并抛弃该版本之后的所有修改:
git reset --hard <commit>
将HEAD重置到上一次提交的版本,并将之后的修改标记为未添加到缓存区的修改:
git reset <commit>
将HEAD重置到上一次提交的版本,并保留未提交的本地修改:
git reset --keep <commit>
最后附一张完整展示git流程的图片
参考:
http://www.codeceo.com/article/git-command-guide.html
http://www.ruanyifeng.com/blog/2014/06/git_remote.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2014-07-01 HTML5的学习--performance获取加载时间的工具