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, usually from your local repo to a remote repo. Pushing transfers recorded Git history (such as past commits), but it does not transfer unstaged changes or untracked files.
- To push, you need to have to the remote repo.
- Pushing is performed one branch at a time; you must specify which branch you want to push.
You can configure Git to track a pairing between a local branch and a remote branch, so in future you can push from the same local branch to the corresponding remote branch without needing to specify them again. For example, you can set your local master
branch to track the master
branch on the remote repo origin
i.e., local master
branch will track the branch origin/master
.
In the revision graph above, you see a new type of ref ( origin/master). This is a remote-tracking branch ref that represents the state of a corresponding branch in a remote repository (if you previously set up the branch to 'track' a remote branch). In this example, the master
branch in the remote origin
is also at the commit C3
(which means you have not created new commits after you pushed to the remote).
If you now create a new commit C4
, the state of the revision graph will be as follows:
Explanation: When you create C4
, the current branch master
moves to C4
, and HEAD
moves along with it. However, the master
branch in the remote origin
remains at C3
(because you have not pushed C4
yet). That is, the remote-tracking branch origin/master
is one commit behind the local branch master
(or, the local branch is one commit ahead). The origin/master
ref will move to C4
only after you push your local branch to the remote again.
Preparation Use a local repo that is connected to an empty remote repo e.g., the things
repo from previous hands-on practicals:
1 Push the master
branch to the remote. Also instruct Git to track this branch pair.
Use the git push -u <remote-repo-name> <local-branch-name>
to push the commits to a remote repository.
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.

2 Observe the remote-tracking branch origin/master
is now pointing at the same commit as the master
branch.
Use the git log --oneline --graph
to see the revision graph.
* f761ea6 (HEAD -> master, origin/master) Add colours.txt, shapes.txt
* 2bedace Add figs to fruits.txt
* d5f91de Add fruits.txt
Click the History
to see the revision graph.
- In some versions of Sourcetree, the
HEAD
ref may not be shown -- it is implied that theHEAD
ref is pointing to the same commit the currently active branch ref is pointing. - If the remote-tracking branch ref (e.g.,
origin/master
) is not showing up, you may need to enable theShow Remote Branches
option.

done!
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.
Target Add a few more commits to the same local repo, and push those commits to the remote repo.
1 Commit some changes in your local repo.
Use the git commit
command to create commits, as you did before.
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 commits that are not present in the corresponding branch in the remote repo).
git status
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
You can also use the git log --oneline --graph
command to see where the branch refs are. Note how the remote-tracking branch origin/master
is one commit behind the local master
.
e60deae (HEAD -> master) Update fruits list
f761ea6 (origin/master) Add colours.txt, shapes.txt
2bedace Add figs to fruits.txt
d5f91de Add fruits.txt
Create commits as you did before.
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).

2 Push the new commits to your fork on GitHub.
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
)
After pushing, the revision graph should look something like the following (note how both local and remote-tracking branch refs are pointing to the same commit again).
e60deae (HEAD -> master, origin/master) Update fruits list
f761ea6 Add colours.txt, shapes.txt
2bedace Add figs to fruits.txt
d5f91de Add fruits.txt
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.

After pushing the new commit to the remote, the remote-tracking branch ref should move to the new commit:

done!
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).