0
$ cd my/repo
$ git checkout develop
$ git fetch origin
$ git checkout -b branch origin/branch
$ git pull origin HEAD
CONFLICT (content): Merge conflict in folder/file.ext
Auto-merging folder/file.ext
Automatic merge failed; fix conflicts and then commit the result.

I am really confused as to why my branch wasn't already up to date. I thought git fetch grabs all the remote branches with their delta info from origin.

Even after going back to develop, deleting branch and repeating the steps, the problem is repeated. What am I doing wrong? I just want to be sure to take --theirs, but even after I do

$ git checkout --theirs folder/file.ext

git status shows folder/file.ext has been modified and wants to me to commit the changes. I don't understand why there are changes to be committed at all. I just want origin/branch exactly as it is at origin, locally, in a tracking branch named the same.

How is this achieved?

phpguru
  • 2,351
  • 2
  • 23
  • 33
  • try using just `git pull` without any arguments and see if you get better results – Andrew C Feb 10 '15 at 23:51
  • 1
    Using `git pull origin HEAD` instructs the `pull` script to: (1) fetch from origin; (2) obtain the latest commit on whatever *origin*'s `HEAD` is (probably the same as your `origin/master`); (3) merge that, i.e., probably merge `origin/master` into your `branch`. That's clearly not what you intended. – torek Feb 11 '15 at 01:07
  • Can you try repeating the steps you have above, but replacing the final command with `git pull origin branch` instead of what you have? – Tim Biegeleisen Feb 11 '15 at 01:59
  • When I have a new local branch and `git push origin HEAD` the remote branch is created & tracked, so I assumed that `git pull origin HEAD` was the same idea in reverse. When I try instead `git pull origin branch` I get the exact same conflicts, forcing me to diff, resolve conflicts and commit. I still don't understand why I cannot just get the exact contents of the remote branch locally. – phpguru Feb 11 '15 at 16:23
  • @TimBiegeleisen it doesn't matter if I use HEAD or branchname; the result is the same, a conflict and merge commit. See my new solution below. Develop has progressed from other changes that I do not yet want to merge. Basically I want to start where I left off on branchname and push the merge down the road. – phpguru Feb 13 '15 at 17:38
  • How do I get a downvote for a question? – phpguru Mar 04 '15 at 15:52

2 Answers2

0

If you want to make sure your remote and local branches of the same name are identical, you need to fetch the latest changes(if any) from the remote or push the latest commits in your local(if any) to the remote. To do that, instead of using HEAD, you need to specify the branch name in the pull command git pull origin branch_name

If you want to make sure there is no extra commit while pulling the latest code that is available in the remote branch, you should probably fix the conflicts that may occur after you pull the remote branch onto your local branch. You can do that by using the git diff command. However, resolving conflicts is made easier by Git, but if you are particular in not having an extra commit, then you can fix the conflicting lines and then pull.

"I thought git fetch grabs all the remote branches with their delta info from origin" - Yes you're right, but it stops at fetching the info and does not merge those changes unless you do a git merge or a git pull (= git fetch + git merge).

Raghav
  • 570
  • 1
  • 13
  • 24
  • I know that `git pull` = `git fetch` + `git merge`. When I perform a `git pull origin branch_name` it is not clear to me why I am asked to resolve conflicts and commit. I want to work on `branch_name` locally from the exact state it is at origin without creating a new merge commit and not sure why git forces me to create a merge commit. – phpguru Feb 11 '15 at 16:26
  • If you resolve conflicts, you have to create a commit to record the changes you have made. In case you don't have any conflicts, you can choose to [omit the pointless merge commit by using git rebase instead](http://stackoverflow.com/questions/10157702/why-am-i-creating-a-pointless-merge-commit) – Raghav Feb 12 '15 at 02:09
0

I think I figured out what I want. First, inspect the git log or browse to the remote branch_name on Github and find the latest commit in the branch, say abc123... is the hash.

$ git co master
$ git fetch
$ git pull origin master
$ # remove local branch if you have one, losing changes if any!
$ git branch -D branch_name
$ git checkout abc123
Note: checking out 'abc123...'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

HEAD is now at abc123... commit message here

$ git checkout -b branch_name
$ git branch --set-upstream-to=origin/branch_name
Branch branch_name set up to track remote branch branch_name from origin by rebasing
$ git pull origin/branch_name

I now have a local branch_name tracking origin/branch_name at the exact point in time (same commit hash exactly) as origin/branch_name without a diff/merge/rebase and without an additional merge commit getting pushed to my remote repo in order to continue.

phpguru
  • 2,351
  • 2
  • 23
  • 33