简体   繁体   中英

Merge a Git fork without changing master branch

My up-to-date branch is master (commit M). I had to create the branch old from an old commit (commit O1) and make some commits on old (commit O3). Now, I want to merge old on master to save it, but I want to keep the last commit on master as it is right now (commit M).

How can I do this?

There are two answers to that:

  • in git, a commit is identified by a hash, which will change if (among others) its parents change.

With this point of view: if you introduce something that merges together master and old , you can't have the same hash as your current commit on master , you will always create a new commit object .

  • regarding a commit's content , however, you can choose to store whatever content you see fit.

With this in mind: you can create a merge commit (one which will have two parents: your current master and old ), but which has the exact same content as your current master .

There actually is an option to git merge to do exactly that:

# from branch 'master' :
git checkout master

# create a merge commit, but ignore all changes that come from 'old' :
git merge -s ours old

Here is a link to the list of built-in merge strategies , here is the section about ours :

ours

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.

the Examples section contains:

  • Merge branch obsolete into the current branch, using ours merge strategy:
 $ git merge -s ours obsolete

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