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
相关推荐
小稀土123几秒前
私有化研发协同系统并行期:旧 Jira 与新 Git 并存 6 条数据规则
git
云南新人和数采集团4 分钟前
DeepSeek Harness 开源贡献手记:从入门到合入主线
python·开源·github
Madison-No71 小时前
Git版本控制工具(二)
git
曹牧1 小时前
电子邮件服务器拒绝了您的用户名
github
wflynn2 小时前
GitHub 今日推荐|Snapintosh:用 ARM EL2 监控器骗过内核,让骁龙芯片笔记本跑 macOS
python·开源·github
钱栈up2 小时前
GitHub 热榜上手|ArtCraft:+13,572 star 的 AI 创作 IDE
人工智能·github
阿里嘎多学长4 小时前
2026-10-23 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Brookty4 小时前
Git 与 GitHub|第一阶段:仓库、版本历史与 Git 对象模型(上篇)
git·github
miofly4 小时前
GitHub 日榜趋势速报 | 2026-10-24
开源·github
怕浪猫12 小时前
外包和大厂的前端代码,差在凌晨三点
前端·面试·github