How to merge some (not all) files from one Git branch to another

You're working on a new Git branch and you notice that you mistakenly committed some changes that need to be in your master branch. How do you find and merge only the files that have changed?

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."

Notes

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.