33# Function to print help and manage arguments
44eval $(
55 zz_args " Fix git base - rebase commits from one branch to another" $0 " $@ " << -help
6- p - push force push changes to remote
6+ p - push push changes to remote
77 n - dry-run show what would be done without making changes
8- - source source source branch to take commits from (default: current branch)
98 - target target target branch to rebase commits onto
109 - source source source branch to take commits from (default: current branch)
1110 help
5352 zz_log i " Found merge base: $base "
5453fi
5554
56- # Get commits that are in source but not in target
57- commits=$( git log --reverse --pretty=oneline ${base} .. ${ source} | grep -vE " ^[a-f0-9]+ Merge " | awk ' {print $1} ' )
55+ # Get commits that are not pushed on the source branch
56+ commits=$( git log --reverse --format=%H " $ source" --not origin/ " $source " --not " $target " --no-merges )
5857
5958if [ -z " $commits " ]; then
6059 zz_log i " No commits to move from '$source ' to '$target '"
@@ -137,16 +136,9 @@ if [ $failed -eq 1 ]; then
137136 exit 1
138137fi
139138
140- # Reset source branch to merge base
141- zz_log i " Resetting '$source ' to merge base"
142- if ! git checkout " $source " ; then
143- zz_log e " Failed to checkout '$source ' branch"
144- # Cleanup temp branch
145- git branch -D " $temp " 2> /dev/null
146- exit 1
147- fi
148-
149- if ! git reset --hard " $base " ; then
139+ # Reset source branch to source base
140+ zz_log i " Resetting '$source ' to source base"
141+ if ! git reset --hard $source ; then
150142 zz_log e " Failed to reset '$source ' branch"
151143 exit 1
152144fi
@@ -156,42 +148,24 @@ zz_log i "Fast-forwarding '$target' branch"
156148if ! git checkout " $target " ; then
157149 zz_log e " Failed to checkout '$target ' branch"
158150 exit 1
151+ elif [ -n " $push " ]; then
152+ zz_log i " Pushing changes to remote"
153+ if ! git push origin " $target " ; then
154+ zz_log e " Failed to push '$target ' branch to remote"
155+ exit 1
156+ fi
159157fi
160158
161159if ! git merge --ff-only " $temp " ; then
162160 zz_log e " Failed to fast-forward '$target ' branch"
163161 # Cleanup temp branch
164162 git branch -D " $temp " 2> /dev/null
165163 exit 1
166- fi
167-
168- # Cleanup temporary branch
169- zz_log i " Cleaning up temporary branch"
170- git branch -D " $temp "
171-
172- # Return to original branch if it still exists and is different from source
173- if [ " $current " != " $source " ] && git rev-parse --verify " $current " > /dev/null 2>&1 ; then
174- git checkout " $current "
175164else
176- # If original branch was source, stay on target
177- zz_log i " Staying on '$target ' branch"
178- fi
179-
180- # Push changes if push flag is set
181- if [ -n " $push " ]; then
182- zz_log i " Pushing changes to remote..."
183-
184- # Push source branch (reset)
185- if git rev-parse --verify " origin/$source " > /dev/null 2>&1 ; then
186- zz_log i " Force pushing '$source ' branch"
187- git push --force-with-lease origin " $source "
188- fi
189-
190- # Push target branch (with new commits)
191- if git rev-parse --verify " origin/$target " > /dev/null 2>&1 ; then
192- zz_log i " Pushing '$target ' branch"
193- git push origin " $target "
194- fi
165+ # Cleanup temporary branch
166+ zz_log i " Cleaning up temporary branch"
167+ git branch -D " $temp "
195168fi
196169
197- zz_log i " Successfully moved commits from '$source ' to '$target '"
170+ # Log success message
171+ zz_log i " Successfully moved commits from '$source ' to '$target '"
0 commit comments