Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: how do I merge between branches while keeping some changesets exclusive to one branch?

There's a special place in hell for people who hardcode absolute paths and database credentials into multiple random places in web applications. Sadly, before they go to hell they're wreaking havoc on Earth. And we have to deal with their code.

I have to perform a few small changes to one of such web applications. I create a new branch features, and perform a global find & replace to update the paths and credentials to my local environment. I commit that. I also tag this as local.

I merrily leap into perilous hacking penitence, and after a perplexing hundred patches, I want to merge my features changes into the master branch, but I do not want the one local commit to be merged.

Onwards, I'll be merging back and forth between master and features, and I'd like local to stay put in features, and never ever show up in master.

Ideally, I'd like all this to happen magically, with as little funny parameters and whatnot as possible.

Is there a simple obvious way to do it that I'm missing?

I can think of a couple, but they all require me to remember that I don't want that commit. And that's definitely not my forte. Especially with such poorly hacked programs.

Failing that, I'm interested in more convoluted, manual-ish ways to handle the situation.

like image 203
kch Avatar asked Aug 17 '09 15:08

kch


People also ask

How do I merge changesets in another branch?

It allows you to merge(unshelve) a shelveset into a specific branch. Using Source Control Explorer in Visual Studio, get the history for any folder or file. Select one or more changesets in the list and right-click your selection. The context menu will now have a 'Merge...' option.

How do I merge two unrelated branches?

In fact, all you need to do to merge unrelated branches is to use the flag --allow-unrelated-histories . This tells Git to combine all the files and commits of both unrelated branches into one branch, as long as there are no file conflicts.

How do I merge specific branches?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.


1 Answers

My solution to this problem uses rebase rather than merge

Starting with a commit tree like this:

a-b-c <-- master
 \
  d <-- local
   \
    e-f-g <-- dev

$ git rebase --onto master local dev

       master 
       V 
   a-b-c-e'-f'-g' <-- dev
     \
      d <-- local

$ git checkout master

$ git merge dev

               master 
               V 
   a-b-c-e'-f'-g' <-- dev
     \
      d <-- local

$ git rebase --onto master master local

               master 
               V 
   a-b-c-e'-f'-g' <-- dev
                \
                 d' <-- local

$ git branch -f dev local

               master 
               V 
   a-b-c-e'-f'-g'
                \
                 d' <-- local
                 ^
                 dev
like image 140
Alex Farran Avatar answered Sep 23 '22 23:09

Alex Farran