1

If I want to pick just a single file from a feature branch and merge it into master, and also if a true merge isn't important, I'll use git checkout. However, if there are new changes to the same file in master, then the checkout operation will overwrite those changes.

What I want git checkout to do is basically merge the file's changes instead of simply overwrite them (which seems contrary to its purpose, I know). It should basically do these steps:

git diff <sha1>..<sha2> > changes.patch
git apply changes.patch

Sure, I could create an alias to do this but I'm specifically interested in seeing if git checkout can do this for me.

2 Answers 2

0

Mostly synthesizing the Grantovich's post and my comment to it…

Merges in Git, since they operate on commits, happen over whole snapshots of repositories which are represented by the commits on both (or more) sides of the merge, so no, basically you have no way to merge just a single file.

What you can do though it this:

  1. Make sure your work tree and the index are clean (possibly use git stash if you have changes you don't want to commit yet).
  2. Do git cherry-pick -n <commitish> where <commitish> contains the changes you want to pick. The -n or --no-commit command-line option prevents this command from automatically recording a new commit if there were no conflicts.
  3. Unstage changes in all the files except the one you wanted to merge. Use git reset for this.

On the other hand, the git read-tree (the command which performs merges in Git) manual mentions the git merge-one-file command which might just do what you want. Unfortunately it appears to very low-level and honestly I never tried to use it.

Sign up to request clarification or add additional context in comments.

Comments

0

git cherry-pick is the one-stop version of the two commands you posted. Given this tool exists, I don't think there is an invocation of git checkout that will do the same thing – it would seem like a real stretch of the tool's intended purpose anyway.

3 Comments

The problem with applying git cherry-pick for this case it that it doesn't understand separate files, so you can't bring just a single file's worth of changes using this command -- I mean not easily: you can call git cherry-pick --no-commit ... and then manually unstage all the changes except for that single file but this is messy. Probably less messy than git diff + git apply path but still...
The diff/apply example Robert provided also doesn't pick changes to individual files, so I assumed cherry-pick would be an acceptable solution. Cherry-picking individual changes within commits is just asking for headaches -- I'd rebase the feature branch so the changes I want are actually in a separate commit, then just cherry-pick that commit.
I can do git diff <sha1>..<sha2> -- path/file.cpp > file.patch. So yes I can patch a single file only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.