upstream is used by default

This commit is contained in:
Takuma Yamaguchi 2016-01-16 01:40:10 +09:00
parent 12e416a324
commit 3481ff245e
4 changed files with 67 additions and 22 deletions

View file

@ -5,14 +5,19 @@ function _usage()
local command="git sync"
cat << EOS
Usage:
${command} <remote> <branch>
${command} -h | --help | help | ?
${command} [<remote> <branch>]
${command} -h | --help
Sync local branch with <branch> of <remote>.
Sync local branch with <remote>/<branch>.
When <remote> and <branch> are not specified on the command line, upstream of local branch will be used by default.
All changes and untracked files and directories will be removed.
Example:
${command} origin master
Examples:
Sync with upstream of local branch:
${command}
Sync with origin/master:
${command} origin master
EOS
}
@ -20,8 +25,9 @@ function main()
{
while [ "$1" != "" ]; do
case $1 in
-h | --help | help | "?" )
-h | --help)
_usage
exit
;;
* )
if [ "${remote}" = "" ]; then
@ -38,18 +44,24 @@ function main()
shift
done
if [ "${remote}" = "" -o "${branch}" = "" ]; then
echo -e "Error: too few arguments.\n"
_usage
exit 1
if [ "${remote}" = "" ]; then
local remote_branch="$(git rev-parse --abbrev-ref --symbolic-full-name @{u})"
branch="$(git rev-parse --abbrev-ref --symbolic-full-name @)"
remote="${remote_branch%%/${branch}}"
elif [ "${branch}" = "" ]; then
echo -e "Error: too few arguments.\n"
_usage
exit 1
else
local remote_branch="${remote}/${branch}"
fi
echo "Are you sure you want to clean all changes & sync with '${remote}/${branch}'? [y/N]"
echo "Are you sure you want to clean all changes & sync with '${remote_branch}'? [y/N]"
local res
read res
case ${res} in
case "${res}" in
"Y" | "y" | "yes" | "Yes" | "YES" )
git fetch '${remote}' && git reset --hard '${remote}'/'${branch}' && git clean -d -f -x
echo "git fetch "${remote}" "${branch}" && git reset --hard "${remote_branch}" && git clean -d -f -x"
;;
* )
echo "Canceled."

View file

@ -7,12 +7,31 @@
\fBgit\-sync\fR \- Sync local branch with remote branch
.
.SH "SYNOPSIS"
\fBgit sync\fR <remote> <branch>
\fBgit sync\fR [ <remote> <branch> ]
.
.SH "DESCRIPTION"
Sync local branch with <branch> of <remote>\. All changes and untracked files and directories will be removed\.
Sync local branch with <remote>/<branch>\.
.
.P
When <remote> and <branch> are not specified on the command line, upstream of local branch will be used by default\.
.
.P
All changes and untracked files and directories will be removed\.
.
.SH "EXAMPLES"
Sync local branch with its upstream
.
.IP "" 4
.
.nf
$ git sync
.
.fi
.
.IP "" 0
.
.P
Sync local branch with origin/master
.
.IP "" 4

View file

@ -75,15 +75,23 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p> <code>git sync</code> &lt;remote&gt; &lt;branch&gt;</p>
<p> <code>git sync</code> [ &lt;remote&gt; &lt;branch&gt; ]</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
<p> Sync local branch with &lt;branch&gt; of &lt;remote&gt;.
All changes and untracked files and directories will be removed.</p>
<p> Sync local branch with &lt;remote&gt;/&lt;branch&gt;.</p>
<p> When &lt;remote&gt; and &lt;branch&gt; are not specified on the command line, upstream of local branch will be used by default.</p>
<p> All changes and untracked files and directories will be removed.</p>
<h2 id="EXAMPLES">EXAMPLES</h2>
<p> Sync local branch with its upstream</p>
<pre><code>$ git sync
</code></pre>
<p> Sync local branch with origin/master</p>
<pre><code>$ git sync origin master
@ -91,7 +99,7 @@
<h2 id="AUTHOR">AUTHOR</h2>
<p>Written by Takuma Yamaguchi &lt;<a href="&#109;&#x61;&#x69;&#108;&#116;&#111;&#58;&#107;&#x75;&#109;&#111;&#x6e;&#x30;&#53;&#56;&#55;&#64;&#103;&#x6d;&#97;&#105;&#108;&#x2e;&#99;&#x6f;&#109;" data-bare-link="true">&#107;&#117;&#x6d;&#x6f;&#110;&#x30;&#53;&#x38;&#55;&#64;&#x67;&#109;&#97;&#x69;&#108;&#x2e;&#99;&#x6f;&#x6d;</a>&gt;</p>
<p>Written by Takuma Yamaguchi &lt;<a href="&#x6d;&#97;&#x69;&#x6c;&#116;&#x6f;&#x3a;&#107;&#x75;&#109;&#x6f;&#x6e;&#x30;&#53;&#56;&#55;&#64;&#x67;&#x6d;&#97;&#105;&#108;&#x2e;&#99;&#111;&#109;" data-bare-link="true">&#107;&#117;&#109;&#x6f;&#110;&#48;&#x35;&#x38;&#x37;&#64;&#103;&#109;&#97;&#105;&#108;&#x2e;&#x63;&#111;&#x6d;</a>&gt;</p>
<h2 id="REPORTING-BUGS">REPORTING BUGS</h2>

View file

@ -3,15 +3,22 @@ git-sync(1) -- Sync local branch with remote branch
## SYNOPSIS
`git sync` &lt;remote&gt; &lt;branch&gt;
`git sync` [ &lt;remote&gt; &lt;branch&gt; ]
## DESCRIPTION
Sync local branch with &lt;branch&gt; of &lt;remote&gt;.
Sync local branch with &lt;remote&gt;/&lt;branch&gt;.
When &lt;remote&gt; and &lt;branch&gt; are not specified on the command line, upstream of local branch will be used by default.
All changes and untracked files and directories will be removed.
## EXAMPLES
Sync local branch with its upstream
$ git sync
Sync local branch with origin/master
$ git sync origin master
@ -27,4 +34,3 @@ Written by Takuma Yamaguchi &lt;<kumon0587@gmail.com>&gt;
## SEE ALSO
&lt;<https://github.com/tj/git-extras>&gt;