One reason git is so popular amongst
professional software developers is the features that it
provides for working with others on a large project.
Let's suppose that I have my git repository here,
and I want to collaborate with Genevieve.
Not only do I need to get a copy of Drew's code,
but we also need to both be able to make changes and merge those changes together easily.
Drew's code is on his laptop,
and I want to work on it on mine.
Since our laptops aren't always available,
we'll make another repository on some server we both have access to.
One of us runs git init to set up an empty repository on this server.
Then I need to tell git on my laptop that I want it to work with a remote repository.
You can name the remote anything you want.
Here, I called it xyz,
as our hypothetical server is xyz.duke.edu,
and you give it the location of the remote.
The only thing this does is make an association with a remote location.
Now, I run git push,
specifying the remotes name,
in this case, xyz,
and the branch I want,
which is called master.
By default, the main branch in git repository is called master.
The set upstream option makes this the default place to push and pull.
We aren't going to go into branching,
but it's an incredibly useful feature of git.
Now, git will copy not only all my files,
but my entire revision history,
every commit I've ever made,
and the log messages that go with them over the remote.
Now, I want to make a copy of the repository on the remote.
So I'm going to use git clone.
I specify the location to clone form,
this is the same location that Drew specified,
but I'm using my own username to authenticate myself.
This will create an empty repository on my laptop and create a remote called origin,
which is automatically set as the default place to push and pull.
Then, git copies all the commits from the remote.
Now I write some more code,
test it out, and add any new files I created to git.
Then, I do git commit,
now my local repository has these changes,
but nobody else does.
Including my repository, which doesn't have any changes.
In fact, I may not even know that she's done anything.
I want to send these changes to the server,
so I run git push,
which will copy any new commits to the remote.
Meanwhile, I've been writing some code of my own on this project.
So I do get commit and create another commit in my own local repository.
Now, I'd like to make my changes visible to Genevieve,
so I run git push.
However, I ran into a problem.
When I do git push,
I get this error message.
Jeez Drew, that error message is log.
You must have really screwed up.
Well, let's take a look at it and see what it says.
It says that the remote has work that I do not have locally.
You mean all the code I worked so hard to write test and debug?
Maybe, I should get that first.
In fact, git suggests exactly that.
I should use git pull to get Genevieve's work first.
If I run git pull,
git will copy Genevieve's commit to my computer,
then it will merge my work and her work.
If we have changed different files or different parts of the same file,
git will handle this process automatically.
If we've both made different changes in the same area,
git will tell me that I have to manually figure things out.
Now, git will put Genevieve's commit,
as well as a new commit for the merged work into my repository.
Since this is a new commit,
git will ask me to enter a message.
The default message says,
"It merged our work." Which is fine with me.
Now I can try to run git push again.
This time it works just fine and copies my new commits to the server.
And then, I can get Drew's latest code by running git pull,
which will copy those new commits to my local repository.
That's an overview of the basics of working with remotes in git,
use get pull to get commits from the remote,
and used git push to send your own commits to the remote.
We talked about this in detail,
not only because it's incredibly useful in software development,
but also because it's how you interact with the grading system in this course.
You push your code to a remote and the grader pulls your code,
it grades your submission,
then commits your grade.txt file and pushes it back.
You then obtain it with git pull.