Search code examples
gitgit-mergegit-fetch

How do you get your local repo to match the server?


    b-c-d     <= branch-1         On GitHub                                     
   /                                                                            
  a-e-f-g     <= branch-2                                                       
      \                                                                         
       h-i    <= branch-3                                                       
                                                                                
                                                                                
    b-c-d     <= branch-1         On local                                      
   /                                                                            
  a-e-f-g     <= branch-2                                                       
                                                 

                           
                                                                            

If I'm on Branch-1 and do a "git pull origin branch-3" I end up with

    b-c-d-h-i <= branch-1         On local                                      
   /                                                                            
  a-e-f-g     <= branch-2                     

                              
                                                                            
                                                                            

If I'm on Branch-2 and do a "git pull origin branch-3" I end up with

    b-c-d     <= branch-1         On local                                      
   /                                                                            
  a-e-f-g-h-i <= branch-2                         

                          
                                                                            
                                                                            

What command do I issue to get branch-3 to be attached to commit " f " as seen on GitHub version ?


Solution

  • git pull is really git fetch followed by git merge.

    When you run git pull branch-3 while on branch-1 or branch-2, you are telling git to merge branch-3 into branch-1 or branch-2.

    branch-3 likely already exists on your local repo as a remote tracking branch (you can see it by running git branch --list --all). You can create a local branch that tracks the remote branch using a git shortcut: git checkout branch-3. This command notes that remotes/origin/branch-3 exists and assumes you want to create a new local branch to track the remote branch.