A Remote Repository is a repository present on some Git hosting websites like GitHub or BitBucket. Remote repositories help developers share their work and collaborate with others.
A branch is simply a pointer to a commit. Branches can exist locally on our system(local branches) or on the remote repositories(remote branches). A Branch helps developers create an independent workspace and test out new features without affecting the main workflow of the project.
Pushing a local branch to a remote repository is a way of sharing our work with other collaborators. Let's learn how to do this.
We first need to create a new local branch and add our work. If you already have a local branch, go to the next step.
We can use the Git Branch command to create a new branch.
There are a few variations that can be used.
git branch <new-branch> # create new branch based on the current branch
git branch <new-branch> <existing-branch> # create new branch based on the given existing branch
git branch <new-branch> <commit-hash> # create new branch based on the given commit hash
After creating a new branch, we need to switch to that branch. Use the Git Checkout command to check out(or switch to) a branch.
git checkout <new-branch>
We can also create and check out a new local branch in a single step by using the Git Checkout command with the -b flag.
It will first create a new local branch and then switch to it.
git checkout -b <new-branch> # create and checkout a new branch based on the current branch
​
git checkout -b <new-branch> <exisiting-branch> # create and checkout a new branch based on the given exisiting branch
​
git checkout -b <new-branch> <commit-hash> # create and checkout a new branch based on the given commit hash
We can push a local branch to the remote repo using the Git Push command. Since we have created a new local branch, there won't be any corresponding upstream branch.
We will use the --set-upstream option or -u flag to set an upstream connection. It makes pushing and pulling easier.
git push --set-upstream <remote-name> <new-branch-name>
git push -u <remote-name> <new-branch-name>
Let's try to understand the entire process with the help of an example. Consider we have a remote repository with a single master branch and three commits(A, B, C) on it.
Remote Repository
​
A -- B -- C <- master
We have cloned this repo to our local system.
Local Repository
​
A -- B -- C <- master <- HEAD, origin/master
Now, we have to create a new feature branch and add some commits. Let's use Git Checkout to create and check out a new feature branch based on commit B.
git checkout -b feature <commit-B-hash>
To find the hash of commit B, use the Git Log command.
git log --oneline
Output:
efd5b5c (HEAD -> master, origin/master, origin/HEAD) Commit C
a8eb35e Commit B
0e03c73 Commit A
Now, let's add two commits to the feature branch.
Local Repository
A -- B -- C <- master, origin/master
\
\
D -- E <- feature <- HEAD
To share our work with other developers, we need to push the feature branch to the remote repository. Let's use the Git Push command.
git push -u origin feature
Output:
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 425 bytes | 85.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
remote:
remote: Create a pull request for 'feature' on GitHub by visiting:
remote: https://github.com/pankajshivnani2001/Demo/pull/new/feature
remote:
To https://github.com/pankajshivnani2001/Demo
* [new branch] feature -> feature
Branch 'feature' set up to track remote branch 'feature' from 'origin'.
The feature branch is added to the remote repository. An upstream connection has been set up between our local feature branch and remote feature branch.
This can be verified by the last line of the output above. The state of the remote repository is shown below.
Remote Repository
A -- B -- C <- master
\
\
D -- E <- feature
Also, we will have a new remote-tracking branch origin/feature in our local repo. This branch will track the changes of the remote feature branch.
Local Repository
A -- B -- C <- master, origin/master
\
\
D -- E <- feature <- HEAD, origin/feature
Remote repositories help us to collaborate and share our work with others. Branches allow developers to create an independent workflow and experiment with things. Pushing a new local branch to a remote repository is pretty simple. Git provides the Git Push command for this. When pushing for the first time, we need to set up an upstream connection.
A summary of the steps to be followed is given below.
Create a new branch(if needed) using Git Branch or Git Checkout commands.
Add commits to the new branch that you created.
Push the new branch to the remote by using Git Push with --set-upstream or -u.