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

Git push to 2 remotes with 2 different branches

目前我开始从事一个项目,该项目之前由另一位开发人员使用 Git 作为版本控制。一旦我接管了这个项目,我就开始在不创建另一个分支的情况下对项目进行更改(更改 1)。

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

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

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

所以,我要做的是更改到具有更改 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-1 包含 aaaf027d138aed 。让我们回到那个改变之前的点:

$ 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

从那里您可以为 Change 2 进行提交,并在没有历史记录中的 Change 1 提交的情况下对它们执行您想要的操作。

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