Repositories

Remote repositories

What makes Git so great is that it allows multiple people to work on the same project at the same time.

But how can we coordinate work when our repository is stored on our local computer?

git remote

That's it! We can create remote repositories on websites like Github and share them publicly or privately with others.

Cloning

As soon as we have a link to a remote repository, we can download, or clone, it to our local computer.

$ git clone https://github.com/Elliot/WhiteRose.git

//Output Below

Cloning into 'WhiteRose'...
remote: Counting objects: 51, done.
remote: Total 51, reused 0
Unpacking objects: 100% (51/51), done.
Checking connectivity... done. 

Fantastic! When we use git clone with the link to the repository, Git downloads the repository to the directory we're currently in.

Viewing remotes

Now that we've cloned WhiteRose, let's take a closer look at the remote repository!

$ cd WhiteRose
$ git remote 

//Output Below

origin

Nice! Using git remote, we can see that the git clone command from earlier added WhiteRose as origin.

Origin

But where does the origin remote actually point to?

$ git remote show origin 

//Output Below

* remote origin  
Fetch URL: https://github.com/Elliot/WhiteRose.git  
Push URL: https://github.com/Elliot/WhiteRose.git  
HEAD branch: master  
Remote branch:    
master tracked
...

There! Using git remote show with the short name of the remote, we can see that it points to URLs and that its HEAD points to master branch.

Psst: origin is just a convention, a name that's automatically given to a remote for which we don't specify a name.

Fetching changes

If the remote has changes in its, say, master branch that we want to bring to our local repository, we need to fetch them.

$ git fetch origin master

//Output Below

remote: Counting objects: 3, done.
remote: Total 3, reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/Elliot/WhiteRose.git 
* branch            master     -> FETCH_HEAD   
c07222e..1b18076  master     -> origin/master

Great! Using git fetch with origin and master brings these changes to a branch in our local repository called origin/master.

Merging fetched changes

Now that we've fetched the changes, we need to merge them from the origin/master branch into master.

$ git merge origin/master master

//Output Below

Updating 4b45192..c239b52
Fast-forward cookie.txt
1 insertion(+), 5 deletions(-)

Sweet! We've used git merge to bring the changes we fetched into origin/master to the master branch of our local repository.

Pulling changes

Now, pulling is what we do to bring a branch in our local repository up-to-date with its remote version.

$ git pull origin master 

//Output Below

remote: Counting objects: 3, done.
remote: Total 3, reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/Elliot/WhiteRose.git 
* branch            master     -> FETCH_HEAD   
c07222e..1b18076  master     -> origin/master
Updating c07222e..1b18076
Fast-forward cookie.txt
1 file changed, 2 insertions

Great! Notice how similar this is to fetching and merging at the same time? Well, that's exactly what git pull does.

Pushing

Finally, let's find out how we can get our local changes to the remote repository!

$ git add README.md
$ git commit -m "Add title to README"
$ git push origin master

//Output Below

Counting objects: 3, done.
Writing objects: 100% (3/3), 292 bytes
Total 3, reused 0
To https://github.com/Elliot/WhiteRose.git 
1b18076..c07222e master -> master 

Nice! git push, well, pushes commits from our local repository to the remote.

Psst: we need to commit changes before we can push them to the remote.