Git 常用命令汇总

导言

如果你是新手小白,完全不懂git,可以先看这一篇 github 详细教程

本文仅适用于对 git 操作已经有了一定掌握的用户,本文的目的在于将常用命令统一梳理记录,便于查阅。


干货

  • 克隆指定分支:git clone -b <branch_name> <git_repo_link>
  • git add的反操作:git reset HEAD [文件名, optional]
  • git commit的反操作:
    • 恢复到暂存区: git reset --soft HEAD^
    • 直接丢弃commit的修改:git reset --hard HEAD^
  • 本地覆盖远程:git push origin -f
  • 远程覆盖本地:git reset --hard origin/master && git pull
  • 拉取所有更新(但不同步):git fetch --all
  • 分支(branch):
    • 创建分支:git checkout -b <new_branch> [old_branch, optional,默认是当前分支]
    • 删除分支:git branch -d(or -D,强制删除) <branch_name>
    • 删除远程分支:git push --delete origin <branch_name>
    • 修改分支名:git branch -m <old_name> <new_name>
  • 标签(tag,tag其实也是一个 branch):
    • 创建 tag:git tag <tag_name>
    • 查看 tag 列表:git tag
    • 查看 tag 列表(有过滤条件):git tag -l "0.1.0*" (只查看0.1.0开头的tag)
    • 删除 tag:git tag -d <tag_name>
    • 将 tag 同步到远程:git push origin <tag_name>
  • git stash相关:
    • 将修改暂存:git stash
    • 将修改释放:git stash pop
    • 查看暂存的修改:git stash list
  • git merge & git rebase 相关,假定我们现在在一个从master checkout 出来的 feature 分支上,(https://joyohub.com/2020/04/06/git-rebase/):
    • git merge master:会在feature 分支的顶端新建个merge commit,将两个分支的修改merge在一起
      • 优点:非破坏性
      • 缺点:创建了新的merge 节点,会使feature分支的历史记录变的复杂,尤其是如果master更新频繁的话,会把feature的提交记录搞的很脏
    • 只merge特定的几个commit(cherry pick):
      • 只将master里的某个commit 合到 feature 里面来:git cherry-pick <commit_id of master>
      • 将master里的某一段commit合到 feature 里面来:git cherry-pick <start_commit_id>...<end_commit_id> (左开右闭,不包含start_commit_id
    • git rebase master:将 feature分支的改动移动到master分支的顶端,不会创建新commit
      • 优点:项目历史很清晰,消除了git merge 所需的不必要的合并节点
      • 缺点:虽然提交历史记录"看起来"是干净整洁的,其实可能会有合并错的风险
    • git rebase 合并多次提交记录:
      • git rebase -i HEAD~4, 进入vi编辑模式;
      • 在编辑模式里修改哪些commit需要被合并,哪些需要保留(pick:保留;squash:和前一个commit合并;drop:删除该commit)
    • git merge 和 git rebase 过程中的 incoming和current branch 分别是什么?
      • git merge:Merge the incoming changes into the current branch
      • git rebase:Rebase the current branch on top of the incoming changes
相关推荐
xlq223223 小时前
5.git
git
Yunzenn3 小时前
深度分析字节最新研究cola-DLM 第 07 章:推理流水线逐行拆解 —— 从 prompt 到生成文本
人工智能·驱动开发·深度学习·chatgpt·架构·prompt·github
努力努力再努力wz4 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github
Irissgwe5 小时前
四、Git 版本回退与撤销
git·git回退·git撤销
Coin_learning6 小时前
Git 小白教程:从安装上云端,一步不落。
git
JoyCong19986 小时前
远控届的隐形冠军ToDesk,告别延迟与卡顿,“无感”重塑远程体验
科技·github·电脑·远程工作·远程操作
峰向AI8 小时前
star 狂飙,把 Deepseek 用到极致的省钱神器
github
.wsy.8 小时前
Git教程(安装+流程+常用命令)
linux·git·gitee·intellij-idea
qq_356408669 小时前
Kubernetes 部署 GitLab Runner 及 Java CI/CD 实践指南
java·kubernetes·gitlab
一勺菠萝丶9 小时前
Git Tag 使用教程:如何打 Tag、切换 Tag、推送 Tag 和删除 Tag
大数据·git·elasticsearch