The key to understanding this is to recognize a couple of facts:
- Files aren't in branches. Files are in commits.
- A merge commit is a commit.
- Each commit stores a full snapshot of all files.
- The files that are in a new commit you make—including a new merge commit—comes from the files that are in Git's index at the time you run
git commit
or git merge --continue
.
Hence, if git merge
is going to make a commit that you don't like, you can do one of two things:
- Allow it to make the commit you don't like, then add a second commit to fix it up.
- Or: make sure
git merge
doesn't make the commit yet, using git merge --no-commit
. The merge will stop before committing. Then, adjust the index to contain the files (and versions of those files) that you want to appear in the commit.
If your question is about the mechanics of getting some file(s) from some commit(s), to put into the next commit, that part is super-trivial, once you understand what a Git commit is and does. For instance, suppose you do this:
git checkout master
git checkout -b otherbranch
git rm -r sounds
git commit
git checkout master
git merge otherbranch
This actually makes the merge commit, assuming there are no merge conflicts (there won't be), and the new merge commit lacks the sounds/
files. But why is this a big deal? You want the sounds files back, just grab them from the commit that comes one step before the new merge commit, along the "main" line:
git checkout master~1 -- sounds/
and then make a new commit:
git commit
that extends master
by one more commit again. The new commit has the same files that the merge
commit has, except that it also has all the sounds/
files too.
Making the merge while retaining the files produces what some call an evil merge (see Evil merges in git?), but it's equally simple. Replace the git merge
step with:
git merge --no-commit otherbranch
git checkout HEAD -- sounds/
git merge --continue
and you make a merge commit that doesn't delete the sounds/
files, because it copied all the sounds/
files from the commit that was the tip of master
just a moment ago, before you made the new merge commit.