How Do I Push a New Local Branch to a Remote Git Repository and Track It Too?

Better Stack Team
Updated on June 24, 2024

To push a new local branch to a remote Git repository and track it, you can use the git push command with the --set-upstream or -u option. Here's how you can do it:

 
git push -u origin <local-branch-name>

Replace <local-branch-name> with the name of your new local branch.

Explanation:

  • git push: This command is used to push changes from your local repository to a remote repository.
  • u or -set-upstream: This option tells Git to set up tracking so that your local branch will track the remote branch with the same name on the remote repository (origin).
  • origin: This is the default name Git gives to the remote repository. If your remote repository has a different name, replace origin with the appropriate remote name.
  • <local-branch-name>: This is the name of the new local branch you want to push to the remote repository.