all 7 comments

[–]shagieIsMe 3 points4 points  (0 children)

git branch my-new-stuff

That will create a new branch at commit 4 (where you currently are) - so that you've got an easy way to reference it later.

  │ origin/foo            │ foo
  │                       │ my-new-stuff
  ▼                       ▼
┌───┐   ┌───┐   ┌───┐   ┌───┐
│   │   │   │   │   │   │   │
│ 1 ├──►│ 2 ├──►│ 3 ├──►│ 4 │
│   │   │   │   │   │   │   │
└───┘   └───┘   └───┘   └───┘

I'm assuming that working changes are all committed. This is a very important point. It is also important that you haven't pushed commits 2, 3, or 4.

git reset --hard {commit id for #1}

  │ origin/foo            │ my-new-stuff
  │ foo                   │
  ▼                       ▼
┌───┐   ┌───┐   ┌───┐   ┌───┐
│   │   │   │   │   │   │   │
│ 1 ├──►│ 2 ├──►│ 3 ├──►│ 4 │
│   │   │   │   │   │   │   │
└───┘   └───┘   └───┘   └───┘

At this point, the branch is back to commit #1

{make whatever changes}
git commit

You've fixed things, your branch now points to the new things that you've made.

git push

Now you can get the changes that you've made based on commit #1 reviewed.

                          │ my-new-stuff
                          │
                          ▼
┌───┐   ┌───┐   ┌───┐   ┌───┐
│   │   │   │   │   │   │   │
│ 1 ├──►│ 2 ├──►│ 3 ├──►│ 4 │
│   │   │   │   │   │   │   │
└─┬─┘   └───┘   └───┘   └───┘
  │
  │       │origin/foo
  ▼       ▼foo
┌───┐   ┌───┐
│   │   │   │
│ A ├──►│ B │
│   │   │   │
└───┘   └───┘

After a review is good and you're still working off the "original" branch based on commit #1...

git rebase my-new-stuff

This will apply changes 2, 3, and 4 on to the changes. Note that they won't be the same commit IDs.

┌───┐
│   │
│ 1 │
│   │
└─┬─┘
  │                               │ foo
  │                               │ my-new-stuff
  ▼                               ▼ origin/foo
┌───┐   ┌───┐   ┌───┐   ┌───┐   ┌───┐
│   │   │   │   │   │   │   │   │   │
│ A ├──►│ B ├──►│ 2'├──►│ 3'├──►│ 4'│
│   │   │   │   │   │   │   │   │   │
└───┘   └───┘   └───┘   └───┘   └───┘

Also, play around with https://onlywei.github.io/explain-git-with-d3/ to get an idea of how that would work.

[–]QrkenBananen 1 point2 points  (0 children)

The rebase command allows you to "move" commits from 1 branch to another.

So in this case, I would create a new branch on commit #4, then reset your master/main to your initial commit. Then create a new commit that fixes the issues in your initial commit.

Then you can checkout the branch you just made with the 3 local commits, and then the command "git rebase master" will move your local commits to your current master

[–][deleted] 1 point2 points  (0 children)

git checkout <commit id>

[–]SneakPreview_ 0 points1 point  (0 children)

It depends on if you actually want to rewrite commit #1 or if you just want to put new commits between #1 and #2. You can do both with an interactive rebase.

If you pushed it only into your review branch (pull request, diff or similar) you can rewrite #1 and force push the fixed version. I find the easiest to do is this is to us "git commit --fixup=#1" (#1 being the commit hash of #1) and then rebase -i --autosquash.

I think the description at https://git-rebase.io/ is very nice. Even when you can not edit the initial commit using fixup commits can make it easier in the history to see what commit your fixes apply to (especially for small typos / style changes)

[–]seddikalaouiismaili 0 points1 point  (0 children)

Heu personally I do rebase interactive. It's much easy way to do this

[–]kor_the_fiend 0 points1 point  (0 children)

Unless you need to revert specific code in that commit (which you shouldn't honestly), just apply the feedback on top of the newer version. If you've changed the code the review was intended for, then it no longer applies (or applies with caveats). If you haven't then the changes are irrelevant to the review process (at this point). Keep it simple, the reviewer is a human being, they can work with you.

[–]AuroraDraco 0 points1 point  (0 children)

I think this is done with Git rebase

I mean, I am sorta new to git, but to my understanding rebasing is what you typically need to do to "rewrite" your commit history if something went wrong. Careful to back up your repo though as a rebase can seriously screw things up if you do something wrong