The third step of backing up a local repo on GitHub: push a copy of the local repo to the remote repo.
You can push content of one repository to another. Pushing can transfer Git history (e.g., past commits) as well as files in the working directory. Note that pushing to a remote repo requires you to have write-access to it.
When pushing to a remote repo, you typically need to specify the following information:
- The name of the remote (e.g.,
origin
). - The name of your current local branch (e.g.,
master
).
If this is the first time you are pushing this branch to the remote repo, you can also ask Git to track this remote/branch pairing (e.g., remember that this local master
branch is tracking the master
branch in the repo origin
i.e., local master
branch is tracking upstream origin/master
branch), so in future you can push the same remote/branch without needing to specify them again.
Here, we assume you already have a local repo that is connected to an empty remote repo, from previous hands-on practicals:
# format: git push -u <remote-repo-name> <branch-name>
git push -u origin master
Explanation:
push
: the Git sub-command that pushes the current local repo content to a remote repoorigin
: name of the remotemaster
: branch to push-u
(or--set-upstream
): the flag that tells Git to track that this localmaster
is trackingorigin/master
branch
Click the Push
button on the buttons ribbon at the top.

In the next dialog, ensure the settings are as follows, ensure the Track
option is selected, and click the Push
button on the dialog.

The push command can be used repeatedly to send further updates to another repo e.g., to update the remote with commits you created since you pushed the first time.
Add a few more commits to your local repo, and push those commits to the remote repo, as follows:
1 Commit some changes in your local repo.
2 Push the new commits to your fork on GitHub
Optionally, you can run the git status
command, which should confirm that your local branch is 'ahead' by one commit (i.e., the local branch has one new commit that is not in the corresponding branch in the remote repo).
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
To push the newer commit(s) to the remote, any of the following commands should work:
git push origin master
git push origin
(due to tracking you set up earlier, Git will assume you are pushing themaster
branch)git push
(due to tracking, Git will assume you are pushing to the remoteorigin
and to the branchmaster
i.e.,origin/master
)
Before pushing the new commit, Sourcetree will indicate that your local branch is 'ahead' by one commit (i.e., the local branch has one new commit that is not in the corresponding branch in the remote repo).

To push, click the Push
button on the top buttons ribbon, ensure the settings are as follows in the next dialog, and click the Push
button on the dialog.

Note that you can push between two repos only if those repos have a shared history among them (i.e., one should have been created by copying the other).