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

时间:2018-01-05 10:57:07

标签: git branch git-branch

目前我开始研究一个以前由其他开发人员使用Git作为版本控制的项目。一旦我接管了项目,然后我开始在项目中进行更改(更改1)而不创建另一个分支。

之后,我才知道Change 1不会很快推向生产,而是我必须推送到另一台转发服务器。同时,有一些变化需要立即更改并立即推送到服务器(变更2),但我已经为变更1提交了几次。变更1的提交已经被推送到远程存储库。 / p>

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

1 个答案:

答案 0 :(得分:1)

解决此问题的一种方法称为"重写历史记录",这意味着更改您迄今为止所做的提交历史记录。但是,由于您已经推送了针对Change 1的提交,这通常是不可取的,因为如果其他人在这些远程存储库上是最新的,那么它们将具有"历史差异"在他们的拉动,需要手动修复。

所以,我要做的是改变到包含Change 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

从那里你可以为Change 2提交你的提交,并且在没有历史记录中的Change 1提交的情况下用它们做你想做的事。

如果您已将Change 1提交合并到master(或直接将更改更改为master),并且您希望从此分支中删除Change 1,那么您需要将master重命名为某个内容与master-with-change-1一样,如上所述回退,然后使用master从新点创建新的-b。这可能需要与回购的其他用户进行一些协调,因为您正在删除他们可能已经撤回的历史记录提交。