Pulling Changes From A Remote
Pull changes from a remote
Let’s say that we are in a situation where there are commits on the _remote _repository that we do not have in our _local _repository. This can happen in several ways: You could be working on a team, and a co-worker has pushed new changes to the remote. Alternatively, you could be working on the same project but from different computers -- for example, say you have a work computer and a personal computer, and you contribute to the repo from both of them. If you push changes to the repo from your work computer, the local repo on your personal computer will not reflect those changes. How do we sync new changes that are on the remote into the local repository? That's exactly where we're going to be looking at now. Let's first look at how pulling in remote changes works in theory, then we'll actually do it ourselves!
I said it before but I'll say it again, the branch that appears in the _local _repository is actually tracking a branch in the _remote _repository (e.g.origin/master
in the local repository is called a tracking branch because it's tracking the progress of themaster
branch on the remote repository that has the shortname "origin").
Add remote changes
Since we don't have any commits on our remote repository yet and we're not collaborating with anybody we're going to fake it and add some commits manually through GitHub's interface on the web.
A walk-through video is below. Use the code snippets to follow along in the video.
New CSS Content
Add the following new ruleset:
.destination:hover h2 {
transform: rotate(0deg);
}
Addtransition: transform 0.5s;
to theh2
ruleset, so it should now be:
h2 {
margin: 0;
transform: rotate(-45deg);
transition: transform 0.5s;
text-shadow: 0 0 5px #01579b;
}
⚠️ Prefer Working Locally ⚠️
Because GitHub has a web interface, it allows you to add commits manually through its interface. But just because youcan_do a thing, doesn't mean you_should. I've demoed making these changes this way so that we could simulate commits being on the remote repository but not in the local repository. But I recommend that you should always work locally on a project and then push those changes to the remote repository.
Retrieve remote commits
Now let's compare our local repository and our remote repository. We only have_three_commits in our local repository:
Our local repository only has three commits in it.
While there are actually_four_commits in the remote repository:
Pulling Changes withgit pull
The local commits end at commit5a010d1
while the remote has two extra commits - commit4b81b2a
and commitb847434
.
Also, notice that in our_local_repository when we did thegit log
theorigin/master
branch is still pointing to commit5a010d1
.
Remember that theorigin/master
branch is not a live mapping of where the remote's master branch is located. If the remote'smaster
moves, the localorigin/master
branch stays the same. To update this branch, we need to sync the two together.
git push
will sync theremote_repository with the_local_repository. To do the opposite (to sync the_local_with the_remote), we need to usegit pull
. The format forgit pull
is very similar togit push
- you provided the shortname for the remote repository and then the name of the branch you want to pull in the commits.
$ git pull origin master
There's several things to note about running this command:
- the format is very similar to that of
git push
- there's counting and compressing and packing of items - it has the phrase "fast-forward" which means Git did a fast-forward merge (we'll dig into this in just a second)
- it displays information similar to
git log --stat
where it shows the files that have been changed and how many lines were added or removed in them
- it displays information similar to
If you don't want to automatically merge the local branch with the tracking branch then you wouldn't usegit pull
you would use a different command calledgit fetch
. You might want to do this if there are commits on the repository that you don't have_but_there are also commits on the local repository that the remote one doesn't have either.
Let's take a look atgit fetch
.
Recap
If there are changes in a remote repository that you'd like to include in your local repository, then you want to_pull_in those changes. To do that with Git, you'd use thegit pull
command. You tell Git the shortname of the remote you want to get the changes from and then the branch that has the changes you want:
$ git pull origin master
Whengit pull
is run, the following things happen:
- the commit(s) on the remote branch are copied to the local repository
- the local tracking branch (
origin/master
) is moved to point to the most recent commit - the local tracking branch (
origin/master
) is merged into the local branch (master
)
Also, changes can be manually added on GitHub (but this is not recommended, so don't do it).