291

If I'm working on a branch and then realize I need to merge another branch into mine here is my current workflow (for this example lets say that I'm working on my-branch and want to merge in master):

git stash git checkout master git pull git checkout my-branch git merge master git stash pop 

Is there a way in git to pull a branch other than the currently checked out one, or is there a better way to do this?

For example, here's what I'd like to be able to do (again lets say I'm on my-branch and want to merge in master):

git pull master git merge master 

The git-pull man page says that a git pull is just a get fetch followed by a git merge, so is there a way to do the merge part of the git pull on a branch other than the one that's currently checked out?

Or is what I'm asking for just not possible?

7
  • 1
    you can make a get pull master at my-branch, after your work you can go to master branch and git merge my-branch Commented Dec 17, 2015 at 21:01
  • I don't like git pull because it can introduce a merge commit behind your back, and I'm looking to bring all my tracking branches up-to-date. To that end, I wrote an addon that will fetch and fast-forward any tracking branch. Myself and others have been using it for quite some time, and it's definitely a time-saver. The nice part is that if it's not a fast-forward merge, it will leave you to resolve it and make it better. This works well for us since we use a rebase workflow quite often. Commented Dec 17, 2015 at 21:22
  • Ah, I see... you want to actually bring your branch up-to-date with master too. My tool will not do that. Commented Dec 17, 2015 at 21:39
  • @jszakmeister if I want to merge the latest updates from master into my-branch don't I have to? Commented Dec 17, 2015 at 21:42
  • 3
    Does this answer your question? Merge, update, and pull Git branches without using checkouts Commented Jan 11, 2021 at 10:45

9 Answers 9

478

I found a git command that worked for me:

Fetch

git fetch origin master:master 

The syntax for the above is as follows (thanks @Adam)

git fetch <remote> <src>:<dst> 

Merge

Then (if you want to merge right away):

git merge master 
Sign up to request clarification or add additional context in comments.

8 Comments

This syntax is actually useful if you wish to pull the branch under a different local branch name.
Does get fetch origin branch:branch do exactly the same as git pull called from branch?
I tried to fetch in this way but it was rejected because of non-fast-forward.
@C.Binair Or if I don't want to fetch all branches for some reason I just do git fetch origin branch2 and merge from origin/branch2 into my working branch. I may also clarify that local branch2 doesn't get updated. Whenever I switch to branch2 I have to do git merge origin/branch2 as well
That is such a weird syntax! I was taught that the difference between git fetch and git pull is that git fetch does not update any (local) branch pointers - but here it is doing just that!
|
82

We can merge changes from another branch in the same repository using git pull command like this:

$ git pull origin <target-branch> 

See the EXAMPLES section of man git-pull :

 • Merge into the current branch the remote branch next: $ git pull origin next 

4 Comments

you should explain your answer or at least add some reference, than other will be able to fully understand your answer.
let's add this checkout part in the beginning just to make sure that we are pulling into right branch: git checkout <target-branch>
"We can fetch changes from another branch..." is a very misleading lede. Needs to be changed to "we can merge changes from another branch...".
Yes, misleading, wrong answer to the question, and cause quite a mess if you run this on your branch
44

Pull in changes from branch1 to branch2

Let's say you have these branches:

  • master
  • branch1
  • branch2

So you want to pull in changes from branch1 to branch2. Here is what you can do:

git checkout branch2 git pull origin branch1 

1 Comment

@GustavoParrado the question was git pull on a different branch. So though this would be relevant answer since I am pulling branch1 to branch2.
43

Try this:

git pull yourRepositoryName master 

8 Comments

I had tried this, but I guess I didn't realize what you had to put for yourRepositoryName. It's not really the name of the repository, but the url you used to checkout the repo. For example: git pull [email protected]:foo/bar.git master
Ah, so if you have the default origin alias you'd run git pull origin master
Exactly. You can specify aliases yourself by using `git remote add yourAlias github.com/yourGitHubRepo´
And for anyone else who comes across this, git remote -v will display any aliases and their URLs. And here is more info on aliases: gitref.org/remotes
This does not answer the question. This command will merge the given branch into your CURRENT branch. For the correct command look for Chris' answer. It also has the most votes.
|
15

You could also try this:

git fetch git merge origin/master 

This won't update your local master pointer, but it will merge the latest origin/master into your current local branch.

3 Comments

If my local master pointer isn't updated then won't the merge from master into my-branch not find any changes to merge in? (assuming I had already merged master into my-branch prior to running git fetch and the fetch found changes in master)
Assume you are on your master branch. git pull is really just a shortcut for git fetch then git merge origin/master. The fetch part updates all your remote tracking pointers (origin/master, origin/whatever) and downloads all dependent objects. This performs no merge—which means now you have the options to choose what you want to merge into what.
This is a good read, with some good diagrams that illustrate this: git-scm.com/book/en/v2/Git-Branching-Remote-Branches
6

If you usually merge some remote branch in to your "feature/*" branch, such as "master", "develop" or "release/*", then the best strategy is not to merge, but to rebase to on a remote branch. Then commits from your branch will be applied on top other branchs history. It's anable fast forward merge into this remote branch afterwards.

git pull origin master git rebase master 

Comments

2

If you need to pull from one LOCAL branch to another LOCAL branch, this is what worked from me. Take this example:

$ git branch master branch1 

Imagine you need to do a bugfix on branch1 so you make a new branch from it named branch2 and pull it to your local repo.

Tree on Remote:

 C0 C1 C2 Master x------x | Branch1 x-----x | Branch2 x-----x 

Tree on Local:

 C0 C1 C2 Master x------x | Branch1 x-----x | Branch2 x-----x 

You forget to checkout to branch2 and made the bugfix on branch1, now it looks like:

Tree on Remote:

 C0 C1 C2 Master x------x | Branch1 x-----x | Branch2 x-----x 

Tree on Local:

 C0 C1 C2 C3 Master x------x | Branch1 x-----x-----x | Branch2 x-----x 

As you can see, branch1 has the data we need but only in local not in remote. so we CANNOT DO

$ git pull origin branch1

What we need to do here is just .

$ git checkout branch2 $ git pull . branch1 ✔️ 

Now you can see the bugfix in branch2. Then you can checkout to branch1 and revert your last commit so that no one knows of your goof-up. Now it looks like:

Tree on Remote:

 C0 C1 C2 Master x------x | Branch1 x-----x | Branch2 x-----x 

Tree on Local:

 C0 C1 C2 C3 Master x------x | Branch1 x-----x | Branch2 x-----x-----x 

Hope you git it :)

Comments

2

The solid way is:

git checkout master git pull --ff origin master git checkout devel # git merge --ff devel 

so there is an alias

git config --global alias.update-branch '! bash -c "X1=\`git symbolic-ref --short HEAD 2> /dev/null \`; echo pulling branch $1 ... && git checkout $1 && git pull --ff origin $1 && git checkout \$X1 "' # call it from 'devel' branch: git update-branch master 

You may improve it to compliant with yours, such as merge master into current branch right now, ....

Comments

1

Step 1:

git checkout yourBranch 

Step 2:

git fetch origin master 

Step 3:

git pull origin master 

1 Comment

FYI: step 2 (git fetch) is not necessary because step 3 (git pull) performs fetching internally. It is explained in the very first line at git-scm.com/docs/git-pull

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.