nobled
nobled

Reputation: 3071

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?

Upvotes: 280

Views: 533477

Answers (9)

hedzr
hedzr

Reputation: 183

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

Upvotes: 2

WISERDIVISOR
WISERDIVISOR

Reputation: 183

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 :)

Upvotes: 1

Chris
Chris

Reputation: 5484

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

Upvotes: 459

Maizied Hasan Majumder
Maizied Hasan Majumder

Reputation: 1053

Step 1:

git checkout yourBranch

Step 2:

git fetch origin master

Step 3:

git pull origin master

Upvotes: 1

Scrutator
Scrutator

Reputation: 89

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

Upvotes: 6

nirojshrestha019
nirojshrestha019

Reputation: 2955

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

Upvotes: 42

SebMa
SebMa

Reputation: 4709

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

Upvotes: 76

Peter
Peter

Reputation: 4057

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.

Upvotes: 15

Robert Kraaijeveld
Robert Kraaijeveld

Reputation: 695

Try this:

git pull yourRepositoryName master

Upvotes: 43

Related Questions