Remix.run Logo
1718627440 a day ago

> `git reset` by itself doesn't split a commit AFAIK.

Git reset splits a single commit, into two "things", another commit with the first part and a second part that is put into a version prepared for a commit (--soft), prepared for further editing (--mixed) or thrown away (--hard). To me that counts as commit splitting, but it may not match with JJ terms. Also splitting into two commits with the same commit message doesn't sound quite useful to me, so the default of Git two require a second commit message is sensible to me.

> Those were rhetorical questions.

Ok, but then what was your point?

martinvonz 21 hours ago | parent [-]

> To me that counts as commit splitting

Correct me I'm I'm wrong but I think were talking about using `git reset HEAD^` for splitting a commit. That will move the current branch backwards one step. With `--mixed`, it will also move the index back one step, so the index is empty (relative to HEAD) and the working copy has the combination of changes that were in the previous HEAD commit and the working copy (relative to the previous HEAD). I think that's more like squashing than splitting because we have fewer "things" after: we have one commit fewer (the previous HEAD commit may of course still be reachable from another branch, in which case we still have the same number of "things" afterwards). It's similar with `--soft` and `--hard`, except that the old changes end up in a different "thing".

At a less technical level, the point of splitting a commit is to end up with some of the changes from one commit in a new commit and the rest of the changes in another commit. That's what meant when I said "`git reset` by itself doesn't split a commit", because you need to do something like this:

  git reset HEAD^
  git add -p
  git commit -C HEAD@{1}
> Ok, but then what was your point?

Just that additional steps are needed.

For example, if you wanted to split the HEAD commit but you had already started working on a new commit so have some changes in the working copy, then you might instead have to do something like this:

  git commit -m tmp
  git rebase -i HEAD^^ # Use the "edit" action for the first commit
  git add -p
  git commit -m first
  git rebase --continue
  git reset HEAD^
The other case I mentioned was if you want to split a commit on another branch. Then you have to insert some additional commands in that sequence. As I said, I know how to do this with Git. I just find it easier to do with jj, where it's `jj split -r xyz` to split commit xyz, whether it's the current working copy, an ancestor commit, or on an unrelated branch.

(Take my Git commands with a grain of salt because I haven't used Git in 10 years.)

1718627440 14 hours ago | parent [-]

> Correct me I'm I'm wrong but I think were talking about using `git reset HEAD^` for splitting a commit.

I wasn't. I wanted to do the same as in the FAQ entry we are talking about, so I wanted to reset it to an older commit representing the same change (i.e. before an amend that we are now reverting). This is likely in a rebase, but we can always rebase later and only do the splitting now.

> With `--mixed`, it will also move the index back one step, so the index is empty (relative to HEAD)

Yes and this is the default (without any flag).

> That's what meant when I said "`git reset` by itself doesn't split a commit", because you need to do something like this:

That's what the `--soft` is for, then `git reset` does not touch the index.

> Just that additional steps are needed.

The only "additional" step required is specifying a commit message, which, as I said earlier, to me is a sensible default.

What I suggested applied on this case would be:

    git commit -m tmp
    git checkout @~
    git reset --soft previous-version # which you get from the reflog
    git commit -C @
    git rebase @~ branch-you-were-on --onto=@  # not of much use, when you only have a single commit you are throwing away in the next step, but when you are editing something earlier, this is likely what you want.
    git reset @~
If you want to do it with rebase:

    git commit -m tmp
    git rebase @~2 # break after first commit
    git reset --soft previous-version
    git commit -C @
    git rebase --continue
    git reset @~
More idiomatic, due to using the global list of todo commits:

    git rebase -i @~ --autostash # break after first commit
    git reset --soft previous-version
    git commit -C @
    git rebase --continue
You can drop the rebase, when it is really the commit in HEAD you want to split.

Actually what you can also do, but this doesn't use reset, is this:

    git rebase -i @~ --autostash
    # add as the first line:
    pick previous-version
    git rebase --continue
This will even do what you wanted and just reuse the same commit message without asking.

Honestly, what I do most of the time to split commits (when there isn't an older version I want to split it to) is to just amend and then unselect the changes I don't want with the cursor.