Question

I have a master branch and a staging branch.

Now the usual workflow is that new changes happen in master then from staging I run a

git rebase master.

However due to the nature of my deployment infrastructure I've had to commit a whole bunch of fixes as I was debugging directly into the staging branch. What's the right way to bring these changes back into master so that my future git rebase will work well.

Was it helpful?

Solution

Rebase staging onto master. You'll have this:

         |
master - o
         |
         x
         |
         x - staging

You want this:

         |
         o
         |
         x
         |
master - x - staging

There's only one operation you need to do. Set master to point to the same commit that staging points at. There are umpteen ways to set master to point at the same commit as staging; here's one:

git checkout master
git reset staging

A simple pull should be equivalent as well. Since master is an ancestor of staging, merging staging will be a fast-forward, unless you have configured --no-ff somewhere, which we can detect with --ff-only:

git checkout master
git pull --ff-only staging

Edit: if there are commits you don't want to add back into master, make a new branch, then use rebase -i to keep just the commits that need to go back to master like this:

git checkout staging
git checkout  -b mypics

Yielding

         |
master - o
         |
         y
         |
mypics - x - staging

rebase mypics, dropping y, creating x':

git rebase -i master
# Drop the lines with commits you don't want to send back to master.

         |
master - o
         | \
         y  x' - mypics
         |
         x - staging

Then set master to point to the same commit as mypics, using reset or pull as described above. Then drop mypics:

git checkout master
git reset mypics
git branch -D mypics
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top