Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Collaborating with other developers is one of the biggest reasons to use Git. And to do that, you need to be able to bring changes from _their_ Git repositories into _your_ repo. You also need to be able to share changes from _your_ repo to _their_ repos. You do that using __remote branches__.
A remote branch is like a local branch, but it points to the commit that a branch in a remote repo is at. You can update a local branch with commits from a remote repository by merging a remote branch into a local branch.
- Normally, remote branches are used with a repo on a different machine, but they'll also work if we clone a repo to a second local directory.
- This will also let us see the effects on both sides as we push and pull commits, so let's do a local clone.
- We'll have our original repo act as the remote repo, and our clone act as the local repo.
- I'm in the original repo right now, so I'll change to the directory that contains it with
cd ..
. Remember that the..
stands for the parent directory. - We clone the repo with the
git clone
command. In place of a URL, we'll have it clone from thedecoder
directory. We'll set up the clone in thedecoder-local
directory:git clone decoder decoder-local
- Let's change into the directory for our so-called "local" repo, the clone:
cd decoder-local
- As always, when you clone a repo, the original repo is set up as a remote repo named
origin
.- We can see this if we run the
git remote
command.
- We can see this if we run the
- If we run the
git branch
command, we'll see that Git has also set up amaster
branch for us.- That's the only branch visible for now. There's no sign of the
add-letters
branch from the remote repo.
- That's the only branch visible for now. There's no sign of the
- If we run
git status
, we'll see something new.- At the top, it says "On branch master", as usual.
- But then below that, it says "Your branch is up to date with 'origin/master'."
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
-
origin/master
is a remote branch.- You can tell a remote branch name when you see one, because it always has the remote repo name, a slash, and a branch name.
Let's try using the remote branch to bring a commit from the remote repo to the local repo. I'll be using the fetch
and merge
subcommands for this.
- Change to the original repo, with
cd ../decoder/
. This changes up to the parent directory, and then immediately changes into thedecoder
directory. - Make sure the
master
branch is checked out:git checkout master
- Now let's edit the
decoder.rb
file, and add a new letter conversion.
26 => 'Z'
- Now let's stage the file:
git add decoder.rb
- And commit it:
git commit -m 'Add Z conversion'
- Now we have a new commit on the
master
branch in our remote repo. Let's go to our local repo and use the remote branch to copy that commit over. - I need to change to the clone's directory
cd ../decoder-local/
. Again, that will change to the parent directory, and into thedecoder-local
subdirectory:cd ../decoder-local
- To connect to the remote repo and update our remote branches in this local repo, we use the
git fetch
command. We need to givegit fetch
an argument with the name of the remote repo to connect to:git fetch origin
$ git fetch origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /Users/jay/th/decoder
a305f48..93252af master -> origin/master
- Now, if I run
git status
again, you'll see that themaster
local branch is out of sync with theorigin/master
remote branch. - This is because the
origin/master
branch has been updated, but themaster
branch hasn't.
$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
- We can update the local
master
branch by merging the remoteorigin/master
branch into it. - To do that, we use the
git merge
command, just as we would with any other branch:git merge origin/master
$ git merge origin/master
Updating a305f48..93252af
Fast-forward
decoder.rb | 1 +
1 file changed, 1 insertion(+)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up