Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new branch with tracking information

Tags:

git

Here's my usual workflow:

  1. create new a branch: git checkout -b foo
  2. commit some stuff
  3. do a push: git push
  4. get angry that push does not work (no upstream set)
  5. reach to mouse to highlight git's recommended command (still angry)
  6. push with setting upstream: git push --set-upstream origin foo (anger subsides)

Instead of 4. to 6., I would like to do some work when creating the new branch locally (without necessarily making my branch public yet, so no pushing) that kills steps 4. through 6. Is that possible?

Ideally something like git checkout -b foo -t origin, which informs git that I plan to track a branch of the same name in origin.

What I Tried

git checkout -b foo --set-upstream origin foo ~> error: unknown option 'set-upstream'

git checkout --track origin/foo ~> fatal: Cannot update paths and switch to branch 'foo' at the same time.

git checkout -b foo --track origin/foo ~> fatal: Cannot update paths and switch to branch 'foo' at the same time

git checkout -b foo --track ~> Branch foo set up to track local branch master.

like image 377
Nicolai Parlog Avatar asked Jun 16 '16 13:06

Nicolai Parlog


People also ask

How do you create a new branch that tracks remote branches?

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.

What are tracking branches and how you can setup one?

Checking out a local branch from a remote branch automatically creates what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch. If you're on a tracking branch and type git push , Git automatically knows which server and branch to push to.

What is a git tracking branch?

They're used to link what you're working on locally compared to what's on the remote. They will automatically know what remote branch to get changes from when you use git pull or git fetch . Even better, git status will recognize him how many commits you are in front of the remote version of the branch.

How do I create a new branch?

New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.

What is a tracked remote branch?

Remote-tracking branches are references to the state of remote branches. They're local references that you can't move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository.


2 Answers

There is an answer to a slightly different topic that may help you with your workflow (not 100% sure).

You can make this happen with less typing. First, change the way your push works:

git config --global push.default current

This will infer the origin my_branch part, thus you can do:

git push -u

Which will both create the remote branch with the same name and track it.

Actually you can even omit the -u and it should still work.

like image 180
Robert Avatar answered Nov 15 '22 21:11

Robert


If you want to set upstream on the step 1. You would be breaking the concept of git as a distributed version control system. You can do that with another version control system as svn.

Alternatively, you can use -u flag on git push to set upstream, before commit the files.

git branch branch-name
git push -u origin branch-name
...
git commit ...
...
git push
like image 44
Hemerson Varela Avatar answered Nov 15 '22 20:11

Hemerson Varela