Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Merge only single file from master into current branch [duplicate]

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

like image 714
Ali Hussain Khan Avatar asked Oct 07 '16 11:10

Ali Hussain Khan


1 Answers

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
like image 108
Jeff Puckett Avatar answered Nov 14 '22 23:11

Jeff Puckett