Search code examples
gitgithubgit-branchgit-remotegit-fetch

Change remote branch of repository and pull only the changes


I have a remote repository that has a lot of branches. Every time there is a new version, a new branch is created. How can I, when a new version is available, change the branch and pull the changes avoiding to clone it all again. I want to only download the new committs.


Solution

  • Executive Summery:

    git fetch origin
    git checkout new-version`
    

    Where origin is the remote name and new-version is the branch name.

    Details:

    The documentation for git fetch says:

    Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated.

    This basically means that it goes and downloads the history of the repository from the remote. However, a repository can have multiple remotes, so git fetch can fetch either one or more of them using the --all flag.

    By default, any tag that points into the histories being fetched is also fetched; the effect is to fetch tags that point at branches that you are interested in. This default behavior can be changed by using the --tags or --no-tags options... By using a refspec that fetches tags explicitly, you can fetch tags that do not point into branches you are interested in as well.

    Meaning that if you had, for example, branches A, B & C on the remote, and on local you were working on branch C. If you git fetch origin C, by default, this will also fetch the tags pointing to that branch, but it will not fetch those related to other branches, for instance A and B.
    If you want to fetch all tags, you just add the --tags to the command.

    Finally,

    When no remote is specified, by default the origin remote will be used, unless there’s an upstream branch configured for the current branch.

    Resources: