0

With one git command and a string argument <topic-name> how can I,

  1. Create a local branch named topic/<topic-name>
  2. Checkout this branch
  3. Ensure the first git push with no args pushes to origin/topic/<topic-name> without warnings or prompts
  4. Ensure that subsequent git push commands with no args push to origin/topic/<topic-name> without warnings or prompts

Assume origin/topic/<topic-name> does not exist on as a branch on the remote repo.

I'm open to the use of git aliases to achieve this.

Janek Bogucki
  • 5,033
  • 3
  • 30
  • 40

2 Answers2

0

This seems to work,

git config --global alias.topic '!f() { b="topic/$1"; git checkout -b "$b"; git config branch."$b".remote origin; git config branch."$b".merge refs/heads/"$b"; }; f'

Output

$ git topic SI-9008
Switched to a new branch 'topic/SI-9008'
$ git push
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create pull request for topic/SI-9008:
remote:   https://bitbucket.org/xxx/xxx/pull-requests/new?source=topic/SI-9008&t=1
remote: 
To bitbucket.org:xxx/xxx.git
* [new branch]          topic/SI-9008 -> topic/SI-9008

Based on git- Creating a branch which will be pushed to a remote later

Community
  • 1
  • 1
Janek Bogucki
  • 5,033
  • 3
  • 30
  • 40
-1

Add the following to your git conifg, as explained here:

[alias]
    setupBranch = "!f() { git checkout -b topic/"$1" origin/topic/"$1"; }; f"

this will setup an alias setupBranch that executes git checkout -b with the parameters you specified.

Edit: Be aware however, that this only works if branch origin/topic/<topic-name> exists, else it will fail with a fatal: Cannot update paths and switch to branch 'topic/<topic-name>' at the same time.

kowsky
  • 12,647
  • 2
  • 28
  • 41
  • Thanks. I will try this but if the error holds true then I will need to look again for a different solution. – Janek Bogucki Apr 25 '17 at 11:13
  • 1
    @JanekBogucki: I'm not sure what you mean by "if the error holds true", but fundamentally, the problem is that you cannot set `origin/blah` as a remote-tracking branch for `blah` if `origin/blah` does not exist yet. If you intend to *create* an `origin/blah` on the remote, you have to actually *do* that, so that `origin/blah` does in fact now exist. This leaves a problem: without first contacting `origin` and asking it, you don't know if `origin/blah` exists yet (though you can check your own remote-tracking branches to see if you got one the *last* time you talked with `origin`). – torek Apr 25 '17 at 11:33
  • I meant if the solution does not work then I would continue looking for a solution. I have updated the problem to remove references to remote tracking branches. Of course ultimately a remote tracking branch will exist by the time the first `git push` is done but I'm not stipulating when it exists. – Janek Bogucki Apr 25 '17 at 16:12
  • Defining the alias with ```git config --global alias.topic '!f() { git checkout -b topic/"$1" origin/topic/"$1"; }; f'``` led to the error noted: ```git topic test-topic fatal: Cannot update paths and switch to branch 'topic/test-topic' at the same time. Did you intend to checkout 'origin/topic/test-topic' which can not be resolved as commit?``` – Janek Bogucki Apr 25 '17 at 18:56