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

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

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

前言

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

表情包

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

Git 基本操作

1. 配置用户信息

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

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

2. 初始化仓库

在项目目录下执行:

$
git init

3. 常用操作命令

添加文件到暂存区

$
git add <文件名>
$
git add .

提交更改

$
git commit -m 提交说明

查看状态

$
git status

查看提交历史

$
git log

4. 远程仓库操作

关联远程仓库

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

推送到远程仓库

$
git push -u origin main

从远程仓库拉取

$
git pull origin main

5. 分支管理

创建新分支

$
git branch <分支名>

切换分支

$
git checkout <分支名>

或者使用新版命令:

$
git switch <分支名>

合并分支

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

6. 撤销操作

撤销工作区修改

$
git checkout -- <文件名>

撤销暂存区文件

$
git reset HEAD <文件名>

7. 忽略文件

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

text
node_modules/
*.log
.env

以上是 Git 常用的基本操作

实践与优化

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

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

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

安全提醒

fork 的仓库默认是公开的,代码中残留的 token、key 等隐私内容会被任何人看到,尽量不要把包含敏感信息的仓库以 fork 形式公开。

方案优化

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

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

  • 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(请根据实际情况替换)。

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

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

第四步:重新配置本地 remote

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

$
git remote rename origin upstream
$
git remote add origin https://github.com/lmb666666/firefly-blog.git
$
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 分支。

$
git checkout -b custom

另外如果想先拉取最新的上游代码再开发,可以先用 git fetch upstream 获取更新。

第六步:日常开发

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

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

$
git add .

或者只添加特定文件:

$
git add <文件>

提交更改,并附上清晰的提交信息:

$
git commit -m 提交说明

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

$
git push -u origin master
$
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
$
git remote add myfork https://github.com/lmb666666/Firefly.git

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

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

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

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

第五步:推送到 fork

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

第六步:在 GitHub 上创建 PR

打开你的 fork 页面,点击 "Contribute" → "Open pull request",选择向 CuteLeaf/Firefly 的 master 分支提交。


结尾

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

悲伤

R3S旁路由网络环境配置:使用OpenClahs轻松解决DNS泄露
近期小记(1)

评论区

评论加载中...