Git Adding a New Remote Branch - Detailed but Simple Example

Initial checkout (to ~/branchtest folder) since it was not specified


apollo:~max$ git clone git@bitbucket.org:inteist/branchtest.git

Let's create a file named "master.txt"


apollo:~max$ echo This is master branch > master.txt

Now let's add the file to track changes


apollo:branchtest max$ git add .
apollo:branchtest max$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#	new file:   master.txt

Now let's push the file to the master branch (until now there were no refs). The "-u" switch adds upstream tracking
apollo:branchtest max$ git push -u origin master

Now let's create a new branch and name it "new". The "-b" switch makes us immediately switch to the "new" branch


apollo:branchtest max$ git checkout -b new
Switched to a new branch 'new'

Make sure we are on the "new" branch. Indeed we are.


apollo:branchtest max$ git status
# On branch new
nothing to commit (working directory clean)

Let's add a file "new.txt"


apollo:branchtest max$ echo New addition > new.txt

It's on the file system.


apollo:branchtest max$ ls
master.txt	new.txt

But is untracked by Git just yet


apollo:branchtest max$ git status
# On branch new
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	new.txt
nothing added to commit but untracked files present (use "git add" to track)

Let's add the file to be tracked


apollo:branchtest max$ git add .
apollo:branchtest max$ git commit -m "Adding the new file while on the new branch"
[new 67f764e] Adding the new file while on the new branch
 1 file changed, 1 insertion(+)
 create mode 100644 new.txt

Let's switch back to "master" branch. The "new.txt" is not there. It is tracked under the new branch.


apollo:branchtest max$ git checkout master
Switched to branch 'master'
apollo:branchtest max$ ls
master.txt

Let's switch back to the "new" branch


apollo:branchtest max$ git checkout new
Switched to branch 'new'
apollo:branchtest max$ ls
master.txt	new.txt

Now with the "master" branch pushed and the the "new" branch created locally but not pushed to the central repository, let's clone the repository to a different folder.


apollo:max$ git clone git@bitbucket.org:inteist/branchtest.git branchtestNEW
Cloning into 'branchtestNEW'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

We checked out the "master" branch, which has the "master.txt" as expected


$ cd branchtestNEW/
$ ls
master.txt

Let's see if the "new" branch is present:


apollo:branchtestNEW max$ git checkout new
error: pathspec 'new' did not match any file(s) known to git.

Nop, it is not, of course, since we did not push it yet.

Now let's push the the "new" local branch to the remote repository. Just to make a case, I will name it "newremote" on the remote.
This is not required. If the second parameter is omitted, then the local and remote names are the same.
We are going to have the "-u" switch so it is tracked in the upstream


apollo:branchtest max$ git push -u origin new:newremote
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 305 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: bb/acl: inteist is allowed. accepted payload.
To ssh://git@bitbucket.org/inteist/branchtest.git
 * [new branch]      new -> newremote
Branch new set up to track remote branch newremote from origin.

Let's clone the repository to yet another folder.


apollo:~ max$ git clone git@bitbucket.org:inteist/branchtest.git branchtestNEWREMOTE
Cloning into 'branchtestNEWREMOTE'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.

Let's see if there is the "new" branch now.


apollo:~ max$ cd branchtestNEWREMOTE/
apollo:branchtestNEWREMOTE max$ git checkout new
error: pathspec 'new' did not match any file(s) known to git.

Surprise? No. There is no "new" branch of course because we named it "newremote" on the remote and this is how it was checked out.

Was it?


apollo:branchtestNEWREMOTE max$ git checkout newremote
Branch newremote set up to track remote branch newremote from origin.
Switched to a new branch 'newremote'

Yes, it was :)

Let's see what's there:


apollo:branchtestNEWREMOTE max$ ls
master.txt	new.txt

And what about the "master" branch?


apollo:branchtestNEWREMOTE max$ git checkout master
Switched to branch 'master'
apollo:branchtestNEWREMOTE max$ ls
master.txt

Surely enough there is only what was tracked under the "master" branch