1

I'm on a my master branch which is a perfect clean (but git presents it that it is ahead of origin/master by 14 commits). When I try to pull a different branch from origin (Eagle) git wants me to merge a few files.

This is not what I expect: When pulling a branch it should import the remote branch and leave the current HEAD untouched and not bother me with merge conflicts. In my perception The 2 branches live apart but peacefully from eachother, without any conflicts. But this perception holds no longer.

What causes these conflicts and how to restore 2 different branches without merging?

Below a transcript of the git session on commandline.

HEAD is now at 34e47ab ISS-652 misspelled MockBean in test/ApplicationContext
build@jenkins/p-project/workspace> git status
# On branch master
# Your branch is ahead of 'origin/master' by 14 commits.
#
nothing to commit (working directory clean)
build@jenkins/p-project/workspace> git log -5
.. Same as origin
build@jenkins/p-project/workspace> git push origin master
Everything up-to-date
build@jenkins/p-project/workspace> git status
# On branch master
# Your branch is ahead of 'origin/master' by 14 commits.
#
nothing to commit (working directory clean)
build@jenkins/p-project/workspace> git pull origin Eagle
From ssh://git@stash.europe.intranet:7999/hig/p-project-container
 * branch            Eagle     -> FETCH_HEAD
Auto-merged pom.xml
CONFLICT (content): Merge conflict in pom.xml
Auto-merged p-project-client/pom.xml
CONFLICT (content): Merge conflict in p-project-client/pom.xml
Auto-merged p-project-container-conf/pom.xml
CONFLICT (content): Merge conflict in p-project-container-conf/pom.xml
Auto-merged p-project-container-conf/src/main/resources/dpl/P-Project_Container.xml
CONFLICT (content): Merge conflict in p-project-container-conf/src/main/resources/dpl/P-Project_Container.xml
Auto-merged p-project-container-conf/src/main/resources/was/P-Project_Container-app-env.cfg
Auto-merged p-project-container-ear/pom.xml
CONFLICT (content): Merge conflict in p-project-container-ear/pom.xml
Auto-merged p-project-container-filters/pom.xml
CONFLICT (content): Merge conflict in p-project-container-filters/pom.xml
Auto-merged p-project-container-ldap/pom.xml
CONFLICT (content): Merge conflict in p-project-container-ldap/pom.xml
Auto-merged p-project-container-tomcattest/pom.xml
CONFLICT (content): Merge conflict in p-project-container-tomcattest/pom.xml
Auto-merged p-project-container-web/pom.xml
CONFLICT (content): Merge conflict in p-project-container-web/pom.xml
Automatic merge failed; fix conflicts and then commit the result.
build@jenkins/p-project/workspace>
dr jerry
  • 9,768
  • 24
  • 79
  • 122
  • 1
    Nothing you've shown us here strikes me as being necessarily wrong or inexplicable. Why would you think that doing a `git pull` would _not_ potentially cause merge conflicts? – Tim Biegeleisen Nov 24 '15 at 09:00
  • 1
    If you have 2 branches locally you should maybe move the head to the branch you want to pull. Git checkout Eagle – Thomas Nov 24 '15 at 09:03

2 Answers2

1

I will provide an explanation of what you did, after which point it will be clear that the results you got can be expected.

You made 14 commits to the master branch since the point when you last synched up with the remote repository:

git commit -m 'You did something'   # you did this 14 times

Next you pushed your work to the remote:

git push origin master

Note carefully that git status is still showing your local branch as being 14 commits ahead of master. Your local branch is ahead of the old stale local tracking branch, but is completely in sync with the remote master.

Next, you did this:

git pull origin Eagle

which is a shorthand for:

git fetch origin
git merge origin/Eagle

In other words, you first fetched the latest changes from the remote into all your tracking branches (good), then you merged the Eagle branch into your local master (bad). The reason this happened is because you never switched branches before doing this git pull.

What you need to do to fix this:

You need to take 2 steps. First, you need to undo the incorrect merge into the master branch. Second, you should switch branches to Eagle, and then do the git pull.

# on the master branch
git log
# find the SHA-1 hash immediately BEFORE the bad merge, then do
git reset --hard SHA-1
# next switch to the Eagle branch
git checkout Eagle
# finally do the correct pull
git pull origin Eagle

You may still have merge conflicts, but they will be the real conflicts and not the result of merging Eagle into master.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks for your thorough and very understandable answer! I was under the assumption that doing `git pull origin Eagle` effectively creates a new local branch Eagle, but this is obviously not the case. I see where I went wrong and even feel a bit embarrassed about it. – dr jerry Nov 24 '15 at 09:16
  • I didn't even think you were trying to create a new local branch. I thought that, for whatever reason, you were trying to just switch branches and update `Eagle`. Actually, the other answer given is correct for creating a new branch from your up to date `master`. – Tim Biegeleisen Nov 24 '15 at 09:17
  • Yeah I was too fast with commenting and rewarding (hadn't even seen the other answer). Hope you don't mind awarding the other answer. – dr jerry Nov 24 '15 at 09:24
  • Just make sure you follow my steps to undo the bad merge _before_ you try to create a new branch. You had a bad merge commit in your `master` and it needs to be removed. – Tim Biegeleisen Nov 24 '15 at 09:25
0

What git pull origin branch does is that it fetches and merges the remote branch into your current local branch. So, the result of the above command is - to merge the remote branch eagle into your local master.

I guess what you want is to create a new local independent branch which tracks the corresponding remote branch. You can do that with

git fetch
git checkout -b eagle --track origin/eagle

This will create a new separate local branch eagle which would reflect the remote branch eagle.

This local branch will be independent of your local master branch and keep your local master untouched.

You can do git status and it will show Your branch is up-to-date with 'origin/eagle'. and you can checkout master with git checkout master and then doing a git status will show ahead by 14 commits.

Harish Ved
  • 560
  • 4
  • 17