SteppingHat
SteppingHat

Reputation: 1362

Git merge updates to a past branch

I have this project I'm working on where I'm building a framework for some content. During development I had a bunch of demo content so I can see what I'm doing essentially. This was all being done in the dev branch.

At some point I branched off the dev branch and created a new branch called demo. Since the branch the dev branch has had all demo content removed. Files were deleted and existing files were also modified to leave a vanilla branch for development to continue.

Now I'm at the stage where I want to work on demo content again. However because the demo branch is outdated, I intend on merging the updates from dev -> demo.

The problem at hand is that if I were to carry on with the merge, the deleted files will automatically be carried across as it won't see a conflict and some of the files that were also updated to remove the demo content will carry across.

In a nutshell, I want to merge the dev into demo and not end up deleting anything as part of it.

            v dev - origin/HEAD
o - o - o - o - ...
     \       \
      o - - - o <- proposed update
      ^ demo

That's essentially what I want to do, extremely basic, but I can't think of a way to do it without destroying anything in the process.

Upvotes: 2

Views: 61

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

You can use a custom Git merge driver, q.v. this SO question. The meat and potatoes of this method is creating a script which does the same thing which Git usually does during a merge, but then always returns 1 at the end of the script, triggering a manual conflict resolution:

#!/bin/bash
git merge-file "${1}" "${2}" "${3}"
exit 1

There are a few other configuration steps, but the jist is to force Git to a manual resolution.

An alternative to this approach would be to simply do the normal merge and then restore the files afterwards. You could checkout the deleted files via:

git checkout <SHA-1> path/to/deleted/file

where <SHA-1> is the commit hash from the previous commit in your demo branch, and path/to/deleted/file is the path to the deleted file. After you have finished, you can make a new commit for the restored files.

Upvotes: 1

Related Questions