Skip to content

Commit a26f1e6

Browse files
authored
Merge pull request #761 from btmurrell/preference-for-create-branch
`create-branch` allows for preference for `remote` option
2 parents 1712db2 + 7d3ab83 commit a26f1e6

File tree

6 files changed

+161
-29
lines changed

6 files changed

+161
-29
lines changed

bin/git-create-branch

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#!/usr/bin/env bash
22

3+
test $# -eq 0 && echo "branch argument required." 1>&2 && exit 1
4+
5+
# preference takes lowest priority; look for remote from prefs first
6+
REMOTE_PREF=`git config git-extras.create-branch.remote`
7+
if [ -n "$REMOTE_PREF" ]; then
8+
REMOTE=$REMOTE_PREF
9+
fi
10+
311
while test $# != 0
412
do
513
case $1 in
@@ -8,7 +16,7 @@ do
816
then
917
REMOTE=$2
1018
shift
11-
else
19+
else
1220
REMOTE=origin
1321
fi
1422
;;
@@ -18,22 +26,32 @@ do
1826
shift
1927
done
2028

21-
if [[ -z $BRANCH ]] && [[ -n $REMOTE ]]
29+
# handle ambiguous `-r` option argument by shift
30+
if [[ -z $BRANCH ]] && [[ -n $REMOTE ]]
2231
then
2332
BRANCH=$REMOTE
2433
REMOTE=origin
2534
fi
2635

27-
test -z $BRANCH && echo "branch required." 1>&2 && exit 1
36+
test -z $BRANCH && echo "branch argument required." 1>&2 && exit 1
37+
2838
if [[ -n $REMOTE ]]
2939
then
30-
git ls-remote --exit-code $REMOTE &>/dev/null
31-
if [ $? = 0 ]
40+
git ls-remote --exit-code $REMOTE 1>/dev/null 2>/tmp/ls-remote-error
41+
REMOTE_EXIT=$?
42+
REMOTE_ERROR=$(</tmp/ls-remote-error)
43+
rm -f /tmp/ls-remote-error
44+
if [ $REMOTE_EXIT -eq 0 ]
3245
then
3346
git push $REMOTE HEAD:refs/heads/$BRANCH
3447
git fetch $REMOTE
3548
git checkout --track -b $BRANCH $REMOTE/$BRANCH
3649
exit $?
50+
else
51+
echo
52+
echo " Error connecting to remote '$REMOTE': $REMOTE_ERROR"
53+
echo
54+
exit $REMOTE_EXIT
3755
fi
3856
fi
3957

man/git-create-branch.1

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" generated with Ronn/v0.7.3
22
.\" http://github.com/rtomayko/ronn/tree/0.7.3
33
.
4-
.TH "GIT\-CREATE\-BRANCH" "1" "October 2017" "" "Git Extras"
4+
.TH "GIT\-CREATE\-BRANCH" "1" "June 2019" "" "Git Extras"
55
.
66
.SH "NAME"
77
\fBgit\-create\-branch\fR \- Create branches
@@ -24,26 +24,76 @@ Setup a remote tracking branch using \fBremote_name\fR\. If \fBremote_name\fR is
2424
.P
2525
The name of the branch to create\.
2626
.
27+
.SH "PREFERENCES"
28+
You may save your default preference for the \fBremote\fR option above by using \fBgit config\fR with the key \fBgit\-extras\.create\-branch\.remote\fR whose value will be the default remote when \fB[\-r|\-\-remote]\fR is not specified\.
29+
.
30+
.IP "" 4
31+
.
32+
.nf
33+
34+
$ git config git\-extras\.create\-branch\.remote lucinda
35+
.
36+
.fi
37+
.
38+
.IP "" 0
39+
.
40+
.P
41+
The command line option \fB\-r|\-\-remote\fR will override this preference\.
42+
.
2743
.SH "EXAMPLES"
44+
With no remote preference set:
45+
.
46+
.IP "" 4
47+
.
48+
.nf
49+
50+
# creates local branch \'integration\'
51+
$ git create\-branch integration
52+
53+
# creates local & remote branch \'integration\' (on default \'origin\')
54+
$ git create\-branch \-r integration
55+
56+
# creates local & remote branch \'integration\' on \'upstream\'
57+
$ git create\-branch \-r upstream integration
58+
.
59+
.fi
60+
.
61+
.IP "" 0
62+
.
63+
.P
64+
With \fBgit\-extras\.create\-branch\.remote\fR preference set to \'lucinda\':
65+
.
66+
.IP "" 4
2867
.
2968
.nf
3069

70+
# creates local & remote branch \'integration\' (on preference \'lucinda\')
3171
$ git create\-branch integration
3272

73+
# overriding preference, using default `\-r` of \'origin\'
74+
# creates local & remote branch \'integration\' on default \'origin\'
3375
$ git create\-branch \-r integration
3476

77+
# overriding preference, using specified `\-r` of \'upstream\'
78+
# creates local & remote branch \'integration\' on \'upstream\'
3579
$ git create\-branch \-r upstream integration
3680
.
3781
.fi
3882
.
83+
.IP "" 0
84+
.
3985
.SH "NOTES"
40-
As of 4\.4\.0, the default behavior has changed\. \fBgit\-create\-branch\fR will no longer automatically setup a remote tracking branch unless the \fB\-r|\-remote\fR option is specified\.
4186
.
42-
.SH "AUTHOR"
43-
Written by Jonhnny Weslley <\fIjw@jonhnnyweslley\.net\fR>
87+
.IP "\(bu" 4
88+
As of 4\.4\.0, the default behavior has changed\. \fBgit\-create\-branch\fR will no longer automatically setup a remote tracking branch unless the \fB\-r|\-remote\fR option is specified\. See additional note on preference feature in 4\.8\.0\-dev below\.
4489
.
45-
.br
46-
Modified by Mark Pitman <\fImark\.pitman@gmail\.com\fR>
90+
.IP "\(bu" 4
91+
As of 4\.8\.0\-dev, the \fBremote\fR option can be set via \fBgit config\fR preference as described in \fIPreferences\fR section\.
92+
.
93+
.IP "" 0
94+
.
95+
.SH "AUTHOR"
96+
Written by Jonhnny Weslley <\fIjw@jonhnnyweslley\.net\fR> Modified by Mark Pitman <\fImark\.pitman@gmail\.com\fR>, Brian Murrell <\fIbtmurrell@gmail\.com\fR>\.
4797
.
4898
.SH "REPORTING BUGS"
4999
<\fIhttps://github\.com/tj/git\-extras/issues\fR>

man/git-create-branch.html

Lines changed: 43 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/git-create-branch.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,62 @@ git-create-branch(1) -- Create branches
77

88
## DESCRIPTION
99

10-
Creates local branch named &lt;branchname&gt; and optionally sets up a remote tracking branch.
10+
Creates local branch named &lt;branchname&gt; and optionally sets up a remote tracking branch.
1111

1212
## OPTIONS
1313

14-
&lt;-r|--remote [remote_name]&gt;
14+
&lt;-r|--remote [remote_name]&gt;
1515

16-
Setup a remote tracking branch using `remote_name`. If `remote_name` is not supplied, use `origin` by default.
16+
Setup a remote tracking branch using `remote_name`. If `remote_name` is not supplied, use `origin` by default.
1717

18-
&lt;branchname&gt;
18+
&lt;branchname&gt;
1919

20-
The name of the branch to create.
20+
The name of the branch to create.
21+
22+
## PREFERENCES
23+
24+
You may save your default preference for the `remote` option above by using `git config` with the key `git-extras.create-branch.remote` whose value will be the default remote when `[-r|--remote]` is not specified.
25+
26+
$ git config git-extras.create-branch.remote lucinda
27+
28+
The command line option `-r|--remote` will override this preference.
2129

2230
## EXAMPLES
2331

32+
With no remote preference set:
33+
34+
# creates local branch 'integration'
35+
$ git create-branch integration
36+
37+
# creates local & remote branch 'integration' (on default 'origin')
38+
$ git create-branch -r integration
39+
40+
# creates local & remote branch 'integration' on 'upstream'
41+
$ git create-branch -r upstream integration
42+
43+
With `git-extras.create-branch.remote` preference set to 'lucinda':
44+
45+
# creates local & remote branch 'integration' (on preference 'lucinda')
2446
$ git create-branch integration
2547

48+
# overriding preference, using default `-r` of 'origin'
49+
# creates local & remote branch 'integration' on default 'origin'
2650
$ git create-branch -r integration
2751

52+
# overriding preference, using specified `-r` of 'upstream'
53+
# creates local & remote branch 'integration' on 'upstream'
2854
$ git create-branch -r upstream integration
2955

3056
## NOTES
3157

32-
As of 4.4.0, the default behavior has changed. `git-create-branch` will no longer automatically setup a remote tracking branch unless the `-r|-remote` option is specified.
58+
* As of 4.4.0, the default behavior has changed. `git-create-branch` will no longer automatically setup a remote tracking branch unless the `-r|-remote` option is specified. See additional note on preference feature in 4.8.0-dev below.
59+
60+
* As of 4.8.0-dev, the `remote` option can be set via `git config` preference as described in [Preferences](#PREFERENCES) section.
61+
3362
## AUTHOR
3463

35-
Written by Jonhnny Weslley &lt;<[email protected]>&gt;
36-
Modified by Mark Pitman &lt;<[email protected]>&gt;
64+
Written by Jonhnny Weslley &lt;<[email protected]>&gt;
65+
Modified by Mark Pitman &lt;<[email protected]>&gt;, Brian Murrell &lt;<[email protected]>&gt;.
3766

3867
## REPORTING BUGS
3968

man/git-extras.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ git-extras(1) -- Awesome GIT utilities
6868
- **git-refactor(1)** Create refactor branch
6969
- **git-release(1)** Commit, tag and push changes to the repository
7070
- **git-rename-branch(1)** rename local branch and push to remote
71+
- **git-rename-remote(1)** Rename a remote
7172
- **git-rename-tag(1)** Rename a tag
7273
- **git-repl(1)** git read-eval-print-loop
7374
- **git-reset-file(1)** Reset one file

man/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ git-rebase-patch(1) git-rebase-patch
4747
git-refactor(1) git-refactor
4848
git-release(1) git-release
4949
git-rename-branch(1) git-rename-branch
50+
git-rename-remote(1) git-rename-remote
5051
git-rename-tag(1) git-rename-tag
5152
git-repl(1) git-repl
5253
git-reset-file(1) git-reset-file

0 commit comments

Comments
 (0)