git操作
语法
git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
选项
add 将文件内容添加到索引
bisect 通过二进制查找引入错误的更改
branch 列出,创建或删除分支
checkout 检查分支或路径到工作树
clone 将存储库克隆到新目录中
commit 将更改记录到存储库
diff 显示提交,提交和工作树等之间的更改
fetch 从另一个存储库下载对象和引用
grep 打印匹配图案的行
init 创建一个空的Git仓库或重新初始化一个现有的
log 显示提交日志
merge 加入两个或更多的开发历史
mv 移动或重命名文件,目录或符号链接
pull 从另一个存储库或本地分支获取并合并
push 更新远程引用以及相关对象
rebase 转发端口本地提交到更新的上游头
reset 将当前HEAD复位到指定状态
rm 从工作树和索引中删除文件
show 显示各种类型的对象
status 显示工作树状态
tag 创建,列出,删除或验证使用GPG签名的标签对象
示例
init
git init #初始化
status
git status #获取状态
add
git add file # .或*代表全部添加
git rm --cached <added_file_to_undo> # 在commit之前撤销git add操作
git reset head # 好像比上面git rm --cached更方便
commit
git commit -m "message" #此处注意乱码
remote
git remote add origin git@github.com:JSLite/test.git #添加源
push
git push -u origin master # push同事设置默认跟踪分支
git push origin master
git push -f origin master # 强制推送文件,缩写 -f(全写--force)
clone
git clone http[s]://example.com/path/to/repo.git/
git clone ssh://example.com/path/to/repo.git/
git clone git://example.com/path/to/repo.git/
git clone /opt/git/project.git
git clone file:///opt/git/project.git
git clone ftp[s]://example.com/path/to/repo.git/
git clone rsync://example.com/path/to/repo.git/
reset
git reset HEAD * # 取消已经暂存的文件
git reset --mixed HEAD * # 同上
git reset --soft HEAD * # 重置到指定状态,不会修改索引区和工作树
git reset --hard HEAD * # 重置到指定状态,会修改索引区和工作树
git reset -- files * # 重置index区文件
revert
git revert HEAD # 撤销前一次操作
git revert HEAD~ # 撤销前前一次操作
git revert commit # 撤销指定操作
checkout
git checkout -- file # 取消对文件的修改(从暂存区——覆盖worktree file)
git checkout branch|tag|commit -- file_name # 从仓库取出file覆盖当前分支
git checkout HEAD~1 [文件] # 将会更新 working directory 去匹配某次 commit
git checkout -- . # 从暂存区取出文件覆盖工作区
git checkout -b gh-pages 0c304c9 # 这个表示 从当前分支 commit 哈希值为 0c304c9 的节点,分一个新的分支gh-pages出来,并切换到 gh-pages
diff
git diff file # 查看指定文件的差异
git diff --stat # 查看简单的diff结果
git diff # 比较Worktree和Index之间的差异
git diff --cached # 比较Index和HEAD之间的差异
git diff HEAD # 比较Worktree和HEAD之间的差异
git diff branch # 比较Worktree和branch之间的差异
git diff branch1 branch2 # 比较两次分支之间的差异
git diff commit commit # 比较两次提交之间的差异
git diff master..test # 上面这条命令只显示两个分支间的差异
git diff master...test # 你想找出‘master’,‘test’的共有 父分支和'test'分支之间的差异,你用3个‘.'来取代前面的两个'.'
stash
git stash # 将工作区现场(已跟踪文件)储藏起来,等以后恢复后继续工作。
git stash list # 查看保存的工作现场
git stash apply # 恢复工作现场
git stash drop # 删除stash内容
git stash pop # 恢复的同时直接删除stash内容
git stash apply stash@{0} # 恢复指定的工作现场,当你保存了不只一份工作现场时。
merge
git merge branchName # 合并分支 - 将分支branchName和当前所在分支合并
git merge origin/master # 在本地分支上合并远程分支。
git rebase origin/master # 在本地分支上合并远程分支。
git merge test # 将test分支合并到当前分支
tag
git tag -a v0.1 -m 'my version 1.4' # 新建带注释标签
git push origin --tags # 一次性推送所有分支
git push origin v1.5 # 推送单个tag到orgin源上
git tag -v v1.4.2.1 # 验证标签,验证已经签署的标签
git show v1.5 # 看到对应的 GPG 签
git tag # 列出现有标签
git tag v0gi.1 # 新建标签
git checkout tagname # 切换到标签
git tag -d v0.1 # 删除标签
git push origin :refs/tags/v0.1 # 删除远程标签
git pull --all # 获取远程所有内容包括tag
git --git-dir='<绝对地址>/.git' describe --tags HEAD # 查看本地版本信息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术