git-town / git-town

Git branches made easy
https://www.git-town.com
MIT License
2.62k stars 107 forks source link

Create a new feature branch as parent of the current branch and move over a few selected commits #3667

Open ruudk opened 4 months ago

ruudk commented 4 months ago

Your message to us

Often I'm working on a feature when I think: these changes belong not to the feature, but can be moved to a prepended branch.

My workflow in those situations is that I run git town prepend, create the branch, then manually select the commit (or commits) and drag them into that branch. Then create the PR. Then go back to the previous child branch and sync it.

But I feel this is something more people might have, so it would be really great if I could do this:

$ git town prepend my-branch --select-commits

There are 3 commits between the current branch X and main.

Select the ones you want to move over.

aaa
bbb
ccc

Done.
ruudk commented 4 months ago

I guess this can be solved in user land via scripting.

If anyone's interested:

#!/bin/bash

BRANCHPOINT=$(git town config get-parent)
BRANCH=$(git branch --show-current)

COMMITS=$(git log --no-merges --oneline --no-decorate "$BRANCHPOINT".."$BRANCH" | tac)

SELECTED=$(
    echo "$COMMITS" | fzf \
    --multi \
    --ansi \
    --layout=reverse \
    --header "Choose one or more commits using Tab. Press Enter to confirm. Press Esc to cancel." \
    --preview 'git show --color=always $(echo {} | awk "{print \$1}")' \
    --preview-window=up:60% \
    | awk '{print $1}'
)

if [ -z "$SELECTED" ]; then
  echo "No commits selected."
  exit 0
fi

echo "Selected commit(s):"
for COMMIT in $SELECTED; do
  git log -1 --format="%C(auto)%h %s" $COMMIT
done
echo ""

git hack

for COMMIT in $SELECTED; do
  git cherry-pick $COMMIT
done
kevgo commented 4 months ago

Interesting idea, and it fits into Git Town's mission to manage Git branches. This could also exist in the form of git town refactor split as part of https://github.com/git-town/git-town/issues/3338.

One challenge I have with features like this is that I would use it infrequent enough that I forget that this feature even exists :laughing:

cjol commented 3 months ago

I would use this all the time. If I'm using prepend at all, it's because I've already got some commits that I want to include in the new branch. I was actually a bit surprised that prepend defaults to the parent commit rather than the current commit (my expectation was that I would prepend and then drop the commits that are no longer in scope of the new branch).

ruudk commented 3 months ago

Yeah I also do this a few times a week. I work on a feature and see some low hanging fruit that I fix in separate commits. Those I want to separately propose.