-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
init-with-feature-branches.sh
executable file
·158 lines (112 loc) · 3.54 KB
/
init-with-feature-branches.sh
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
#
# Create our Git repo, popupate it, and clone into a bare repo and a copy
#
set -e
DEV1="dev1-alice"
DEV2="dev2-bob"
REPO="repo.git"
FILE="file.txt"
BRANCH1="feature-branch-1"
BRANCH2="feature-branch-2"
BRANCH3="feature-branch-3"
function print_syntax() {
echo "# "
echo "# Syntax: $0 directory"
echo "# "
echo "# directory - The directory in which to set up sample git repos"
echo "# "
exit 1
}
if test "$1" == "" -o "$1" == "-h" -o "$1" == "--help"
then
print_syntax
fi
TARGET=$1
echo "# Cleaning up any past installs in ${TARGET}..."
rm -rfv ${TARGET} > /dev/null
echo "# Creating and cding to ${TARGET}..."
mkdir -p ${TARGET}
pushd ${TARGET} > /dev/null
echo "# Creating initial copy of archive in ${DEV1}..."
mkdir -p $DEV1
pushd $DEV1 > /dev/null
git init >/dev/null
touch .initial-checkin
git add .initial-checkin
git commit -m "Initial Checkin" >/dev/null 2>/dev/null
#
# Write a commit that will conflict
#
function commit_will_conflict() {
LINE="${1}-will-conflict"
echo "${LINE}" >> $FILE
echo "${LINE}" >> "${LINE}.txt"
git add ${FILE} "${LINE}.txt"
git commit -m "${LINE}" > /dev/null
} # End of commit_will_conflict()
#
# Write a commit that won't conflict.
#
function commit() {
LINE=$1
echo "${LINE}" >> "${LINE}.txt"
git add "${FILE}" "${LINE}.txt"
git commit -m "${LINE}" > /dev/null
}
commit_will_conflict 01-first
commit_will_conflict 02-second
commit 03-third
commit 04-fourth
popd > /dev/null
echo "# Cloning bare repo to ${REPO}..."
git clone --bare $DEV1 $REPO >/dev/null 2>/dev/null
pushd $REPO > /dev/null
git remote remove origin
git branch -m master main
popd > /dev/null
echo "# Fixing origin in ${DEV1}..."
pushd $DEV1 > /dev/null
git branch -m master main
git remote add origin ../${REPO} >/dev/null 2>/dev/null
git fetch >/dev/null 2>/dev/null
git branch --set-upstream-to=origin/main main >/dev/null 2>/dev/null
popd >/dev/null
echo "# Cloning to ${DEV2}..."
git clone ${REPO} ${DEV2} >/dev/null 2>/dev/null
echo "# Switching back to ${DEV1}..."
pushd $DEV1 > /dev/null
echo "# Creating braches..."
git checkout -b $BRANCH1 >/dev/null 2>/dev/null
commit 10-branch1
git push --set-upstream origin feature-branch-1 >/dev/null 2>/dev/null
git checkout main >/dev/null 2>/dev/null
git checkout -b $BRANCH2 >/dev/null 2>/dev/null
commit 20-branch2
git push --set-upstream origin feature-branch-2 >/dev/null 2>/dev/null
git checkout main >/dev/null 2>/dev/null
git checkout -b $BRANCH3 >/dev/null 2>/dev/null
commit 30-branch3
git push --set-upstream origin feature-branch-3 >/dev/null 2>/dev/null
git checkout main >/dev/null 2>/dev/null
commit 05-fifth
git push 2>/dev/null
echo "# "
echo "# The repo in ${DEV1} is currently two branches deep, and the commit log for main looks like this:"
echo "# "
git log --pretty=oneline --graph --all
echo "# "
echo "# To get started, drop into that directory with:"
echo "# "
echo "# cd ${TARGET}/${DEV1}/"
echo "# "
echo "# Some exercises to try with git rebase -i, in increasing difficulty: "
echo "# "
echo "# - Switch the order of commits 04-fourth and 05-fifth"
echo "# - Switch the order of commits 01-first-will-conflict and 02-second-will-conflict"
echo "# - Merge the changes of branch2 into main but NOT the changes of branch1"
echo "# - Squash the two commits in branch1 then push to origin"
echo "# - Switch the order of commits 04-fourth and 05-fifth, push to origin"
echo "# - Switch the order of commits 01-first-will-conflict and 02-second-will-conflict, THEN push to origin"
echo "# - Run git rebase -i and delete commit 05-fifth. Then recover it."
echo "# "