235

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
  • 128,036
  • 21
  • 144
  • 167
nobled
  • 2,601
  • 2
  • 14
  • 15
  • 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
  • 3
    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

9 Answers9

386

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
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Chris
  • 4,734
  • 2
  • 19
  • 26
  • 11
    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
  • 7
    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
  • 10
    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
  • @C.Binair Nowadays when I need to pull changes from `origin/branch2` to my Working branch `branch1` I just `git fetch` all branches. They get fetched into `origin/...` branches. Then I just merge them into my `branch1` calling this: `git merge origin/branch2` from my target branch `branch1` – Seangle Jan 09 '23 at 09:54
  • 2
    @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 – Seangle Jan 09 '23 at 09:57
  • 1
    @C.Binair According to an unnamed assistant, that rhymes with 'bee', `git fetch origin branch:branch` performs a hard update and would overwrite any unpushed changes in your local `branch.` – cyclingLinguist Jun 14 '23 at 15:33
  • To clarify points in these comments, git fetch grabs from the remote into your local _repository_ but not workspace, so you would still have to merge files to combine any remote updates into your local 'on-disk' ones. So the fetch updates the other branch, which has no 'on-disk' manifestation, then you merge/rebase it into your current space and all seems to be well with this method. – Adam Tolley Jul 27 '23 at 15:15
  • This worked like a charm! – Jijo Aug 11 '23 at 14:25
66

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
  • 4,037
  • 29
  • 39
  • 7
    you should explain your answer or at least add some reference, than other will be able to fully understand your answer. – devasia2112 Sep 21 '20 at 03:05
  • let's add this checkout part in the beginning just to make sure that we are pulling into right branch: `git checkout ` – nirojshrestha019 Mar 11 '22 at 14:07
  • 3
    "We can fetch changes from another branch..." is a very misleading lede. Needs to be changed to "we can *merge* changes from another branch...". – dqbydt Dec 04 '22 at 23:21
  • Yes, misleading, wrong answer to the question, and cause quite a mess if you run this on your branch – laurent Apr 06 '23 at 09:08
43

Try this:

git pull yourRepositoryName master
Robert Kraaijeveld
  • 687
  • 1
  • 6
  • 14
  • 11
    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
  • 22
    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
  • 6
    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
33

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
nirojshrestha019
  • 2,068
  • 1
  • 10
  • 14
  • 2
    @GustavoParrado the question was git pull on a different branch. So though this would be relevant answer since I am pulling branch1 to branch2. – nirojshrestha019 Mar 24 '22 at 01:15
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.

Peter
  • 3,998
  • 24
  • 33
  • 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
5

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
Scrutator
  • 79
  • 1
  • 4
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
  • 155
  • 2
  • 7
0

Step 1:

git checkout yourBranch

Step 2:

git fetch origin master

Step 3:

git pull origin master
Maizied Hasan Majumder
  • 1,197
  • 1
  • 12
  • 25
  • 3
    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 https://git-scm.com/docs/git-pull – Nik O'Lai Oct 21 '22 at 14:06
0

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

WISERDIVISOR
  • 154
  • 2
  • 12