Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions bin/git-create-branch
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#!/usr/bin/env bash

test $# -eq 0 && echo "branch argument required." 1>&2 && exit 1

# preference takes lowest priority; look for remote from prefs first
REMOTE_PREF=`git config git-extras.create-branch.remote`
if [ -n "$REMOTE_PREF" ]; then
REMOTE=$REMOTE_PREF
fi

while test $# != 0
do
case $1 in
Expand All @@ -8,7 +16,7 @@ do
then
REMOTE=$2
shift
else
else
REMOTE=origin
fi
;;
Expand All @@ -18,22 +26,32 @@ do
shift
done

if [[ -z $BRANCH ]] && [[ -n $REMOTE ]]
# handle ambiguous `-r` option argument by shift
if [[ -z $BRANCH ]] && [[ -n $REMOTE ]]
then
BRANCH=$REMOTE
REMOTE=origin
fi

test -z $BRANCH && echo "branch required." 1>&2 && exit 1
test -z $BRANCH && echo "branch argument required." 1>&2 && exit 1

if [[ -n $REMOTE ]]
then
git ls-remote --exit-code $REMOTE &>/dev/null
if [ $? = 0 ]
git ls-remote --exit-code $REMOTE 1>/dev/null 2>/tmp/ls-remote-error
REMOTE_EXIT=$?
REMOTE_ERROR=$(</tmp/ls-remote-error)
rm -f /tmp/ls-remote-error
if [ $REMOTE_EXIT -eq 0 ]
then
git push $REMOTE HEAD:refs/heads/$BRANCH
git fetch $REMOTE
git checkout --track -b $BRANCH $REMOTE/$BRANCH
exit $?
else
echo
echo " Error connecting to remote '$REMOTE': $REMOTE_ERROR"
echo
exit $REMOTE_EXIT
fi
fi

Expand Down
62 changes: 56 additions & 6 deletions man/git-create-branch.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "GIT\-CREATE\-BRANCH" "1" "October 2017" "" "Git Extras"
.TH "GIT\-CREATE\-BRANCH" "1" "June 2019" "" "Git Extras"
.
.SH "NAME"
\fBgit\-create\-branch\fR \- Create branches
Expand All @@ -24,26 +24,76 @@ Setup a remote tracking branch using \fBremote_name\fR\. If \fBremote_name\fR is
.P
The name of the branch to create\.
.
.SH "PREFERENCES"
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\.
.
.IP "" 4
.
.nf

$ git config git\-extras\.create\-branch\.remote lucinda
.
.fi
.
.IP "" 0
.
.P
The command line option \fB\-r|\-\-remote\fR will override this preference\.
.
.SH "EXAMPLES"
With no remote preference set:
.
.IP "" 4
.
.nf

# creates local branch \'integration\'
$ git create\-branch integration

# creates local & remote branch \'integration\' (on default \'origin\')
$ git create\-branch \-r integration

# creates local & remote branch \'integration\' on \'upstream\'
$ git create\-branch \-r upstream integration
.
.fi
.
.IP "" 0
.
.P
With \fBgit\-extras\.create\-branch\.remote\fR preference set to \'lucinda\':
.
.IP "" 4
.
.nf

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

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

# overriding preference, using specified `\-r` of \'upstream\'
# creates local & remote branch \'integration\' on \'upstream\'
$ git create\-branch \-r upstream integration
.
.fi
.
.IP "" 0
.
.SH "NOTES"
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\.
.
.SH "AUTHOR"
Written by Jonhnny Weslley <\fIjw@jonhnnyweslley\.net\fR>
.IP "\(bu" 4
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\.
.
.br
Modified by Mark Pitman <\fImark\.pitman@gmail\.com\fR>
.IP "\(bu" 4
As of 4\.8\.0\-dev, the \fBremote\fR option can be set via \fBgit config\fR preference as described in \fIPreferences\fR section\.
.
.IP "" 0
.
.SH "AUTHOR"
Written by Jonhnny Weslley <\fIjw@jonhnnyweslley\.net\fR> Modified by Mark Pitman <\fImark\.pitman@gmail\.com\fR>, Brian Murrell <\fIbtmurrell@gmail\.com\fR>\.
.
.SH "REPORTING BUGS"
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
Expand Down
53 changes: 43 additions & 10 deletions man/git-create-branch.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 37 additions & 8 deletions man/git-create-branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,62 @@ git-create-branch(1) -- Create branches

## DESCRIPTION

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

## OPTIONS

&lt;-r|--remote [remote_name]&gt;
&lt;-r|--remote [remote_name]&gt;

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

&lt;branchname&gt;
&lt;branchname&gt;

The name of the branch to create.
The name of the branch to create.

## PREFERENCES

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.

$ git config git-extras.create-branch.remote lucinda

The command line option `-r|--remote` will override this preference.

## EXAMPLES

With no remote preference set:

# creates local branch 'integration'
$ git create-branch integration

# creates local & remote branch 'integration' (on default 'origin')
$ git create-branch -r integration

# creates local & remote branch 'integration' on 'upstream'
$ git create-branch -r upstream integration

With `git-extras.create-branch.remote` preference set to 'lucinda':

# creates local & remote branch 'integration' (on preference 'lucinda')
$ git create-branch integration

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

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

## NOTES

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.
* 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.

* As of 4.8.0-dev, the `remote` option can be set via `git config` preference as described in [Preferences](#PREFERENCES) section.

## AUTHOR

Written by Jonhnny Weslley &lt;<[email protected]>&gt;
Modified by Mark Pitman &lt;<[email protected]>&gt;
Written by Jonhnny Weslley &lt;<[email protected]>&gt;
Modified by Mark Pitman &lt;<[email protected]>&gt;, Brian Murrell &lt;<[email protected]>&gt;.

## REPORTING BUGS

Expand Down
1 change: 1 addition & 0 deletions man/git-extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ git-extras(1) -- Awesome GIT utilities
- **git-refactor(1)** Create refactor branch
- **git-release(1)** Commit, tag and push changes to the repository
- **git-rename-branch(1)** rename local branch and push to remote
- **git-rename-remote(1)** Rename a remote
- **git-rename-tag(1)** Rename a tag
- **git-repl(1)** git read-eval-print-loop
- **git-reset-file(1)** Reset one file
Expand Down
1 change: 1 addition & 0 deletions man/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ git-rebase-patch(1) git-rebase-patch
git-refactor(1) git-refactor
git-release(1) git-release
git-rename-branch(1) git-rename-branch
git-rename-remote(1) git-rename-remote
git-rename-tag(1) git-rename-tag
git-repl(1) git-repl
git-reset-file(1) git-reset-file
Expand Down