简体   繁体   中英

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?

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.

尝试这个:

git pull yourRepositoryName master

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

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.

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

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

我主要使用 IntelliJ,我希望有一个上下文菜单项可以做这个 master 分支更新,然后将 master 合并到 current。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM