Git: What does EXACTLY “git pull” do?

后端 未结 4 1580
情歌与酒
情歌与酒 2020-12-09 23:44

I know that the \"git pull\" is actually a combination of \"git fetch\" and \"git merge\" and that basically it brings the repository as it it in remote repository.

4条回答
  •  甜味超标
    2020-12-10 00:38

    1. No :
      if you have local commits, which you haven't pushed yet, or some indexed changes (git added), you will still have those local changes on top of the last public commit (or merged with the last public commit) ;

    2. Yes :
      if nothing was pushed to the remote repo since your last git pull, you are already up to date, so nothing will change ;

    3. No :
      if you see changes in the index after a git pull, the files were already indexed before you ran git pull ;

    4. git already will, with the following caveat : if one of your indexed files should be updated by the merge, git will not perform the merge, and print a message :

      error: Your local changes to the following files would be overwritten 
      by merge:
          bb
      Please commit your changes or stash them before you merge.
      Aborting
      

      In that case : you should probably create a commit from your index, and run git merge origin/current/branch (or git rebase origin/current/branch) to incorporate the remote modifications with your local modifications.


    The default behavior of git fetch [origin] is to read all branches stored in the remote repo, and update all local refs stored under refs/remotes/[origin]/*.

    You can then use origin/branch/name as a valid tree-ish name in all standard git commands :

    # difference with remote "master" branch :
    $ git diff HEAD origin/master
    
    # history of remote branch "feature" alongside your local branch "feature" :
    $ git log --oneline --graphe feature origin/feature
    
    # merge changes from remote "master" :
    $ git merge origin/master
    
    # rebase your local commits on top of remote "develop" branch :
    $ git rebase origin/develop
    
    # etc ...
    

    You also have a shortcut to say "the remote branch linked to my active branch" : @{u}

    $ git diff @{u}
    $ git log --oneline --graph HEAD @{u}
    $ git merge @{u}
    $ git rebase @{u}
    # etc ...
    

    What does EXACTLY "git pull" do ?

    Right after a git fetch, git updates a special ref, named FETCH_HEAD, which generally match the @{u} of the active branch ;
    git pull does git fetch && git merge FETH_HEAD.

    I tried to explain git fetch in my own words in the paragraph above.

提交回复
热议问题