How to Push and Track a New Local Branch to a Remote Repository in Git

One of the most common steps in the process of working with Git is pushing and tracking a new local branch to a remote repository.

Here is a step-by-step guideline, explaining how to do it in a relatively fast way.

For creating a new branch and switching from the current branch to the new one, you can use the git checkout command by running:

git checkout -b <branch>

Then you need to go on with editing files, adding and committing them.

The next step is pushing the local branch to the remote repository using the git push command, like this:

git push -u origin <branch>

The tracking will be set up by Git during the pushing.

Description of Git Branch

The git branch command allows creating new branches to your project.

For viewing the branches of your git repository, you need to run the following command:

git branch

To view both remote and local branches, invoke:

git branch -a

You will notice an asterisk (*) near the branch you are currently working on.

Creating a New Git Branch

For creating a new git branch, it is necessary to run the following command:

git branch NEW-BRANCH-NAME

The new branch will include the commits of the parent branch. The parent branch is the one you are on while generating a new branch.

It is important to note that the above-given command will only create a branch. For switching to it you need to invoke the git checkout command:

git checkout BRANCH-NAME

Renaming a Git Branch

For renaming a git branch, you need to invoke the following command:

git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME

Description of Git Checkout

As it was already mentioned, the git checkout command allows switching between branches. It works with git branch. Git checkout updates the files in the working directory to agree with the version that is in that branch, alerting Git to record all the new commits. Git checkout is also associated with the git clone command. The latter is aimed at fetching code from the remote repository. On the contrary, git checkout is switching code versions on the local repository.


Do you find this helpful?

Related articles