git pull on a different branch

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

7 Answers

I found an incantation that worked for me:

git fetch origin master:master 

then (if you want to merge right away):

git merge master 

I was really surprised how hard it was to find an answer to this since it seems like it would be a common use case.

5

Try this:

git pull yourRepositoryName master 
8

We can fetch 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 
2

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

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 
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 HEAD 2> /dev/null | cut -b 12-\`; 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, ....

I use IntelliJ mostly, I hope there is a context menu item can do this master branch update, then merge master into current.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like