Search code examples
gitgit-branchgit-pull

Is it safe to do a git pull from master to my local repo after creating a new branch on which I need to see the latest changes too?


I would like to have the latest updates from the remote repo on my local one. I usually do git pull while being on the master branch and then create a new branch. I have now created a new branch (did not add/change anything in the local code) but skipped the git pull on master before this. If I do a git pull, will the latest remote modifications be reflected on my new local branch and local master, or should I delete it and recreate it after git pull? Thanks!


Solution

  • What git will do depends on the exact command you issue, and also on your git configuration.

    The first thing to understand about pull is, it updates the current branch. If you check out master and then pull, the changes you pull will be incorporated into master (but not your branch). If you check out your branch and then pull, the changes will be incorporated into your branch (but not master). This, of course, is the point of branching - changes to one branch don't automatically affect the other. If you want to affect both, you can (see below for more on that).

    Also, you can tell git what changes to incorporate into the current branch, or if you don't specify then it will look for a configured default corresponding to the current branch. (Some people seem to think this default behavior is all pull does, and that can get them into trouble.) So if you want to integrate the remote master's changes into your branch, you can do

    git checkout my_branch
    git pull origin master
    

    I typically don't recommend this usage. It's basically a shorthand for

    git checkout my_branch
    git fetch 
    git merge origin/master
    

    which is a little more explicit (so less dependent on configuration details, etc.). I use pull quite a bit, but only for its default behavior in repos whose configuration/branch setup is "typical".

    Now, if you want the origin/master changes reflected in multiple branches, then you have to perform multiple merge (and/or rebase) operations; and a given pull only does one. So typically you'd first get the changes into your local master

    git checkout master
    git pull
    

    (or, depending on configuration, maybe you'd need git pull origin or even git pull origin master). Then you'd incorporate them into the branch by either

    git checkout my_branch
    git merge master
    

    or

    git rebase master my_branch