简体   繁体   中英

git pull origin branch wants a merge on clean current master branch

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>

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 .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM