From 12e416a324bb08dfa5c72dee8aa5b4dcfb6ea53e Mon Sep 17 00:00:00 2001 From: Takuma Yamaguchi Date: Thu, 14 Jan 2016 00:19:12 +0900 Subject: [PATCH 1/4] add git-sync --- Commands.md | 10 ++++ bin/git-sync | 61 +++++++++++++++++++++++++ man/git-sync.1 | 35 ++++++++++++++ man/git-sync.html | 113 ++++++++++++++++++++++++++++++++++++++++++++++ man/git-sync.md | 30 ++++++++++++ 5 files changed, 249 insertions(+) create mode 100755 bin/git-sync create mode 100644 man/git-sync.1 create mode 100644 man/git-sync.html create mode 100644 man/git-sync.md diff --git a/Commands.md b/Commands.md index a294dbd..ab899f0 100644 --- a/Commands.md +++ b/Commands.md @@ -43,6 +43,7 @@ - [`git setup`](#git-setup) - [`git squash`](#git-squash) - [`git summary`](#git-summary) + - [`git sync`](#git-sync) - [`git touch`](#git-touch) - [`git undo`](#git-undo) - [`git unlock`](#git-unlock) @@ -987,3 +988,12 @@ $ git psykorebase master feature ``` The above rebase `feature` branch on top of `master` branch + +## git sync + +Sync local branch with remote branch + +```bash +$ git sync origin master +``` + diff --git a/bin/git-sync b/bin/git-sync new file mode 100755 index 0000000..765f19a --- /dev/null +++ b/bin/git-sync @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +function _usage() +{ + local command="git sync" + cat << EOS +Usage: + ${command} + ${command} -h | --help | help | ? + +Sync local branch with of . +All changes and untracked files and directories will be removed. + +Example: + ${command} origin master +EOS +} + +function main() +{ + while [ "$1" != "" ]; do + case $1 in + -h | --help | help | "?" ) + _usage + ;; + * ) + if [ "${remote}" = "" ]; then + local remote="$1" + elif [ "${branch}" = "" ]; then + local branch="$1" + else + echo -e "Error: too many arguments.\n" + _usage + exit 1 + fi + ;; + esac + shift + done + + if [ "${remote}" = "" -o "${branch}" = "" ]; then + echo -e "Error: too few arguments.\n" + _usage + exit 1 + fi + + echo "Are you sure you want to clean all changes & sync with '${remote}/${branch}'? [y/N]" + local res + read res + case ${res} in + "Y" | "y" | "yes" | "Yes" | "YES" ) + git fetch '${remote}' && git reset --hard '${remote}'/'${branch}' && git clean -d -f -x + ;; + * ) + echo "Canceled." + ;; + esac +} + +main "$@" + diff --git a/man/git-sync.1 b/man/git-sync.1 new file mode 100644 index 0000000..90a4954 --- /dev/null +++ b/man/git-sync.1 @@ -0,0 +1,35 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "GIT\-SYNC" "1" "January 2016" "" "" +. +.SH "NAME" +\fBgit\-sync\fR \- Sync local branch with remote branch +. +.SH "SYNOPSIS" +\fBgit sync\fR +. +.SH "DESCRIPTION" +Sync local branch with of \. All changes and untracked files and directories will be removed\. +. +.SH "EXAMPLES" +Sync local branch with origin/master +. +.IP "" 4 +. +.nf + +$ git sync origin master +. +.fi +. +.IP "" 0 +. +.SH "AUTHOR" +Written by Takuma Yamaguchi <\fIkumon0587@gmail\.com\fR> +. +.SH "REPORTING BUGS" +<\fIhttps://github\.com/tj/git\-extras/issues\fR> +. +.SH "SEE ALSO" +<\fIhttps://github\.com/tj/git\-extras\fR> diff --git a/man/git-sync.html b/man/git-sync.html new file mode 100644 index 0000000..05a5c1f --- /dev/null +++ b/man/git-sync.html @@ -0,0 +1,113 @@ + + + + + + git-sync(1) - Sync local branch with remote branch + + + + +
+ + + +
    +
  1. git-sync(1)
  2. +
  3. +
  4. git-sync(1)
  5. +
+ +

NAME

+

+ git-sync - Sync local branch with remote branch +

+ +

SYNOPSIS

+ +

git sync <remote> <branch>

+ +

DESCRIPTION

+ +

Sync local branch with <branch> of <remote>. + All changes and untracked files and directories will be removed.

+ +

EXAMPLES

+ +

Sync local branch with origin/master

+ +
$ git sync origin master
+
+ +

AUTHOR

+ +

Written by Takuma Yamaguchi <kumon0587@gmail.com>

+ +

REPORTING BUGS

+ +

<https://github.com/tj/git-extras/issues>

+ +

SEE ALSO

+ +

<https://github.com/tj/git-extras>

+ + +
    +
  1. +
  2. January 2016
  3. +
  4. git-sync(1)
  5. +
+ +
+ + diff --git a/man/git-sync.md b/man/git-sync.md new file mode 100644 index 0000000..8854634 --- /dev/null +++ b/man/git-sync.md @@ -0,0 +1,30 @@ +git-sync(1) -- Sync local branch with remote branch +================================================================= + +## SYNOPSIS + + `git sync` <remote> <branch> + +## DESCRIPTION + + Sync local branch with <branch> of <remote>. + All changes and untracked files and directories will be removed. + +## EXAMPLES + + Sync local branch with origin/master + + $ git sync origin master + +## AUTHOR + +Written by Takuma Yamaguchi <> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<> + From 3481ff245ecf99dd283b75d70b68e05684ceeee3 Mon Sep 17 00:00:00 2001 From: Takuma Yamaguchi Date: Sat, 16 Jan 2016 01:40:10 +0900 Subject: [PATCH 2/4] upstream is used by default --- bin/git-sync | 38 +++++++++++++++++++++++++------------- man/git-sync.1 | 23 +++++++++++++++++++++-- man/git-sync.html | 16 ++++++++++++---- man/git-sync.md | 12 +++++++++--- 4 files changed, 67 insertions(+), 22 deletions(-) diff --git a/bin/git-sync b/bin/git-sync index 765f19a..cbb6845 100755 --- a/bin/git-sync +++ b/bin/git-sync @@ -5,14 +5,19 @@ function _usage() local command="git sync" cat << EOS Usage: - ${command} - ${command} -h | --help | help | ? + ${command} [ ] + ${command} -h | --help -Sync local branch with of . +Sync local branch with /. +When and 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." diff --git a/man/git-sync.1 b/man/git-sync.1 index 90a4954..498b2f2 100644 --- a/man/git-sync.1 +++ b/man/git-sync.1 @@ -7,12 +7,31 @@ \fBgit\-sync\fR \- Sync local branch with remote branch . .SH "SYNOPSIS" -\fBgit sync\fR +\fBgit sync\fR [ ] . .SH "DESCRIPTION" -Sync local branch with of \. All changes and untracked files and directories will be removed\. +Sync local branch with /\. +. +.P +When and 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 diff --git a/man/git-sync.html b/man/git-sync.html index 05a5c1f..234cdb0 100644 --- a/man/git-sync.html +++ b/man/git-sync.html @@ -75,15 +75,23 @@

SYNOPSIS

-

git sync <remote> <branch>

+

git sync [ <remote> <branch> ]

DESCRIPTION

-

Sync local branch with <branch> of <remote>. - All changes and untracked files and directories will be removed.

+

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.

EXAMPLES

+

Sync local branch with its upstream

+ +
$ git sync
+
+

Sync local branch with origin/master

$ git sync origin master
@@ -91,7 +99,7 @@
 
 

AUTHOR

-

Written by Takuma Yamaguchi <kumon0587@gmail.com>

+

Written by Takuma Yamaguchi <kumon0587@gmail.com>

REPORTING BUGS

diff --git a/man/git-sync.md b/man/git-sync.md index 8854634..f2f6e43 100644 --- a/man/git-sync.md +++ b/man/git-sync.md @@ -3,15 +3,22 @@ git-sync(1) -- Sync local branch with remote branch ## SYNOPSIS - `git sync` <remote> <branch> + `git sync` [ <remote> <branch> ] ## DESCRIPTION - 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. ## 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 <> ## SEE ALSO <> - From 19ece6192e21612642e7fbdbf6c4c7beb946bd0a Mon Sep 17 00:00:00 2001 From: Takuma Yamaguchi Date: Sat, 16 Jan 2016 01:47:30 +0900 Subject: [PATCH 3/4] add example of git-sync --- Commands.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Commands.md b/Commands.md index ab899f0..6fd1cbc 100644 --- a/Commands.md +++ b/Commands.md @@ -991,7 +991,13 @@ The above rebase `feature` branch on top of `master` branch ## git sync -Sync local branch with remote branch +Sync local branch with its remote branch + +```bash +$ git sync +``` + +Sync local branch with origin/master ```bash $ git sync origin master From f0ecd8212922b83975aff78e61bf0f21273061c1 Mon Sep 17 00:00:00 2001 From: Takuma Yamaguchi Date: Sun, 17 Jan 2016 00:56:45 +0900 Subject: [PATCH 4/4] add upstream check & bugfix --- bin/git-sync | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/git-sync b/bin/git-sync index cbb6845..8de036a 100755 --- a/bin/git-sync +++ b/bin/git-sync @@ -44,16 +44,20 @@ function main() shift done + local remote_branch 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}}" + if ! remote_branch="$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)"; then + echo "There is no upstream information of local branch." + exit 1 + fi + local branch="$(git rev-parse --abbrev-ref --symbolic-full-name @)" + local remote=$(git config "branch.${branch}.remote") elif [ "${branch}" = "" ]; then echo -e "Error: too few arguments.\n" _usage exit 1 else - local remote_branch="${remote}/${branch}" + remote_branch="${remote}/${branch}" fi echo "Are you sure you want to clean all changes & sync with '${remote_branch}'? [y/N]" @@ -61,7 +65,7 @@ function main() read res case "${res}" in "Y" | "y" | "yes" | "Yes" | "YES" ) - echo "git fetch "${remote}" "${branch}" && git reset --hard "${remote_branch}" && git clean -d -f -x" + git fetch "${remote}" "${branch}" && git reset --hard "${remote_branch}" && git clean -d -f -x ;; * ) echo "Canceled."