Created
December 20, 2022 22:37
-
-
Save jphalip/b7d0bc22c908eac956d2adae733796c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Takes two parameters: target branch and commit message. | |
# Does a hard reset on the target branch then adds a single commit with the diff | |
# between the two branches. Sort of simulates a rebase but without having to | |
# resolve potential conflicts in intermediary commits. | |
function git-hard-rebase() { | |
target_branch=$1 | |
if git rev-parse --quiet --verify ${target_branch} > /dev/null; then | |
else | |
echo "Branch name ${target_branch} does not exist." | |
return 1 | |
fi | |
message=$2 | |
if test -z "$message" | |
then | |
echo "Message is empty" | |
return 1 | |
fi | |
current_branch=$(git branch --show-current) | |
file_diff=$(mktemp "/tmp/${current_branch}.${target_branch}.XXXXXX") | |
echo ${file_diff} | |
git diff ${target_branch} > ${file_diff} | |
git branch "backup/${current_branch}-$(date '+%Y_%m_%d-%H_%M_%S')" | |
git reset --hard ${target_branch} | |
git apply ${file_diff} | |
git add -A | |
git commit -m ${message} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment