Creating and Pushing Branches

Create a branch and push it to the remote repository.

We'll cover the following

In this section, you’re going to familiarise yourself with how branches are managed between the two and what exactly goes on in a push.

Setup#

First, set up a simple origin Git repository and clone it like you did before.

1   mkdir -p lgthw_pushing
2   cd lgthw_pushing
3   mkdir git_origin
4   cd git_origin
5   git init
6   echo 'first commit' > file1
7   git add file1
8   git commit -am file1
9   cd ..
10  git clone git_origin git_clone
Terminal 1
Terminal

Click to Connect...

As you can see, there are no branches on either the origin or the clone other than the default (master):

11  cd git_origin
12  git branch -a -v
13  cd ../git_clone
14  git branch -a -v

Make sure you understand why there are three lines in the second git branch output! If you don’t, start the chapter again!

Creating branch and pushing it#

Now you’re going to create a branch on the clone, do some work on it, and then push it to the remote repository.

This is a common use-case, as users may experiment with different branches locally and then decide they want to share their work with others by pushing it to a commonly-accessible remote repository, e.g., on GitHub.

15  git checkout -b abranch
16  echo 'cloned abranch commit' >> file1
17  git commit -am 'cloned abranch commit'
18  git push origin abranch
Terminal 1
Terminal

Click to Connect...

The key bit there was the git push command. The first item after the push specifies the remote (which is origin by default) and the branch is the next item (abranch here).

Git will create a branch on the remote repository for you if one does not already exist.

Introduction: Pushing Code
Pushing to Repositories With Different Content
Mark as Completed
Report an Issue