Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge a specific file from one branch into another branch in Git

Tags:

git

I have 2 branches in a git repo, lets call them, dev and test. I have changes in a single file, somecode.js. Both branches have changes to somecode.js. The 2 branches have diverged significantly (but manageably) so a straight "merge" is insufficient.

I have tried http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/ but it doesn't merge the contents of both files. You basically just write over the file instead of actually merging the contents of the file.

I have also tried:

git checkout -b newbranch git checkout test somecode.js git commit -m "somecode changes from newbranch" git checkout dev git merge newbranch 

And

git checkout -m test somecode.js 

(I was really hopeful with the -m for merge, but it didn't appear to work for me...)

I thought I was close to what I needed, but then I realized that it just fast-forwarded the commit meaning it didn't merge, it wrote over the original file in test.

So, to reiterate, how can I merge a specific file from one branch into another branch without just writing over the file in the branch I am merging into using git.

like image 642
jeremy Avatar asked Dec 04 '12 20:12

jeremy


People also ask

Can I merge specific commit from another branch?

Go to either the git log or the GitHub UI and grab the unique commit hashes for each of the commits that you want. "Cherry pick" the commits you want into this branch. Run this command: git cherry-pick super-long-hash-here . That will pull just this commit into your current branch.

How do I merge a branch from another branch in GitHub?

Merging another branch into your project branchIn GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH.


1 Answers

I think you like to use

git checkout -p 

In your case

git checkout dev git checkout -p test somecode.js 

And you can interactively apply the diffs.

like image 83
Peter van der Does Avatar answered Sep 19 '22 00:09

Peter van der Does