type
status
date
slug
summary
tags
category
titleIcon
password
icon
insider
🎮
结合项目实践总结反复用到的git命令,一作备忘录,二作带执行效果的git命令教程

拉取远程更新同步本地Fork

  • 主要用于同步NotionNext上游更新

一次性

  • 设置上游仓库地址
  • 查看当前已经设置的远程仓库
notion image

重复用

  • 拉取上游代码库更新(fetch只拉取更新,不直接修改本地分支,pull会直接进行本地文件修改)
  • (可选)切换到待push分支,以main为例

Rebase方案

  • Rebase如同字面意思,是将当前项目相对于上游旧基点所做的所有更改逐个重新应用到新的项目基点上,比如此处从“修复音乐组件”开始的修改commit实际早于“Merge pull request #3568”,现在将所有的修改全部移动到基于“#3568”这个位置重做所有修改。
notion image
  • 选择基点进行rebase
冲突时:
  • git会暂停rebase,需要自行重复以下步骤:
  • 解决冲突,可以使用IDE可视化选择左右任一份或混合二者
  • 添加修改文件(自行指定,一般在.gitignore写完筛出的文件后可以方便地使用.,应检查保证包含密钥等的环境文件均为灰色)
  • 继续rebase
结束后:
  • 注意提交的目的分支是origin自己的分支,因为rebase打乱了原本的commit历史(比如在v1时做的更新commit,rebase后成了v4时做的更新commit),需要force提交,覆盖历史

Merge方案

  • 使用merge,指明上游分支
  • 解决冲突
  • commit提交变更
  • 正常push

git提交后发现有误的回退

revert方案

  • 如果修改不是相邻的,而是隔了比较多的commits比较适合
  • revert如同字面意义,可以理解为一次通常的commit,只不过这次commit的内容是反转以前的某次提交修改
  • 如下为示例
notion image
notion image
  • -n 表示no-commit,不用的话会直接commit,后接commit hash值

reset方案

  • 适合直接丢弃最近的无效提交,回退到一个有意义的更新点
  • reset也是字面意思,重置到对应的提交节点,之后的更新全部丢弃
  • 参数接commit哈希值
之后就是重复commit push的流程
示例:
 
TODO:未整理
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image

合并master与github未合并的pr

pull方案

  • pull可以理解为直接fetch后马上进行merge,二合一,除非明确只有追加新文件不然不推荐
  • 先clone下来master分支
notion image
  • git pull拉取的对应pr
  • pull之后直接触发merge,填写merge的commit信息然后按照vim方式退出即可(a进入insert填写完信息后esc切回,:进入命令,输入wq回车保存退出即完成提交)
notion image
notion image
  • 提交完成,可以看到左下的历史
notion image

fetch后merge方案

  • 先把演示过pull的效果回退到pull之前
notion image
  • 回到原本的状态
notion image
  • git fetch,:后跟需要创建的分支名,拉取的内容会放在对应分支里,不影响当前分支
notion image
  • git branch检查分支状况
notion image
  • git checkout切换到作为合并后的分支(演示中目的为将主分支合并到pr-214上)
notion image
  • git merge,填写commit信息提交完成合并
notion image
notion image
notion image
  • 从更完整的历史可以看到主分支(jingyaogong)历史被合并进当前分支(mingkun)的历史
notion image
  • 这样不会影响到原本的主分支使用,可以随时checkout回去
notion image
  • 检查不会有notebook文件夹(pr分支内容)
notion image
  • 切回pr-214,带有notebook文件夹
notion image
notion image

主分支做了变更,希望将变更移动到新分支中,主分支维持稳定版本不动

  • 比如此处已经在master分支上做了修改,但考虑现有修改不一定保留(比如网络创新效果有可能比之前的差,需要保留一个稳定版本)
notion image
  • 可以先打tag标记重要版本(仅标记已经提交的版本),此处标记目前实现的四个创新点
notion image
  • git checkout -b 新分支(-b创建新分支)
  • 在新分支:
    • git add .
    • git commit -m
notion image
  • 可以给新的打个tag
notion image
  • 重新切回master,原本的变更自动复原(提交在了新分支上)
notion image
  • 如果是已经commit的情景,可以git checkout -b,git checkout回来,git reset
  • tag便于后续reset与checkout使用(在旧版上开新分支/查看旧版(不使用-b),重置reset到旧进度)
 

ssh使用局域网私仓

  • idea来源
[中英熟肉] 你不需要 GitHub:任何 SSH 服务器都是一个 Git 服务器 | Тsфdiиg_哔哩哔哩_bilibili
title: Тsфdiиg - Microsoft doesn't want you to know thisurl: https://x.com/tsoding/status/1983417259294109724channel: Тsфdiиgupload_timestamp: 2025-10-29T06:14:59Zduration: 3:31description:微软不想让你知, 视频播放量 61304、弹幕量 0、点赞数 1907、投硬币枚数 581、收藏人数 3035、转发人数 354, 视频作者 一摩尔炸鸡翅, 作者简介 科技/AI/开发/挖掘机/生活 译制视频只为自己摸鱼看,不盈利,偶尔也原创,相关视频:我花了十年的时间提高编程能力才意识到我和Jonathan Blow的差距 | Tsoding,【大学生扫盲课】1 Git、GitHub 和 Gitee 完整讲解:从基础到进阶功能,[中英熟肉] 三小时搞懂Asio?不,是三小时搞懂了为什么讨厌"现代"C++ | Tsoding Daily,直接使用git pull拉代码,被同事狠狠diss了!,为什么 Stripe 的 API 永不宕机: 基于日期的版本控制详解 | ByteMonk,[中英熟肉] 永远*不要使用 git pull | Philomatics,不依赖三方库和框架,直接操纵像素在 CPU 上跑 Shader | Tsoding,【研1基本功 别人不教的,那就我来】SSH+Git+Gitee+Vscode 学会了就是代码管理大师,[中英熟肉] 你不懂网络编程 | Tsoding Daily,吃瓜围观Linux内核开发人员花3小时为AppleXserve前面板编写USB驱动
[中英熟肉] 你不需要 GitHub:任何 SSH 服务器都是一个 Git 服务器 | Тsфdiиg_哔哩哔哩_bilibili
  • 如果只在远程修改,即本地不执行push,不需要bare仓库(git init —bare)
  • 格式git clone 用户@ip:仓库地址(文件夹路径)
notion image
notion image
  • 查看配置信息
    • 远程的url即为ssh相关信息
    • 检查author
notion image
notion image
 
 
 
 
 
git rebase
git checkout -b
tag静态,
 
 
 

git commit —amend —no-edit

不修改上次commit信息进行修改
notion image
 
  • 如果有冲突pull会不成功
notion image
  • 检查发现hash值不变,pull的合并失败
notion image
  • 如果有本地需要保留的变更(此处没有),应用rebase,不需要就git reset到远程的分支上
notion image
  • 检查hash值正确变更
notion image
 
 
 

git rebase

  • 网络结构的改变属于对应的分支,dev_plan的改变应属于master分支,应有:dev_plan.md变更提交到master分支,然后将网络改变的分支rebase其上
notion image
The Plan
  1. Stage only the file meant for master.
  1. Stash the rest (the models/ files) so they are safe and hidden.
  1. Switch to master, commit the plan, and bring arashi up to date.
  1. Restore the models/ files to arashi and commit them.

The Commands

Run these commands in order:
codeBash
# 1. Stage ONLY the file destined for master git add dev_plan.md # 2. Stash the unstaged files (models/*), but keep the staged one (dev_plan) # "-k" means keep-index, "-m" gives the stash a name so you recognize it git stash push -k -m "model changes for arashi" # 3. Switch to master. # (Since dev_plan.md is staged but not committed, it comes with you) git checkout master # 4. Commit the plan to master git commit -m "Update dev_plan.md" # 5. Switch back to arashi git checkout arashi # 6. Rebase arashi onto the new master # (This effectively fast-forwards arashi to include the new dev_plan commit) git rebase master # 7. Bring back your model changes git stash pop # 8. Commit the model changes to arashi git add models/u_net.py models/unet_flow.py git commit -m "Update U-Net model architecture"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
npm uninstall -g @ no version specified
git diff master...arashi
折腾博客表里站复盘理解Git原理
Loading...
2024-2025CamelliaV.

CamelliaV | Java;前端;AI;ACGN;