In this video, you will learn to push commits from
a local repository to a remote repository.
We will start by discussing the two fundamental options when starting to work with
a remote repository, cloning and adding.
There are two ways to start working with a remote repository,
it depends on if you already have a local repository.
In other words, it depends on if you already have some work done via
commits in a local repository that you want to push to a remote repository.
If you do not have an existing local repository,
then you will clone the remote repository,
creating a local repository that is associated with the remote repository.
If you already have a local repository with
commits that you want to push to a remote repository,
then you will add the remote repository to your local repository.
We will begin by discussing
the first scenario where you do not have a local repository and
you want to clone a remote repository.
We will start by cloning a remote Bitbucket repository.
The word clone can be used as both a noun and a verb.
As a noun, a clone is a local copy of a remote repository.
As a verb, cloning is a process of creating a clone.
Once you have cloned a repository,
you can work with the local repository,
optionally pushing your commits to and pulling commits from the remote repository.
A reference to the remote repository is included in the local repository.
This allows you to synchronize the repositories.
If you have a remote repository in Bitbucket,
you can easily clone that repository to your SourceTree client,
creating a local repository.
In Bitbucket, with the remote repository selected,
click the plus sign,
then select clone this repository,
then select clone in SourceTree to begin
the process of cloning the remote repository locally.
At this point, SourceTree will open,
default information is filled in for you
specifying the project directory and repository name.
When this information is correct,
you can click clone to create the local repository.
The local repository has now been created and you can see under
the remotes tab that this repository is associated with a remote repository.
Notice that the remote repository is referred to as origin in the local repository.
This is a convenient name commonly used as
a substitute for the URL of the remote repository.
At any time, you can view
the specific remote repository information by clicking on settings and then remotes.
This shows that origin is a shorthand name for the remote repository URL.
We have seen how to clone a repository from Bitbucket to SourceTree.
Now we will see how to clone any remote repository into SourceTree.
In SourceTree, you can clone a remote repository hosted anywhere,
all that you need is the remote repository URL.
Here's an example of a remote repository hosted on GitHub.
You can see that the URL of the repository is easily available.
To clone this repository in SourceTree,
start by copying this URL.
After copying the remote URL,
open SourceTree and select new,
then click clone from URL.
The window on the right will open,
allowing you to paste the remote URL,
as well as specify the project directory and name of the local repository.
When the information is correct,
click clone and you will have a local clone of the remote repository.
Next, we will add a remote repository to a local repository.
This assumes that we are starting with
an existing local repository and want to add an association
to a remote repository so that we can synchronize them.
We have seen that there are two fundamental starting scenarios
when you want to work with a remote repository.
We've already covered the scenario where you do not start with a local repository.
In that case, we cloned the remote repository.
Now, we will cover the scenario where you have commits in
a local repository and you want to add
an association to a remote repository so that you can synchronize them.
If you already have a local repository and want to
add a remote repository so that you can synchronize with it,
first navigate to the local repository in Sourcetree,
right-click on remotes and then select new remote.
You then must specify the remote information.
You can specify a name for the remote repository,
and origin is a common name to use.
For the URL, copy and paste the clone URL from the hosting provider.
You optionally can fill in your information for the hosting provider to
provide a smoother integration between SourceTree and the hosting provider.
If the remote repository is hosted on Bitbucket,
this information should fill in for you.
When the information is correct, click okay.
A remote repository should now be associated with the local repository.
This allows you to easily synchronize the repositories by fetching,
pulling, and pushing, which we'll learn about later.
Finally, we will discuss pushing commits from
the local repository to the remote repository.
Before we talk about pushing commits to a repository,
let's take a moment to remember what branches
are because they are related to pushing commits.
As a reminder, all commits belong to a branch.
A branch can be thought of as an independent line of development of the project.
By default, there's a single branch and it is called master.
In this example, commits A, B,
and C all belong to the master branch.
So far in this course,
we've not created any branches so all of
our commits have been made to the default master branch.
We will cover branches in much more detail later in the course.
A push copies commits for a branch from the local repository to the remote repository.
A successful push synchronizes the branch on
the local and remote repositories so that they contain exactly the same commits.
Pushing to the remote repository is a good backup of
the local repository and allows other team members to see and synchronize your work.
In SourceTree, when you have commits in
a local repository that you want to push to the remote repository,
you begin by clicking the push button,
then you select the branch to push.
In this case, we are pushing the master branch on
the local repository to the master branch on the remote repository.
Select the track checkbox to set up a relationship between these branches in the future.
SourceTree can then inform you when the branches are out of sync.
Click okay to push the commits to the remote repository.
Assuming the push of your first commit is successful,
the local and remote repositories contain the same commit.
You can see that the commit is labeled with both the master branch from
the local repository and the origin/master branch of the remote repository.
Sometimes, when you try to push,
there are conflicts with what others have pushed to the repository,
which we will discuss later in the course.
For now, pushing your first commit to an empty remote repository should be successful.
Once you have successfully pushed a commit to Bitbucket from SourceTree,
you can log into Bitbucket,
click on the commits tab and verify that the commit is in the remote repository.
To review what we've discussed,
if you want to begin working with a remote repository,
you have two main options.
If you do not have a local repository,
you clone the remote repository and a local repository will be created.
If you already have a local repository with
commits that you want to push to the remote repository,
you can add the remote repository from the local repository.
Once you have associated the repositories,
you can push commits from the local repository to the remote repository.
That is just the start.
We will discuss synchronizing the repositories more later in the course.
Now, it's time for you to push commits to a remote repository.
Separate hands-on instructions will guide you through the process.
You will first clone your remote Bitbucket repository,
creating a local repository,
then you will create a commit in
the local repository and push that commit to the remote repository.