-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-upstream-merge
executable file
·55 lines (46 loc) · 1.82 KB
/
git-upstream-merge
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
55
#!/bin/sh
# Created by Michael Baltaks
# This assumes a local branch "upstream" that tracks your upstream remote. `git checkout -b upstream upstream/master`
# This assumes that your local master branch is always clean and only tracks the remote for your fork.
# The workflow is make local commits to a working branch. When ready to have the team review those changes, git-publish, which pushes to the remote for your local master branch (usually the origin, which should be your fork of the project repo). Then create a merge request for the team to review. Once that merge request is accepted into the main repo, run this script to merge those and all other changes into your fork and local master.
# Don't allow using unset vars (-u)
set -o nounset
# Exit on error (-e)
#set -o errexit (not used here because if git pull fails we need to clean up)
branch_name="$(git symbolic-ref HEAD 2>/dev/null)" || branch_name="(unnamed branch)" # detached HEAD
branch_name="${branch_name##refs/heads/}"
if [ "${branch_name}" = "(unnamed branch)" ]
then
echo "This will not run if you're not on a normal branch."
exit 0
fi
# stop if there is already something stashed.
stash_list=`git stash list`
if [[ ${stash_list} == *stash* ]]
then
echo "There are already stashed changes, please empty your stash before using this script"
exit 0
fi
git stash && git checkout upstream && git pull
if [ 0 = $? ]
then
git checkout master && git merge upstream
else
echo "git pull failed"
git checkout "${branch_name}" && git stash pop
fi
if [ 0 = $? ]
then
git push # push the merge commit back to origin
else
echo "git merge of upstream into master failed. Don't forget to 'git stash pop' later."
exit 1;
fi
if [ 0 = $? ]
then
git rebase master "${branch_name}" && git stash pop
exit 0;
else
echo "git push failed"
git checkout "${branch_name}" && git stash pop
fi