简体   繁体   中英

Git push to 2 remotes with 2 different branches

Currently I start working on a project which previously worked by another developer which is using Git as version control. Once I take over the project, then I start making changes (Change 1) into the project without creating another branch.

After that I just knew that Change 1 won't be pushing to production so soon, instead I have to push to another staging server instead. At the same time there is some changes that need to change urgently and push to live server immediately (Change 2), but I had already committed a few times for Change 1. The commits for Change 1 have already been pushed to remote repositories.

So my question is, how can I move all my Change 1 to a branch and keep the master branch the same as production?

One approach to fixing this problem is known as "rewriting history", which means changing the commit history that you have made this far. However, since you have already pushed your commits for Change 1, this is generally not advisable, since if anyone else is up-to-date on those remote repositories, they will have a "history divergence" in their pull, which needs manual fixing.

So, what I would do is change to the branch that has Change 1, which we'll call change-1 . Let's say you do this (using my current repo as an example):

$ 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)

These commits are in reverse time order. So, let's say that change-1 incorporates aaaf027 to d138aed inclusive. Let's wind back to the point before that change:

$ 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

Great. From this point you can create a new branch, like so:

git checkout -b before-change-1

From there you can make your commits for Change 2, and do what you want with them without the Change 1 commits in the history.

If you have already merged Change 1 commits to master (or made the changes to master directly) and you wish to remove Change 1 from this branch, then you would need to rename master to something like master-with-change-1 , wind back as above, and then create a new master from the new point with -b . This may need some co-ordination with other users of the repo, since you are removing from history commits they may already have pulled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM