diff --git a/Commands.md b/Commands.md index a294dbd..6fd1cbc 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,18 @@ $ git psykorebase master feature ``` The above rebase `feature` branch on top of `master` branch + +## git sync + +Sync local branch with its remote branch + +```bash +$ git sync +``` + +Sync local branch with origin/master + +```bash +$ git sync origin master +``` + diff --git a/bin/git-sync b/bin/git-sync new file mode 100755 index 0000000..8de036a --- /dev/null +++ b/bin/git-sync @@ -0,0 +1,77 @@ +#!/usr/bin/env bash + +function _usage() +{ + local command="git sync" + cat << EOS +Usage: + ${command} [ ] + ${command} -h | --help + +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. + +Examples: + Sync with upstream of local branch: + ${command} + + Sync with origin/master: + ${command} origin master +EOS +} + +function main() +{ + while [ "$1" != "" ]; do + case $1 in + -h | --help) + _usage + exit + ;; + * ) + 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 + + local remote_branch + if [ "${remote}" = "" ]; then + 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 + remote_branch="${remote}/${branch}" + 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}" "${branch}" && 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..498b2f2 --- /dev/null +++ b/man/git-sync.1 @@ -0,0 +1,54 @@ +.\" 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 /\. +. +.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 +. +.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..234cdb0 --- /dev/null +++ b/man/git-sync.html @@ -0,0 +1,121 @@ + + + + + + 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 <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
+
+ +

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..f2f6e43 --- /dev/null +++ b/man/git-sync.md @@ -0,0 +1,36 @@ +git-sync(1) -- Sync local branch with remote branch +================================================================= + +## SYNOPSIS + + `git sync` [ <remote> <branch> ] + +## DESCRIPTION + + 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 + +## AUTHOR + +Written by Takuma Yamaguchi <> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<>