1

If I have merge conflicts, how can I take all changes from HEAD for a single file? Not for the whole rebase, but for a single file?

Is this possible, or do I have to manually change it?

4 Answers 4

2

Following up on Codrin's answer, yes you can checkout a specific file from HEAD with git checkout HEAD -- path/to/file. However, in general, if you want to checkout a file from the branch being rebased or the branch you're rebasing onto there are the options --theirs and --ours.

When checking out paths from the index, check out stage #2 (ours) or #3 (theirs) for unmerged paths.

Note that during git rebase and git pull --rebase, ours and theirs may appear swapped; --ours gives the version from the branch the changes are rebased onto, while --theirs gives the version from the branch that holds your work that is being rebased.

So, if you need to checkout the version of a file on the branch being rebased, you can use

git checkout --theirs -- path/to/file 

While, if you need to checkout the version of a file on the branch you're rebasing onto, you can use

git checkout --ours -- path/to/file 

However, in general, you can use the syntax git checkout <commit_id> -- path/to/file to checkout a certain file version from whatever commit.

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

Comments

1

You can run git checkout HEAD -- path/to/your/file to reset the file to the state of HEAD. You can also replace HEAD with other identifiers if you want your file to come from other sources e.g. git checkout my-branch -- path/to/file or git checkout HEAD~1 -- path/to/file if you want it to come from 1 commit before HEAD.

Comments

0

if the problem is with the base branch

git checkout --ours -- path/to/file 

if the problem is with the incoming branch or the branch that is being merged

git checkout --theirs -- path/to/file 

Comments

0

When you use KDiff3 to resolve conflicts then what you ask for is trivial because KDiff3 has support for doing exactly that, overriding all automatic and manual conflict resolutions and pick one source specifically (on a per file basis):

KDiff3 menu screenshot

And regardless of this specific use case, you should always use a proper 3-way merge tool, like KDiff3, to resolve your conflicts. It is one of those things where once you start using it you will never go back to not using it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.