21 | Retrieving Remote Changes, Part II |
Many people new to Git treat git fetch and git pull as synonyms. Understanding the differences between the two is important to understanding how Git handles remote repositories. Remember, remotes are read-only branches. You fetch changes from a remote repository into those branches (which are stored locally), instead of committing directly to them, and then merge those changes as necessary. You can use git pull to combine fetching and merging into one command.
git pull follows Git’s convention and assumes that you want to pull from the origin remote repository if you do not specify a remote. You must specify a remote if you want to provide a specific branch to pull from.
You can provide a full refspec—two branches separated by a colon—to control which branch you are pulling from and which branch you want those changes to end up in. You specify the remote branch before the colon and the local branch after the colon. You can pull into branches that don’t exist.
You can use the --rebase option to tell Git to rebase your local changes on top of the remote changes instead of performing a merge. This is the equivalent of running git fetch followed by git rebase. This allows you to cleanly apply all your local changes on top of the remote changes that have already been shared.
What To Do... |
| prompt> git pull [name [branch name]] |
| ... example ... |
| prompt> git pull tswicegood master |
| From git://github.com/tswicegood/bobby-tables |
| * branch master -> FETCH_HEAD |
| prompt> git pull origin <remote branch>:<local branch> |
| ... example ... |
| prompt> git pull origin development:team-dev |
To fetch from origin and rebase against its main branch, use this:
| prompt> git pull --rebase origin master |
| From git://github.com/origin/bobby-tables |
| * branch master -> FETCH_HEAD |
| First, rewinding head to replay your work on top of it... |
| Applying: add <meta> tags |
Task 15, Merging Commits Between Branches
Task 16, Rewriting History by Rebasing
Task 19, Adding and Removing Remotes
Task 20, Retrieving Remote Changes
Task 22, Sending Changes to Remotes
18.189.188.238