5

I have a git repository and I am on branch A. How can I pull the latest code to my local master branch without checking out to master? I know that I can do below code:

git checkout master
git pull

but if I have changed files in my current branch A. I have to either commit or stash it before changing to the master branch. Is there a way for me to do that in my current branch A?

I don't have any local commits in master since I always work on the personal branch and I use master only for read-only purpose.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • Do you have any local commits in your `master`? – max630 Jul 27 '18 at 05:18
  • 1
    No I don't have any commits in local master – Joey Yi Zhao Jul 27 '18 at 05:18
  • Possible duplicate of [How to pull into not-the-current-branch?](https://stackoverflow.com/questions/9747718/how-to-pull-into-not-the-current-branch) – phd Jul 27 '18 at 11:02
  • Does this answer your question? [How to pull a git branch without checking it out?](https://stackoverflow.com/questions/29895560/how-to-pull-a-git-branch-without-checking-it-out) – Tom Hale Aug 03 '20 at 04:42

4 Answers4

2

If the master does not have any local commits, then it points to the very same commit as origin/master. In this case, it does not make much sense to have it. For whatever purpose you need a master, use can use the origin/master, so that you don't have to maintain the local master:

$ git branch <feature> origin/master # make new feature branch
$ git log ..origin/master # see what is new in the upstream
$ git diff origin/master...HEAD # see your changes

...etc.

max630
  • 8,762
  • 3
  • 30
  • 55
0

It seems you want to keep your master branch up to date so that you can do this later:

$ git checkout master
$ git merge A

But you don't need to strictly keep your master branch up to date till the time you're finished with A, since the origin/master remote-tracking branch will be kept up to date when you do a fetch(this'll keep the branch A unaffected, since the fetch will only get changes from the remote repository not change local branches at all.). In fact, this is the very purpose of a remote tracking branch.

Then when you are ready to merge your branch A into master, then you can do a merge on your master branch(which git pull does automatically - it's just a fetch followed by a merge):

$ git checkout master
$ git fetch
$ git merge FETCH_HEAD

FETCH_HEAD is updated by git fetch and used by the git merge command to store the commit which it wants to merge to - when you do a git pull, this is the latest commit which is merged into your local branch.

From the git-merge docs:

If no commit is given from the command line, merge the remote-tracking branches that the current branch is configured to use as its upstream. See also the configuration section of this manual page.

When FETCH_HEAD (and no other commit) is specified, the branches recorded in the .git/FETCH_HEAD file by the previous invocation of git fetch for merging are merged to the current branch.

cst1992
  • 3,823
  • 1
  • 29
  • 40
0

Can use following:

git pull origin origin-branch-name:local-branch-name

above will merge the origin branch with local branch

git merge another-local-branch:another2-local-branch

it should(not tested) merge the two different branches while keeping active intact

justnajm
  • 4,422
  • 6
  • 36
  • 56
-1

Getting latest develop and merging it onto you current branch:

git fetch origin develop && git merge origin/develop
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • 1
    How will this prevent the stash operation? The `git merge` will still complain that the branch has uncommitted changes before merging the remote branch. Also, do you mean `A`? 'Cause there's no `develop` branch in this scenario. – cst1992 Aug 06 '18 at 06:43