Definition of “downstream” and “upstream”

前端 未结 5 1320
难免孤独
难免孤独 2020-11-21 04:59

I\'ve started playing with Git and have come across the terms \"upstream\" and \"downstream\". I\'ve seen these before but never understood them fully. What do these terms

5条回答
  •  醉话见心
    2020-11-21 05:24

    Upstream (as related to) Tracking

    The term upstream also has some unambiguous meaning as comes to the suite of GIT tools, especially relative to tracking

    For example :

       $git rev-list --count --left-right "@{upstream}"...HEAD
       >4   12
    

    will print (the last cached value of) the number of commits behind (left) and ahead (right) of your current working branch, relative to the (if any) currently tracking remote branch for this local branch. It will print an error message otherwise:

        >error: No upstream branch found for ''
    
    • As has already been said, you may have any number of remotes for one local repository, for example, if you fork a repository from github, then issue a 'pull request', you most certainly have at least two: origin (your forked repo on github) and upstream (the repo on github you forked from). Those are just interchangeable names, only the 'git@...' url identifies them.

    Your .git/configreads :

       [remote "origin"]
           fetch = +refs/heads/*:refs/remotes/origin/*
           url = git@github.com:myusername/reponame.git
       [remote "upstream"]
           fetch = +refs/heads/*:refs/remotes/upstream/*
           url = git@github.com:authorname/reponame.git
    
    • On the other hand, @{upstream}'s meaning for GIT is unique :

    it is 'the branch' (if any) on 'said remote', which is tracking the 'current branch' on your 'local repository'.

    It's the branch you fetch/pull from whenever you issue a plain git fetch/git pull, without arguments.

    Let's say want to set the remote branch origin/master to be the tracking branch for the local master branch you've checked out. Just issue :

       $ git branch --set-upstream  master origin/master
       > Branch master set up to track remote branch master from origin.
    

    This adds 2 parameters in .git/config :

       [branch "master"]
           remote = origin
           merge = refs/heads/master
    

    now try (provided 'upstream' remote has a 'dev' branch)

       $ git branch --set-upstream  master upstream/dev
       > Branch master set up to track remote branch dev from upstream.
    

    .git/config now reads:

       [branch "master"]
           remote = upstream
           merge = refs/heads/dev
    

    git-push(1) Manual Page :

       -u
       --set-upstream
    

    For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch..merge in git-config(1).

    git-config(1) Manual Page :

       branch..merge
    

    Defines, together with branch..remote, the upstream branch for the given branch. It tells git fetch/git pull/git rebase which branch to merge and can also affect git push (see push.default). \ (...)

       branch..remote
    

    When in branch < name >, it tells git fetch and git push which remote to fetch from/push to. It defaults to origin if no remote is configured. origin is also used if you are not on any branch.

    Upstream and Push (Gotcha)

    take a look at git-config(1) Manual Page

       git config --global push.default upstream
       git config --global push.default tracking  (deprecated)
    

    This is to prevent accidental pushes to branches which you’re not ready to push yet.

提交回复
热议问题