How to merge some (not all) files from one Git branch to another
Sometimes you need to merge specific from one branch to another in Git. This article show you how.
The first thing to do is to find a history of what you've changed in your new branch. To do this, given that your main branch is called master and your new branch is called screencast, issue the following git log command:
git log --name-status master..screencast
This will give you a list of all files you've changed in just the screencast branch.
Next, switch to your master branch:
git checkout master
Finally, checkout the files that you want to merge into the master branch. (Copy them from the output of the git log command, above. This is the manual/yucky bit – I copied the git log output into TextMate and created the file list there.)
git checkout screencast -- file1 file2 etc.
Commit your changes on the master branch and voila, you're done!
git commit -m "Merged this but not that from the screencast branch."
I use -- to separate the name of the branch from the names of the files since I initially got the following error:
fatal: ambiguous argument 'screencast': both revision and filename Use '--' to separate filenames from revisions
Git tracks files, not directories and this is why the git log step is necessary.
The git checkout tip comes courtesy of the article Git Tip: How to "Merge" Specific Files from Another Branch by Jason Rudolph, and the git log command for listing the changes for a specific branch is from the git log documentation.
The How to merge some (not all) files from one Git branch to another article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.Sometimes you need to merge specific from one branch to another in Git. This article show you how.
Add Your Comment