35

I've created a branch b1 and I made some changes on it and I push it to the remote repository:

git branch b1
git checkout b1
git add newfile.txt
git commit -m "adding a new file"
git push origin b1

On an other machine which is connected to the remote repository, I tried to pull the branch without merge it with master:

$git branch
*master
$git pull origin b1
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From sl*******02:/opt/git/projet1
 * branch            b1    -> FETCH_HEAD
Updating fca3b48..1d96ceb
Fast-forward
 newfile.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 newfile.txt

$git branch
*master

what I expected:

$git branch
*master
b1
4
  • It seems you have missed one step to checkout new branch before pushing your code?
    – Vuong
    May 23, 2018 at 10:14
  • git branch lists local branches, since you've never checked out b1 it only resides in the remote, use git branch -a to list this as well. In order to get it as a local branch, check it out with git checkout b1, this will create a local branch out of the remote branch and set up upstream mapping between them. May 23, 2018 at 10:17
  • @LasseVågsætherKarlsen this is what I'm getting when I checkout b1 error: pathspec 'b1' did not match any file(s) known to git.
    – Carole
    May 23, 2018 at 10:21
  • Try git fetch without parameters first, or git fetch origin May 23, 2018 at 13:11

6 Answers 6

51

You are actually asking two questions:

  • in the title - how to pull without merge (I came here for this question, originally;)
  • in the description - how to check out a branch without merging in to master

Pull remote branch without merge

# Assuming you are on wanted branch (b1) already

git pull --rebase

No merge, just rebase.

Check out remote branch (from origin)

git fetch 
git checkout b1

No merge to master, new branch b1 is created.

1
  • This was definitely the most useful answer for me. Doing a pull through a rebase in a single command is super convenient.
    – Zoom
    Apr 21, 2022 at 14:08
50

You can use git fetch origin b1 to only fetch remote branch without merge.

See : https://git-scm.com/docs/git-fetch

Basically git pull is a shortcut to git fetch && git merge

Merge execute because you was on master branch, and not your local b1 branch.

8

You want to checkout the remote branch with

git checkout b1

if you are on a git version < 1.6.6 the way to do that is instead

git checkout -b b1 origin/b1
4
  • how could I checkout a branch if this one still not exist?
    – Carole
    May 23, 2018 at 10:10
  • @Carole it defaults from the only remote available you already fetched
    – DRC
    May 23, 2018 at 10:11
  • 1
    can you explain more?
    – Carole
    May 23, 2018 at 10:22
  • 2
    @Carole you first need to do git fetch, this fetches all the remote branches, and you can see them with git branch -a , when doing git checkout b1 you switch to the origin/b1 branch
    – DRC
    May 23, 2018 at 10:30
3

It also possible before committing

with the following commands

git stash
git pull
git stash apply
1
  • Please explain your answer a little more, because I want something like stash pop on some feature A branch, but changes in feature B branch is already committed... So how to get the stash from branch feature B. Apr 11 at 5:24
1
git remote update origin —prune
1
  • 6
    Welcome to Stack Overflow. Commands are easier to read if formatted as code. Code only answers can generally be improved by adding some explanation of how and why they work and in the case of commands what expected output would look like if it was successful, or how to interpret the results of the command if applicable. Aug 2, 2020 at 17:25
0

I did it this way:

steps:

  • checkout to different branch
  • git stash
  • deleted the local branch in which was having merge error/conflict
  • did git fetch all
  • git checkout <branch_name in which you were getting merge error before>
  • git pull origin

commands:

ex:

  • error branch name: Error-branch

  • other branch name: Other-branch

  • git checkout Other-branch
  • git stash
  • git branch -d Error-branch (if doesn't work then use below)
  • git branch -D Error-branch (if you have merge conflict)
  • git fetch all
  • git checkout Error-branch
  • git pull origin

I hope this will solve your problem

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.