Git 推送到 2 个具有 2 个不同分支的遥控器



目前,我开始处理一个以前由另一个开发人员使用Git作为版本控制的项目。一旦我接管了项目,我就开始对项目进行更改(更改 1),而无需创建另一个分支。

在那之后,我只知道 Change 1 不会这么快推送到生产环境,而是必须推送到另一个暂存服务器。同时有一些更改需要紧急更改并立即推送到实时服务器(更改 2),但我已经为更改 1 提交了几次。更改 1 的提交已推送到远程存储库。

所以我的问题是,如何将所有更改 1 移动到一个分支并保持主分支与生产相同?

解决此问题的一种方法称为"重写历史记录",这意味着更改到目前为止所做的提交历史记录。但是,由于您已经推送了更改 1 的提交,因此通常不建议这样做,因为如果其他人在这些远程存储库上是最新的,他们的拉取中将出现"历史分歧",这需要手动修复。

因此,我要做的是更改为具有更改 1 的分支,我们将其称为change-1。假设您这样做(以我当前的存储库为例):

$ git branch change-1
$ git log -n 10 --oneline
d138aed Report success from deploying the final config
fdc9ce3 Rename some templates
2fa1bb3 Continuing work on deploy progress/failure handling
696e470 Add some UI notices to report deploy queue progress
e72b151 Add skeleton JS file for deploy queue stuff (WIP)
0145a1c Make a start on requesting a final settings FTP/SSH send
aaaf027 Reflect Emails To Send readiness in tab
e84510c Use real system messages, implement screen rendering & removal
e6a8a4f Add a dummy primary announcement, add some logic to receive it
f7df9cd Move the PID location (prior to making container r/o)

这些提交按相反的时间顺序排列。因此,假设change-1aaaf027纳入d138aed包容性。让我们回到更改之前的观点:

$ git checkout `e84510c`
Note: checking out 'e84510c'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at e84510c... Use real system messages, implement screen rendering & removal

伟大。从这一点开始,您可以创建一个新分支,如下所示:

git checkout -b before-change-1

从那里,您可以为更改 2 进行提交,并在历史记录中不使用更改 1 提交来执行所需的操作。

如果您已经将更改 1 提交合并到master(或直接对 master 进行了更改),并且您希望从此分支中删除更改 1,则需要将master重命名为类似master-with-change-1,按上述方式回退,然后使用-b从新点创建一个新master。这可能需要与存储库的其他用户进行一些协调,因为您要从历史记录中删除他们可能已经拉取的提交。

最新更新