Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I push a new local branch to a remote Git repository and track it too?

I want to be able to do the following:

  1. Create a local branch based on some other (remote or local) branch (via git branch or git checkout -b)

  2. Push the local branch to the remote repository (publish), but make it trackable so git pull and git push will work immediately.

How do I do that?

I know about --set-upstream in Git 1.7, but that is a post-creation action. I want to find a way to make a similar change when pushing the branch to the remote repository.

like image 412
Roni Yaniv Avatar asked May 04 '10 12:05

Roni Yaniv


People also ask

How do I create a local branch to track a remote?

To create a new local branch based on a remote branch, use the "-track" option in the branch command. You can also do this by using the "checkout" command. If you want your local branch to have the same name as the remote branch, you only need to specify the name of the remote branch.

How will you create a new branch in remote and push?

But you can specify a new name when you initially push it to remote: git push -u origin [local-branch-name]:[remote-branch-name] . You can also specify your own branch name when you pull a remote branch: git branch -t -b [local-branch-name] origin/[remote-branch-name] .

How do I change the tracking on my remote branch?

Change tracking We can change a local branch tracking to a new remote by using the git push command followed by -u flag and origin your-branch-name .


1 Answers

In Git 1.7.0 and later, you can checkout a new branch:

git checkout -b <branch> 

Edit files, add and commit. Then push with the -u (short for --set-upstream) option:

git push -u origin <branch> 

Git will set up the tracking information during the push.

like image 182
Daniel Ruoso Avatar answered Oct 08 '22 04:10

Daniel Ruoso