bigtunacan
bigtunacan

Reputation: 4986

Git merge in a branch that was previously merged

I have run into a situation where a branch was merged into the master branch at some point previously and the changes from the branch are no longer in master. Possibly this was due to mishandling of merge conflicts, but at this point in time I'm not really sure.

The changes in this previously merged branch are needed in the master branch, but now if I try to merge the branch into master Git returns the message "Already up-to-date." since this branch was previously merged in. What is the best way to force a re-merging of this branch to master?

Upvotes: 4

Views: 1228

Answers (3)

jthill
jthill

Reputation: 60275

I think this'll do ya

mkdir alreadyapplied.patches
git format-patch -o alreadyapplied.patches master..alreadyapplied

and then

git checkout -b wip master
git am alreadapplied.patches
# (do whatever's necessary to deal with conflicts here)
# (rebase wip here if the conflict resolution has taken
# long enough that the wip branch has gotten behind master)
git checkout -B master wip
git branch -d wip    

Upvotes: 3

malgca
malgca

Reputation: 2947

Another method would be to get the commit ID of the branch before the merge and reset your HEAD to point to it, you could then make any fixes you need to make and re-merge that branch into master

i.e.

git reset (--hard/--soft/--mixed) _commit_ # where the _commit_ is the commit ID of the old branch
<work and make changes>
git commit -m "Made changes to commit before merging into master"
git checkout master 
git merge (--no-ff) otherBranch

Resetting your HEAD will effectively bring you back to how things were at that commit. Note that it may be dangerous using the --hard option, as it is destructive and will make your index and working area look exactly like it did at the stage of that commit (you will lose your other work).

Upvotes: 0

patthoyts
patthoyts

Reputation: 33203

Depending on how many commits were involved you could just cherry-pick each one in turn.

Upvotes: 0

Related Questions