tj / git-extras

GIT utilities -- repo summary, repl, changelog population, author commit percentages and more
MIT License
17.33k stars 1.21k forks source link

proposal: git-flick, move a commit to another branch #33

Open FND opened 13 years ago

FND commented 13 years ago

While working on a feature branch, I often come across and fix various minor issues (e.g. code readability). Ideally such commits should live on a separate branch to avoid obscuring feature changes.

Based on IRC conversations (#git on FreeNode), here's what I came up with (tentatively called git-flick, for lack of inspiration):

#!/usr/bin/env bash

set -e
set -x

treeish=${1:?}
target_branch=${2:?}

git checkout "$target_branch"
git cherry-pick "$treeish"
# return to original branch
git checkout @{-1}
# remove commit from original branch
# NB: rebase shortens treeish to seven characters; rebasing from commit's parent
GIT_EDITOR="grep -v ${treeish:0:7}" git rebase -p -i "${treeish}^"

(This is not extensively tested yet.)

If this is deemed of general interest, I could submit a pull request.

tj commented 13 years ago

so it's basically a cherry-pick in reverse? punting it at the target branch? sounds good to me

FND commented 13 years ago

so it's basically a cherry-pick in reverse?

Exactly. However, turns out the above script doesn't work reliably; seems like the last line's rebase call is not passed the grep-filtered output, so nothing actually changes. Needs investigation...

tj commented 13 years ago

I like the idea though, when I have a chance I'll whip something up

tj commented 13 years ago

maybe git punt sha1 to branch to being optional because it sounds cool :D haha

FND commented 13 years ago

punt seems like a good name.

However, perhaps this whole thing not such a good idea after all, as it kinda encourages rebasing (which should generally be avoided, unless you really know what you're doing - and it's not even explicit here).

Maybe this should at least accept multiple commit hashes, thus encouraging users to only do only a single, conscious punt/rebase? (This would also mean not just automatically passing it through grep; perhaps use sed to prefix the respective lines and then pass it to $EDITOR as usual?)

FND commented 13 years ago

FWIW, I managed to fix the aforementioned (original) issue by using sed:

GIT_EDITOR="sed -i /.*${treeish:0:7}.*/d" git rebase -p -i "${treeish}~2"