8

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.

Nicolai Parlog
  • 36,673
  • 16
  • 109
  • 236

3 Answers3

6

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.

Community
  • 1
  • 1
Robert
  • 311
  • 3
  • 5
1

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
Hemerson Varela
  • 19,204
  • 12
  • 60
  • 63
1

Isn't it as simple as git checkout branch-name without the -b. That creates a remote tracking branch associated with branch-name on your origin. Well, does for me. What am I not understanding about whats not working for you?

DavidN
  • 7,947
  • 2
  • 17
  • 21