Push Changes To A Remote
Reviewing Commits
Let's take a look at the commits that I have in my Repository.
The terminal application displaying the log of the project's commits.
I used the following log command to display these commits
$ git
log
--oneline --graph --decorate --all
These commits are only in the local repository, though. They haven't been sent up to the remote repository yet. When commits are sent to the remote a remote branch indicator will appear in the log. Since there aren't any remote branch indicators we can tell that there are no commits on the remote repository. But just to be 100% certain let's look at the remote repository on GitHub to see if there any commits.
The remote repository doesn't contain any commits, so GitHub displays the repository's setup screen.
Since we haven't sent any commits update to GitHub yet it's still showing us the setup screen to tell us how we can connect our local repository to the remote repository and send some commits. Since this is still the setup screen we can know that there are no commits in the remote repository.
Sending Commits
To send local commits to a remote repository you need to use thegit push
command. You provide the remote short name and then you supply the name of the branch that contains the commits you want to push:
$ git push
<
remote-shortname
>
<
branch
>
My remote's shortname isorigin
and the commits that I want to push are on themaster
branch. So I'll use the following command to send my commits to the remote repository on GitHub:
$ git push origin master
There a couple of things to notice:
- Depending on how you have configured GitHub and the remote URL that's being used, you might have to enter your username and password.
- this will happen if you use the HTTP version of the remote (rather than the
ssh
version) - If you have configured GitHub to use the SSH protocol and have supplied it with your SSH key then you don't need to worry about doing this step. Check the Connecting to GitHub with SSH documentation page if you're interested in using SSH with GitHub.
- this will happen if you use the HTTP version of the remote (rather than the
- If you have to enter your username and password your username will show up after typing but your password will not. So just keep typing your password and press enter when you're done.
- If you encounter any errors with your password don't worry it'll just ask you to type it in again
- Git does some compressing of things to make it smaller and then sends those off to the remote
- A new branch is created - at the very bottom it says
[new branch]
and thenmaster -
>
master
Now let's look at GitHub:
My entire project is up on GitHub!
Our project is up on GitHub - how awesome and easy was that! One cool feature that GitHub does is that it automatically shows the content of the README file which can be extremely helpful.
GitHub also displays a lot of details about our Repository. Right now it's showing that there are:
- three commits
- one branch
- one contributor
The project's main page on GitHub displays information about the repository.
Now before we move on, let's just check the local repository to see how it changed after pushing.
Run the following command:
$ git
log
--oneline --graph --decorate --all
Important: make sure to include the--decorate
and--all
flags
We now have a new marker in the output! This marker isorigin/master
and is called atracking branch. A tracking branch's name includes the shortname of the remote repository as well as the name of the branch. So the tracking branchorigin/master
is telling us that the remoteorigin
has amaster
branch that points to commit9b7d28f
(and includes all of the commits before9b7d28f
). This is really helpful because this means we can track the information of the remote Repository right here in our local one!
One_very important thing to know_is that thisorigin/master
tracking branch is not a live representation of where the branch exists on the remote repository. If a change is made to the remote repository not by us but by someone else, theorigin/master
tracking branch in our local repository will not move. We have to tell it to go check for any updates and_then_it will move. We'll look at how to do this in the next section.
Recap
Thegit push
command is used to send commits from a local repository to a remote repository.
$ git push origin master
Thegit push
command takes:
- the shortname of the remote repository you want to send commits to
- the name of the branch that has the commits you want to send