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
16 changes: 16 additions & 0 deletions Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
```

77 changes: 77 additions & 0 deletions bin/git-sync
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env bash

function _usage()
{
local command="git sync"
cat << EOS
Usage:
${command} [<remote> <branch>]
${command} -h | --help

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 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 "$@"

54 changes: 54 additions & 0 deletions man/git-sync.1
Original file line number Diff line number Diff line change
@@ -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 [ <remote> <branch> ]
.
.SH "DESCRIPTION"
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
.
.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>
121 changes: 121 additions & 0 deletions man/git-sync.html

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

36 changes: 36 additions & 0 deletions man/git-sync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
git-sync(1) -- Sync local branch with remote branch
=================================================================

## SYNOPSIS

`git sync` [ &lt;remote&gt; &lt;branch&gt; ]

## DESCRIPTION

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

## AUTHOR

Written by Takuma Yamaguchi &lt;<[email protected]>&gt;

## REPORTING BUGS

&lt;<https://github.com/tj/git-extras/issues>&gt;

## SEE ALSO

&lt;<https://github.com/tj/git-extras>&gt;