Skip to content

Commit 98d0d8c

Browse files
committed
Merge pull request #500 from kumon/master
Add "sync" command.
2 parents 642eede + f0ecd82 commit 98d0d8c

File tree

5 files changed

+304
-0
lines changed

5 files changed

+304
-0
lines changed

Commands.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [`git setup`](#git-setup)
4444
- [`git squash`](#git-squash)
4545
- [`git summary`](#git-summary)
46+
- [`git sync`](#git-sync)
4647
- [`git touch`](#git-touch)
4748
- [`git undo`](#git-undo)
4849
- [`git unlock`](#git-unlock)
@@ -987,3 +988,18 @@ $ git psykorebase master feature
987988
```
988989

989990
The above rebase `feature` branch on top of `master` branch
991+
992+
## git sync
993+
994+
Sync local branch with its remote branch
995+
996+
```bash
997+
$ git sync
998+
```
999+
1000+
Sync local branch with origin/master
1001+
1002+
```bash
1003+
$ git sync origin master
1004+
```
1005+

bin/git-sync

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
3+
function _usage()
4+
{
5+
local command="git sync"
6+
cat << EOS
7+
Usage:
8+
${command} [<remote> <branch>]
9+
${command} -h | --help
10+
11+
Sync local branch with <remote>/<branch>.
12+
When <remote> and <branch> are not specified on the command line, upstream of local branch will be used by default.
13+
All changes and untracked files and directories will be removed.
14+
15+
Examples:
16+
Sync with upstream of local branch:
17+
${command}
18+
19+
Sync with origin/master:
20+
${command} origin master
21+
EOS
22+
}
23+
24+
function main()
25+
{
26+
while [ "$1" != "" ]; do
27+
case $1 in
28+
-h | --help)
29+
_usage
30+
exit
31+
;;
32+
* )
33+
if [ "${remote}" = "" ]; then
34+
local remote="$1"
35+
elif [ "${branch}" = "" ]; then
36+
local branch="$1"
37+
else
38+
echo -e "Error: too many arguments.\n"
39+
_usage
40+
exit 1
41+
fi
42+
;;
43+
esac
44+
shift
45+
done
46+
47+
local remote_branch
48+
if [ "${remote}" = "" ]; then
49+
if ! remote_branch="$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)"; then
50+
echo "There is no upstream information of local branch."
51+
exit 1
52+
fi
53+
local branch="$(git rev-parse --abbrev-ref --symbolic-full-name @)"
54+
local remote=$(git config "branch.${branch}.remote")
55+
elif [ "${branch}" = "" ]; then
56+
echo -e "Error: too few arguments.\n"
57+
_usage
58+
exit 1
59+
else
60+
remote_branch="${remote}/${branch}"
61+
fi
62+
63+
echo "Are you sure you want to clean all changes & sync with '${remote_branch}'? [y/N]"
64+
local res
65+
read res
66+
case "${res}" in
67+
"Y" | "y" | "yes" | "Yes" | "YES" )
68+
git fetch "${remote}" "${branch}" && git reset --hard "${remote_branch}" && git clean -d -f -x
69+
;;
70+
* )
71+
echo "Canceled."
72+
;;
73+
esac
74+
}
75+
76+
main "$@"
77+

man/git-sync.1

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.\" generated with Ronn/v0.7.3
2+
.\" http://github.com/rtomayko/ronn/tree/0.7.3
3+
.
4+
.TH "GIT\-SYNC" "1" "January 2016" "" ""
5+
.
6+
.SH "NAME"
7+
\fBgit\-sync\fR \- Sync local branch with remote branch
8+
.
9+
.SH "SYNOPSIS"
10+
\fBgit sync\fR [ <remote> <branch> ]
11+
.
12+
.SH "DESCRIPTION"
13+
Sync local branch with <remote>/<branch>\.
14+
.
15+
.P
16+
When <remote> and <branch> are not specified on the command line, upstream of local branch will be used by default\.
17+
.
18+
.P
19+
All changes and untracked files and directories will be removed\.
20+
.
21+
.SH "EXAMPLES"
22+
Sync local branch with its upstream
23+
.
24+
.IP "" 4
25+
.
26+
.nf
27+
28+
$ git sync
29+
.
30+
.fi
31+
.
32+
.IP "" 0
33+
.
34+
.P
35+
Sync local branch with origin/master
36+
.
37+
.IP "" 4
38+
.
39+
.nf
40+
41+
$ git sync origin master
42+
.
43+
.fi
44+
.
45+
.IP "" 0
46+
.
47+
.SH "AUTHOR"
48+
Written by Takuma Yamaguchi <\fIkumon0587@gmail\.com\fR>
49+
.
50+
.SH "REPORTING BUGS"
51+
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
52+
.
53+
.SH "SEE ALSO"
54+
<\fIhttps://github\.com/tj/git\-extras\fR>

man/git-sync.html

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/git-sync.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
git-sync(1) -- Sync local branch with remote branch
2+
=================================================================
3+
4+
## SYNOPSIS
5+
6+
`git sync` [ &lt;remote&gt; &lt;branch&gt; ]
7+
8+
## DESCRIPTION
9+
10+
Sync local branch with &lt;remote&gt;/&lt;branch&gt;.
11+
12+
When &lt;remote&gt; and &lt;branch&gt; are not specified on the command line, upstream of local branch will be used by default.
13+
14+
All changes and untracked files and directories will be removed.
15+
16+
## EXAMPLES
17+
18+
Sync local branch with its upstream
19+
20+
$ git sync
21+
22+
Sync local branch with origin/master
23+
24+
$ git sync origin master
25+
26+
## AUTHOR
27+
28+
Written by Takuma Yamaguchi &lt;<[email protected]>&gt;
29+
30+
## REPORTING BUGS
31+
32+
&lt;<https://github.com/tj/git-extras/issues>&gt;
33+
34+
## SEE ALSO
35+
36+
&lt;<https://github.com/tj/git-extras>&gt;

0 commit comments

Comments
 (0)