git pull on a different branch

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

Try this:

git pull yourRepositoryName master

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

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

Solution 6 - Git

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

Solution 7 - Git

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, ....

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnobledView Question on Stackoverflow
Solution 1 - GitChrisView Answer on Stackoverflow
Solution 2 - GitRobert KraaijeveldView Answer on Stackoverflow
Solution 3 - GitSebMaView Answer on Stackoverflow
Solution 4 - GitPeterView Answer on Stackoverflow
Solution 5 - Gitnirojshrestha019View Answer on Stackoverflow
Solution 6 - GitErkangView Answer on Stackoverflow
Solution 7 - GithedzrView Answer on Stackoverflow