134

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?

CodeWizard
  • 110,388
  • 20
  • 126
  • 153
nobled
  • 1,581
  • 2
  • 11
  • 14
  • you can make a **get pull master** at my-branch, after your work you can go to master branch and **git merge my-branch** – Alexandre Mendes Dec 17 '15 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](https://github.com/jszakmeister/etc/blob/master/git-addons/git-ffwd) 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. – John Szakmeister Dec 17 '15 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. – John Szakmeister Dec 17 '15 at 21:39
  • @jszakmeister if I want to merge the latest updates from `master` into `my-branch` don't I have to? – nobled Dec 17 '15 at 21:42
  • @nobled Yes... I was just misunderstanding what you wanted to do at first. I thought you were looking to bring `master` up-to-date and avoid switching branches--in which case, my tool would help. But you also want to merge master into your branch, and it won't help with that bit. – John Szakmeister Dec 17 '15 at 22:40
  • Possible duplicate of [How to 'git pull' into a branch that is not the current one?](https://stackoverflow.com/questions/18994609/how-to-git-pull-into-a-branch-that-is-not-the-current-one) – underscore_d Apr 17 '18 at 12:50
  • 2
    Does this answer your question? [Merge, update, and pull Git branches without using checkouts](https://stackoverflow.com/questions/3216360/merge-update-and-pull-git-branches-without-using-checkouts) – icc97 Jan 11 '21 at 10:45

7 Answers7

218

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.

Chris
  • 2,983
  • 2
  • 17
  • 25
  • 7
    This syntax is actually useful if you wish to pull the branch under a different local branch name. – DustWolf Oct 01 '20 at 08:41
  • 6
    This should be the accepted answer as it exactly answers the question. The accepted answer does not provide the correct command and does something you don't expect based on the question. – stricq Oct 30 '20 at 19:57
  • 3
    Does `get fetch origin branch:branch` do exactly the same as `git pull` called from `branch`? – C. Binair Feb 26 '21 at 13:06
  • 1
    I tried to fetch in this way but it was rejected because of non-fast-forward. – Memphis Meng Oct 18 '21 at 17:39
  • 3
    Just to clarify: the syntax here is: `git fetch :` – Adam Mar 15 '22 at 11:33
39

Try this:

git pull yourRepositoryName master
Robert Kraaijeveld
  • 647
  • 1
  • 6
  • 14
  • 9
    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 git@github.com:foo/bar.git master` – nobled Dec 17 '15 at 21:30
  • 2
    Quite right, I assumed you had made an alias for that URL. Glad to have helped! – Robert Kraaijeveld Dec 17 '15 at 21:32
  • 20
    Ah, so if you have the default `origin` alias you'd run `git pull origin master` – nobled Dec 17 '15 at 21:35
  • 3
    Exactly. You can specify aliases yourself by using `git remote add yourAlias https://github.com/yourGitHubRepo´ – Robert Kraaijeveld Dec 17 '15 at 21:36
  • 8
    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: http://gitref.org/remotes/ – nobled Dec 17 '15 at 21:50
  • 4
    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. – stricq Oct 30 '20 at 20:00
  • 1
    That answer does not do what OP asked for, it will not update master, only your current branch. Checkout @chris's answer on how to update master, without checking out master – User2585 Dec 02 '20 at 15:09
  • Yes, Chris' answer is better, it just came 2 years later, but I did mark it as the right answer eventually... – nobled Jul 09 '21 at 15:08
34

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
SebMa
  • 3,181
  • 24
  • 32
11

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.

Peter
  • 3,879
  • 21
  • 32
  • 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`) – nobled Dec 17 '15 at 21:45
  • 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. – Peter Dec 17 '15 at 22:01
  • 1
    This is a good read, with some good diagrams that illustrate this: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches – Peter Dec 17 '15 at 22:04
2

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
nirojshrestha019
  • 685
  • 6
  • 11
1

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

hedzr
  • 145
  • 2
  • 6
0

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

Erkang
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 15 '22 at 09:23