20 Retrieving Remote Changes

You must keep your repository in sync with the changes from everyone else that is collaborating on it with you. You do this with the git fetch command. Fetching is closely related to git pull, and many people incorrectly use the two commands interchangeably.

Fetching changes from a remote repository retrieves—literally fetches—the changes from that remote repository. This stores them in their remote branches on your local repository. You can use this to see what changes are on the remote repository without affecting your local repository.

Git fetches the changes from the origin remote repository if you don’t specify a remote. You can fetch from another repository by providing the name of that remote repository. By default, it fetches all branches from a remote repository. You can change this depending on the parameters you provide to git fetch.

You can fetch a specific branch by calling git fetch with an explicit remote name and a refspec. Refspecs provide the name of the remote branch and the branch in your local repository that it should be fetched into separated by a colon. For example, to fetch only the master branch from your origin branch, you use this: git fetch origin master:remotes/origin/master.

This format might look a little wonky at first glance. Most of the time you refer to a remote branch by <remote name>/<branch name> without the remotes/ prefix. Its full name contains the remotes/ prefix, however, and you must use its full name with this command.

You can fetch changes from multiple remotes at one time. You can use the --multiple parameter to provide Git with multiple remotes to fetch changes from. Use --all to tell Git to go through all of your remotes and fetch the changes from them.

What To Do...
  • Fetch changes from remote repository.
     
    prompt>​ git fetch <remote name>
     
    ...​ example ...
     
    prompt>​ git fetch tswicegood
     
    remote: Counting objects: 39, done.
     
    remote: Compressing objects: 100% (25/25), done.
     
    remote: Total 39 (delta 16), reused 26 (delta 9)
     
    Unpacking objects: 100% (39/39), done.
     
    From git://github.com/tswicegood/bobby-tables
     
    * [new branch] master -> tswicegood/master
  • Fetch a specific branch.

    This requires a peculiar refspec to make sure the fetched branch ends up in the right place in your local repository.

     
    prompt>​ git fetch remote
     
    local branch:remotes/remote/remote branch

    To fetch master from origin to your local copy of the origin/master remote branch, use this:

     
    prompt>​ git fetch origin master:remotes/origin/master
  • Fetch changes from multiple remote repositories.
     
    prompt>​ git fetch --multiple remote1 remote2 ... and so on ...
  • Fetch changes from all remote repositories.
     
    prompt>​ git fetch --all
     
    ...​ example after adding another remote ...
     
    prompt>​ git fetch --all
     
    Fetching tswicegood
     
    Fetching petdance
     
    remote: Counting objects: 414, done.
     
    remote: Compressing objects: 100% (161/161), done.
     
    remote: Total 407 (delta 231), reused 397 (delta 227)
     
    Receiving objects: 100% (407/407), 52.53 KiB, done.
     
    Resolving deltas: 100% (231/231), completed with 2 local objects.
     
    From http://github.com/petdance/bobby-tables
     
    * [new branch] master -> petdance/master

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.86