7

I have just started using git. I created branch-A from master. I have created the file abc.txt in branch-A and merged branch-A into master successfully. Now I am working same file in branch-A and want to merge master's (which abc.txt) file into branch-A (abc.txt) file. I tried "git checkout master abc.txt" but it replaces branch-A file with master's abc.txt.

I know "git merge master" will do the work, but if someone can show me how to merge single file ONLY into current branch.

Thanks in advance

Matthew Hallatt
  • 1,310
  • 12
  • 24
Ali Hussain Khan
  • 111
  • 1
  • 1
  • 9
  • thats not a duplicate. he is asking about merging a single file not a single commit – Efrat Levitan Jul 18 '19 at 07:13
  • @LazyBadger please reopen; this is not a duplicate. It's about the very specific case of doing a pseudo-merge of a single file from another commit. – matt May 02 '21 at 21:32

2 Answers2

10

Firstly, make sure your working directory is clean.

Then as you've discovered...

git checkout master abc.txt

...will replace the whole file, but what you might not have realized is that now you could make a patch from those differences if you reset. So after checking out the file:

git reset --mixed
git add --patch abc.txt

Now you can selectively choose the changes you want to include, commit, and then reset hard to arrive at that final state by discarding the unwanted differences.

git commit -m "merge changes to abc.txt"
git reset --hard
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • Thanks Jeff for your answer. So you are saying I should start with: 1- git reset 2- git add -p abc.txt Do I need to "git checkout master abc.txt" first? – Ali Hussain Khan Oct 07 '16 at 12:00
  • @AliHussainKhan yes, `checkout` the file first, then you will notice that it's been staged, but not committed. So if you `reset --mixed` this will keep all the changes in your working directory and you'll be able to `add --patch` – Jeff Puckett Oct 07 '16 at 13:47
  • Actually you don’t need the working tree to be clean. Just commit the one file so you can reset it in the second step. – matt May 02 '21 at 03:33
0

Is there a reason you only want this single file to be included in branch-A? It sounds to me more like you want to do a rebase of branch-A on top of master, so all changes that have happened on master will be included in branch-A.

Here's the documentation for rebasing: https://git-scm.com/docs/git-rebase

Basically, it will take you from:

      A---B---C branch-A
     /
D---E---F---G master

to

              A'--B'--C' branch-A
             /
D---E---F---G master

by checking out branch-A and calling

git rebase master

This means that whatever changes happened to abc.txt in commits E, F and G on master will be reflected in your commits on branch-A.

Rebase simply checks out master and applies the commits from branch-A one by one.

Matthew Hallatt
  • 1,310
  • 12
  • 24
  • 1
    Thanks @Mathew for your answer. Yes there is a reason. When I create pull request I see conflicts and I want to resolve it by merging the same ONLY into my branch not whole master branch into my branch. – Ali Hussain Khan Oct 07 '16 at 11:31
  • From my understanding of git, it is not possible to take the changes made to one file on another branch and apply them to your current branch. If you do a rebase like I've described, you will be forced to resolve any conflicts along the way. Then when you create your pull request, there should be no conflicts. The chances are you will just want to call `git checkout --ours abc.text` if a conflict appears to ensure that you take the changes made to the file on the master branch. – Matthew Hallatt Oct 07 '16 at 11:34
  • Yes, that makes sense. But I don't want to include other files into my branch from master except abc.txt – Ali Hussain Khan Oct 07 '16 at 11:40
  • But when you merge in to master, you're going to end up with those other changes anyway - why would you not want them now so you can ensure nothing is going to break? – Matthew Hallatt Oct 07 '16 at 11:41
  • Lets say, there is another branch called "middle" which I use to do QA testing and after QA testing passed I create pull request to master. Now there is a scenario that my work is ready for QA testing and I want to create pull request to "middle" branch and found conflict in one file xyz.txt. Now I don't want to merge whole "middle" branch into my branch but xyz.txt file. Thats why I was looking for a solution to fetch and merge single file – Ali Hussain Khan Oct 07 '16 at 11:50