Git fetch and merge ONE file only


Just getting started with git and GitHub. I am mostly working on my local copy (only master branch for now), pushing to origin occasionally. But today I had bunch of local commits (not pushed) and I updated on the GitHub web site resulting in two different versions.

I figured out

git fetch


git diff master origin/master

I could see new files added and README with all deletions … I was hoping to do something like

git merge origin/master/

Is this possible?

On a related note:
What is the difference between “git diff master origin/master” and “git diff origin/master master”? They looked orthogonal to each other … deletions became additions and vice versa.

At any rate I ended up coping REDAME additions from web, doing

git push --force

Editing on-line

git fetch && git merge origin/master

It worked but it was awkward …


I’m not sure what you mean by that, could you explain? From your description up to that point I would’ve assumed that git merge origin/master would have been the way to go to include the change made via web in your local branch.

That’s exactly what they are. When called on two trees (in this case branches) git diff compares the files in each tree. What is “added” or “removed” depends on which way around you look at them.

Thank you for a quick reply.

The issue I created was that I had changes in BOTH local and origin repos. The local changes were significantly more extensive (new files and other edits to almost all files). The origin edit was impulsive one: updating README file.

The task I was trying to achieve:
update local README from origin/master AND keep all local changes.

When I did

git fetch && git merge origin/master

it showed deletions to all my local files (disastrous) and addition to README (desirable).

Question remains: can I update a single file without touching anything else?

Thanks you.

1 Like

Ah, I think there might be a misunderstanding about git diff here. A diff does not show you what happens if you would merge, it just shows you the differences between the branches as they currently are.

A merge would try to bring the changes on both branches since the last common ancestor commit together: Both your new local files, and the README change in the GitHub repository. So yes, it sounds like a git merge origin/master would have gotten you what you wanted. :slight_smile:

A problem that can happen when merging is a merge conflict, e.g. if the same section of a file has been changed on both branches, or a file has been changed on one branch and deleted on the other. In that case git will notify you about the conflict and you’ll have to resolve it manually, deciding what you want to keep.

I recommend reading the Basic Branching and Merging chapter of the Git Book to get a better idea of what merging does.