Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit 87fce31

Browse files
committed
Add git-rebase-all
1 parent dd6dc8f commit 87fce31

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ This script was intended to quickly deploy changes to heroku when using the git
2323
## git-full-restore
2424

2525
Small script to quickly remove all changes from a file and restore it to the last commit. This includes if the files are
26-
staged or not.
26+
staged or not.
27+
28+
## git-rebase-all
29+
30+
Script that will pull in the latest copy of `origin/master`, and then rebase all other local branches around
31+
origin/master. It will abort a rebase if it encounters merge conflicts. This is very useful to rebase all branches upon
32+
a merge occuring to the origin/master branch.

bin/git-rebase-all

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,67 @@ TMPDIR=$(mktemp -d -t "git-rebase-all")
44
log="branchdata.log"
55

66
progress() {
7-
local w=120 p=$1; shift
8-
# create a string of spaces, then change them to dots
9-
printf -v dots "%*s" "$(( $p*$w/100 ))" ""; dots=${dots// /.};
10-
# print those dots on a fixed-width space plus the percentage etc.
11-
printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*";
7+
local w=120 p=$1
8+
shift
9+
# create a string of spaces, then change them to dots
10+
printf -v dots "%*s" "$((p * w / 100))" ""
11+
dots=${dots// /.}
12+
# print those dots on a fixed-width space plus the percentage etc.
13+
printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*"
1214
}
1315

14-
1516
on_fail() {
16-
local branch="$1"
17-
local branch_log="$TMPDIR/branch-$branch.log"
18-
echo "Failed to rebase $branch" >> "$TMPDIR/$log"
19-
git rebase --abort >> "$branch_log" 2>&1
17+
local branch="$1"
18+
local branch_log="$TMPDIR/branch-$branch.log"
19+
echo "Failed to rebase $branch" >>"$TMPDIR/$log"
20+
git rebase --abort >>"$branch_log" 2>&1
2021
}
2122

2223
on_success() {
23-
local branch="$1"
24-
local branch_log="$TMPDIR/branch-$branch.log"
25-
echo "Successfully rebased branch $branch" >> "$TMPDIR/$log"
26-
git push --force >> "$branch_log" 2>&1
24+
local branch="$1"
25+
local branch_log="$TMPDIR/branch-$branch.log"
26+
echo "Successfully rebased branch $branch" >>"$TMPDIR/$log"
27+
git push --force >>"$branch_log" 2>&1
2728
}
2829

2930
update_counter() {
30-
counter="$(( $counter+${1:-1} ))"
31-
progress $(( ($counter*100)/${branch_count:-$counter} ))
31+
counter="$((counter + 1))"
32+
progress $((counter * 100 / ${branch_count:-$counter}))
3233
}
3334

3435
{
35-
branches="$(git branch | sed "s/\*//g" | sed "s/ //g")"
36-
branch_count="$(echo ${branches} | wc -w| tr -d '\040\011\012\015')"
36+
branches="$(git branch | sed "s/\*//g" | sed "s/ //g")"
37+
branch_count="$(echo "${branches}" | wc -w | tr -d '\040\011\012\015')"
3738
}
3839

39-
git cleanbranches
40+
git checkout master
41+
git pull
4042

41-
for branch in ${branches};
42-
do {
43+
for branch in ${branches}; do
44+
{
4345
branch_log="$TMPDIR/branch-$branch.log"
44-
echo Processing branch ${branch} >> ${branch_log} 2>&1
45-
git checkout ${branch} >> ${branch_log} 2>&1
46-
if git rebase upstream/master >> ${branch_log} 2>&1
47-
then {
48-
on_success ${branch}
49-
}
50-
else {
51-
on_fail ${branch}
52-
}
46+
echo Processing branch "${branch}" >>"${branch_log}" 2>&1
47+
git checkout "${branch}" >>"${branch_log}" 2>&1
48+
if git rebase origin/master >>"${branch_log}" 2>&1; then
49+
{
50+
on_success "${branch}"
51+
}
52+
else
53+
{
54+
on_fail "${branch}"
55+
}
5356
fi
5457
update_counter
55-
}
58+
}
5659
done
5760

58-
git checkout master >> /dev/null 2>& 1
61+
git checkout master >>/dev/null 2>&1
5962
{
60-
echo
61-
echo "Printing log"
62-
cat "$TMPDIR/$log"
63-
echo
64-
echo "View rest of logs at $TMPDIR"
65-
ls -FGl $TMPDIR
63+
echo
64+
echo "Printing log"
65+
cat "$TMPDIR/$log"
66+
echo
67+
echo "View rest of logs at $TMPDIR"
68+
ls -FGl "$TMPDIR"
6669
}
6770
exit 0

0 commit comments

Comments
 (0)