-
Notifications
You must be signed in to change notification settings - Fork 12
/
git-sync
executable file
·56 lines (43 loc) · 1.52 KB
/
git-sync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Decription
# ----------
# Script to simply sync all changes into git repository with one command. For example publish local changes to Github.
# Author: [Dmitry](http://dmi3.net) [Source](https://github.com/dmi3/bin)
# Requirements
# ----------
# 1. `sudo apt-get install git`
# 2. [Setup mergetool](https://developer.atlassian.com/blog/2015/12/tips-tools-to-solve-git-conflicts/#parade-of-merge-tools)
# Usage
# -----
# git-sync /path/to/repo
# git-sync # current dir
# git-sync # if you want sync all files
# git-sync -u # if you want only sync files explicitly added via `git add filename`
if [ "$BASH_ARGV" != "" ] && [ -d $BASH_ARGV ]; then # if last argument is dir
echo "Running in dir $BASH_ARGV."
cd $BASH_ARGV
fi
git pull
if [[ $1 == "-u" ]]; then # if first argument is -u
echo "Ignoring new files."
add_arg="-u"
else
add_arg="-A"
fi
git add $add_arg
CHANGED=$(git diff --name-status HEAD)
if [[ -z $CHANGED ]]; then
echo -e "No changes to commit.\nEverything in sync."
exit 0
fi
echo -e "Files in changelist:\n$CHANGED\nCancel and run 'git difftool HEAD'/'git diff HEAD' to review changes."
read -p "Commit message (or Ctrl+C to cancel): " commit_message
test -n "$commit_message" || commit_message="bump" #default value if empty
git commit -a -m "$commit_message"
# Try to push, if no sucess run merge and push again
git push origin master \
|| git pull \
|| git mergetool \
&& git commit -a -m "merging" \
&& git push origin master
exit 0