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
相关推荐
liuzheng66213 小时前
我给 Agent 测试注入了 72 种谎话,它抓住了 44 种
github
feasibility.9 小时前
一物降一物?AI逆向没有终局:REA拆App,BLA设套,LASIK清场
人工智能·爬虫·github·逆向·skill·rea
wno70410 小时前
常用的Git命令清单
大数据·git·elasticsearch
真上帝的左手1 天前
3. 代码管理-Git-进阶
git·源代码管理
真好啊又活了一天1 天前
服务器和虚机配置IPV6
运维·服务器·github
Cosolar1 天前
极简简历:开源免费,希望帮助到更多求职者
开源·github·敏捷开发
YIN_尹1 天前
【git原理与使用】git基本操作及底层原理(二)
大数据·git·elasticsearch
Cosolar1 天前
极简智库:快速搭建你的开源项目教程阅读平台,让 docs/ 目录被更多人看见
java·架构·github
敢敢是只喵i1 天前
false不等于没填:一次 AI 接入业务系统的SDK的修复记录
github
一条Go1 天前
历史告警归档:为什么"删掉主库行"是最后一步
后端·架构·github