Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Once you've cloned a repository, how do you keep the clone up to date with new commits? In this video, we'll show you how to pull commits from another repo.
When commits get added to a Git repository, they aren't automatically copied to clones of that repository. One way to distribute the new commits is to pull them in from other repos.
- In the original
medals
repo, save agold.html
file that advertises gold medals.
medals/gold.html
:
<h1>Check out our gold medals!</h1>
<p>Medallion: $60</p>
<p>Ribbon: $30</p>
- Stage the file with
git add gold.html
, and commit it:git commit -m "Add gold medals"
-
git log
in themedals
repo will show the new commit, butgit log
in themyclone
repo will not. - We need to get the commit that adds
gold.html
from themedals
repo to themyclone
repo.- To do that, we need a link from the
myclone
repo back to themedals
repo. - Within a Git repository, you can add links to other repos. These linked repos are referred to as remote repos.
- We can get a list of remote repos with the
git remote
command. - We see one remote repo listed here, named
origin
.
- To do that, we need a link from the
$ git remote
origin
When you clone a Git repo, the original repo is automatically added as a remote on the clone. You can name remote repos whatever you want, but the default used when cloning is origin
, because it represents the repo this clone originated from. Because of this default, you'll see a remote repo named origin
on most Git repos you work with. In fact, in many cases, the origin repo will be the only remote repo, because everyone on the project just pulls changes from that single repo.
- Since a remote repo is already set up, we're ready to pull changes from it.
- We do this with the
git pull
command. -
git pull
takes an argument, with the name of the remote repo you want to pull changes from. So this command will pull fromorigin
:
- We do this with the
git pull origin
- But just as the repo we cloned from was set up as a remote repo automatically, it was also set up as the default repo to pull from.
- So we can actually omit the remote repo name, and just run
git pull
:
- So we can actually omit the remote repo name, and just run
git pull
- Now, we can run
git log
within themyclone
repo. In the output we'll see the "Add gold medals" commit, just like we did in themedals
repo.
-
0:00
When commits get added to a git repository,
-
0:02
they aren't automatically copy the clones of that repository.
-
0:06
One way to distribute the new commits is to pull them in from other repos.
-
0:10
In our original medals repo,
-
0:12
we've saved a gold.html file, that advertises our gold medals.
-
0:17
We're in the myclone repo right now, but we need to be in the medals repo.
-
0:22
So let's change into it with the cd command.
-
0:25
We can't just type cd medals, though,
-
0:27
that would indicate the medals directory lives inside the myclone directory.
-
0:33
The medals directory actually exists alongside the myclone directory in
-
0:37
the parent directory.
-
0:39
Remember how we've been typing cd… to change to the parent directory?
-
0:44
We can use that here, but now we need to indicate that we mean the medals directory
-
0:48
inside the parent directory.
-
0:50
Directory names on UNIX-like systems are joined with a slash character.
-
0:55
So we'll add a slash, then we'll type the name of the directory, medals.
-
0:59
That leaves us with ../medals,
-
1:02
you can read that as the parent directory's medals subdirectory.
-
1:07
If we run the command, it will change up to the parent directory and
-
1:11
then into the medals subdirectory.
-
1:13
You don't have to remember this double dot notation for directory names, but
-
1:17
we will be using it later in the stage.
-
1:19
So at least now you'll know what's going on.
-
1:22
Now we're in the medals repo, where our new gold.html file is saved.
-
1:27
Let's stage it with git add gold.html, and
-
1:31
now we'll commit it, git commit- m "Add gold medals".
-
1:40
Now let's run git log, we'll see the new commit in the history for
-
1:44
the medals repo, Add gold medals.
-
1:47
I'll press q to quit out of the history.
-
1:49
But will that commit also be in the history for the my clone repo?
-
1:53
Let's find out, I'll type cd ../myclone/.
-
1:58
Again, the slash here at the end of the myclone directory name is optional.
-
2:03
Let's run git log again here in the myclone repo.
-
2:06
There's no sign of the Add gold medals commit.
-
2:09
And if I quit out of the log and run ls, there's no gold.html file.
-
2:15
We need to get the commit that adds gold.html from the medals repo to
-
2:19
the myclone repo.
-
2:21
To do that, we need a link from the myclone repo back to the medals repo.
-
2:26
Within a git repository, you can add links to other repos.
-
2:30
These linked repos are referred to as remote repos.
-
2:34
We can get a list of remote repos with the git remote command.
-
2:39
We see one remote repo listed here named origin.
-
2:43
When you clone a git repo,
-
2:44
the original repo is automatically added as a remote on the clone.
-
2:49
You can name remote repos whatever you want.
-
2:51
But the default used when cloning is origin,
-
2:54
because it represents the repo this clone originated from.
-
2:58
Because of this default,
-
2:59
you'll see a remote repod name origin on most git repos you work with.
-
3:04
In fact, in many cases, the origin repo will be the only remote repo.
-
3:08
Because everyone on the project just pulls changes from that single repo.
-
3:13
Since a remote repo is already set up, we're ready to pull changes from it.
-
3:17
We do this with the git pull subcommand.
-
3:20
git pull takes an argument with the name of the remote
-
3:23
repo you want to pull changes from, so we'll pull from origin.
-
3:28
But just as the repo we cloned from was set up as a remote repo automatically,
-
3:32
it was also set up as the default repo to pull from.
-
3:35
So we can actually omit the remote repo name.
-
3:40
And just run git pull, when we run it, we'll see some info about the commits
-
3:45
being pulled over, and any files that are being added or updated.
-
3:49
We can see the gold.html file here in the output.
-
3:53
Now we can run git log within the myclone repo.
-
3:56
And here in the output,
-
3:58
we'll see the Add gold medals commit, just like we did in the medals repo.
-
4:04
And if we run ls, we'll see the gold.html file.
-
4:07
So now you know what a remote repository is and how to pull commits from one.
-
4:11
But this remote repo was added automatically for us by git.
-
4:15
What happens if you need to add a remote yourself, we'll see how to do that next.
You need to sign up for Treehouse in order to download course files.
Sign up