喜之梁
使用 Git 管理项目代码:基础入门与多远程仓库工作流

使用 Git 管理项目代码:基础入门与多远程仓库工作流

从零掌握 Git 版本管理,涵盖用户配置、提交、分支与远程操作,并分享私有仓库 + 上游仓库多 remote 的协同工作流与提 PR 的方法。

前言

新的一年,祝大家新春快乐。

表情包

最近给博客做了一次彻底大升级,抛弃了hexo框架转向了astro框架。在拉取官方仓库到本地进行适配与迁移的过程中,我越来越能感受到Git的重要性。于是乎,这篇就来记录一下我用Git管理本地和远程仓库以及从上游仓库获取更新的方法经验。

Git基本操作

1. 配置用户信息

首次使用 Git 前,需要配置全局用户名和邮箱:

text
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"

2. 初始化仓库

在项目目录下执行:

text
git init

3. 常用操作命令

添加文件到暂存区

text
git add <文件名>        # 添加指定文件
git add .              # 添加所有文件

提交更改

text
git commit -m "提交说明"

查看状态

text
git status

查看提交历史

text
git log

4. 远程仓库操作

关联远程仓库

text
git remote add origin <远程仓库地址>

推送到远程仓库

text
git push -u origin main

从远程仓库拉取

text
git pull origin main

5. 分支管理

创建新分支

text
git branch <分支名>

切换分支

text
git checkout <分支名>
# 或
git switch <分支名>

合并分支

text
git merge <要合并的分支名>

6. 撤销操作

撤销工作区修改

text
git checkout -- <文件名>

撤销暂存区文件

text
git reset HEAD <文件名>

7. 忽略文件

创建 .gitignore文件,列出需要忽略的文件和文件夹:

text
node_modules/
*.log
.env

以上是 Git 常用的基本操作

实践与优化

我的博客使用的是Astro框架Firefly主题

我最开始的管理方案很简单,就是先fork官方仓库,然后clone下来到本地进行修改,之后建一个新的分支用来放修改后的代码,之后全部推送到github就完了。用这种方案管理代码流程简单,也方便给官方仓库提pr。

如果你只clone代码到本地,不推送到github上的话,那么我认为这种方法很不错。如果你想将代码提交到github进行备份并使用github的工作流的话,那么有一点我不太能接受——你fork下来的仓库是公开权限,这意味着任何人都能获取你的所有代码,包括可能填写在代码中的token、key之类的隐私内容

方案优化

有没有什么便于管理与更新代码,且没有安全风险,又能白嫖github算力,同时日后还能提pr贡献代码的方案呢?

有的兄弟有的

我们只需要给本地的git仓库增加多个远程仓库即可。一个是你的私有仓库origin,另一个是你的项目的上游官方仓库upstream,还有你的fork仓库(提PR,可选)

text
origin   → https://github.com/lmb666666/firefly-blog.git (私有仓库)
upstream → https://github.com/CuteLeaf/Firefly.git (原始上游仓库)
myfork   → https://github.com/lmb666666/Firefly.git (fork仓库)

具体步骤

第一步:在 GitHub 上创建私有仓库

  1. 打开 https://github.com/new
  2. 仓库名填写 firefly-blog(或你喜欢的名字)
  3. 选择 Private
  4. 不要勾选 "Add a README file" 等任何初始化选项
  5. 点击 Create repository

第二步:查看原始上游仓库地址

到GitHub上查看你的原始上游仓库地址。假设原始仓库是 https://github.com/CuteLeaf/Firefly.git(请根据实际情况替换)。

第三步:拉取原始上游仓库代码

bash
git clone https://github.com/CuteLeaf/Firefly.git
cd Firefly

第四步:重新配置本地remote

在你的项目目录下依次执行:

bash
# 1. 将当前的 origin(原始上游仓库)改名为 upstream
git remote rename origin upstream

# 2. 添加你的私有仓库作为新的 origin
git remote add origin https://github.com/lmb666666/firefly-blog.git

# 3. 验证配置
git remote -v

预期输出:

text
origin    https://github.com/lmb666666/firefly-blog.git (fetch)
origin    https://github.com/lmb666666/firefly-blog.git (push)
upstream  https://github.com/CuteLeaf/Firefly.git (fetch)
upstream  https://github.com/CuteLeaf/Firefly.git (push)

第五步:创建并切换到custom开发分支

为了不影响主分支,我们创建一个专门用于开发custom 分支。

bash
# 从当前分支(通常是 master/main)创建 custom 分支
git checkout -b custom
# 或者先创建再切换
# git branch custom
# git checkout custom

第六步:日常开发

custom 分支上进行日常的功能开发、bug修复等工作。

将修改的文件添加到暂存区,并提交到本地仓库。

bash
# 添加所有修改过的文件
git add .
# 或者只添加特定文件
# git add <file_name>

# 提交更改,并附上清晰的提交信息
git commit -m "Your descriptive commit message here"

第七步:推送所有分支到私有仓库

bash
# 推送 master 分支
git push -u origin master

# 推送 custom 分支(你的个人修改)
git push -u origin custom

日后同步上游更新

bash
# 获取上游最新代码
git fetch upstream

# 切到 master 分支,合并上游更新
git checkout master
git merge upstream/master
git push origin master

# 切到 custom 分支,合并 master 的更新
git checkout custom
git merge master
git push origin custom

流程图

mermaid
sequenceDiagram
    participant U as 上游仓库 (upstream)
    participant L as 本地仓库
    participant P as 私有仓库 (origin)

    Note over L: 日常在 custom 分支开发
    L->>L: git add & commit
    L->>P: git push origin custom

    Note over L: 同步上游更新
    U->>L: git fetch upstream
    L->>L: git checkout master
    L->>L: git merge upstream/master
    L->>P: git push origin master
    L->>L: git checkout custom
    L->>L: git merge master
    L->>P: git push origin custom

提交PR?贡献代码?

如果想要给上游官方仓库提交代码的话可以再添加一个你的fork仓库

第一步:在 GitHub 上 Fork 原始仓库

打开原始上游仓库的GitHub页面,点击右上角 "Fork" 按钮,创建你自己的 fork。

第二步:添加 fork 为新的 remote
bash
git remote add myfork https://github.com/lmb666666/Firefly.git

(fork 后的仓库地址通常是 https://github.com/你的用户名/Firefly.git

第三步:基于 upstream/master 创建功能分支
bash
git fetch upstream
git checkout -b feature/你的功能名称 upstream/master

第四步:把你想提交的改动 cherry-pick 或手动应用到功能分支上

bash
git cherry-pick <你的commit-hash>

第五步:推送到 fork

bash
git push myfork feature/你的功能名称

第六步:在 GitHub 上创建 PR 打开你的 fork 页面,点击 "Contribute" → "Open pull request",选择向 CuteLeaf/Fireflymaster 分支提交。


碎碎念

写完这篇博文已经2月18日了,寒假过了一大半了,好想让时间倒流。

悲伤

R3S旁路由网络环境配置
近期小记(1)

评论区

评论加载中...