I have 2 files which got committed in a certain branch and I would like to merge only a single file from the commit changeset. How should I do that?
2 Answers
There is no option to git-merge that takes a list of path(s) to merge.
Instead, you will have to do this manually, by
- instructing git to stop after the merge has taken place but before committing;
- resetting the second file to the contents of
HEAD; - placing those contents on disk, which will result in only merging the first file.
For example, if the commit in question is 0123abc and the file you wish to omit from the merge is file2.txt:
git merge --no-commit --no-ff 0123abc git reset HEAD file2.txt git checkout -- file2.txt Comments
Assuming that what you're really after for one of your files to look like it does in the changed branch, and for the other file to look like it does in the current branch, then you can pull the changes from one branch without actually creating a merge. To do this interactively, you can use git rebase -i.
You can short-cut the interactive process by using git stash on the file you want to merge, git checkout to the unmodified branch, then git stash pop.
If you want to end up with one of the changes you made in the current branch, and for the other change to disappear, then you can manually revert the unwanted changes and then git commit --amend to edit your original commit so that only the other file remains changed.
There are more advanced options availble, using blobs and some of git's low-level merging powers, but they're to be avoided without great git-fu.