Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pull on a different branch

Tags:

git

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?

like image 314
nobled Avatar asked Dec 17 '15 20:12

nobled


People also ask

Can I git pull from another branch?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

How do I pull changes from another branch without merging?

You could use git checkout from the branch you want to transfer the changes to: git checkout <branch name> . That will change all files to match the version in the desired branch. Then you can commit, change, discard whatever you want.


2 Answers

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.

like image 92
Chris Avatar answered Oct 06 '22 13:10

Chris


Try this:

git pull yourRepositoryName master 
like image 27
Robert Kraaijeveld Avatar answered Oct 06 '22 14:10

Robert Kraaijeveld