0

I need to make a change and submit it as a separate commit, right before the commit at the top of my commit stack (size 1).

e.g., right now I have A -- B Where B is the top my commit stack, and A is the latest commit I just pulled from origin. I found that I need to make a change and submit a new commit, but it makes more sense to make this change before the changes in commit B. Let's call the new commit C, so I want the chain to look like

A--C--B

How can I go about doing this? Should I first make A--B--C and then swap B and C using interactive commit?

user5965026
  • 465
  • 5
  • 16
  • Have you pushed `B` anywhere? This requires rewriting history, so if others have seen `B` it makes it questionable whether or not you should do this. – Stephen Newell Jan 24 '22 at 18:44
  • `B` was submitted as a pull request, but it won't be accepted until I introduce `C`, so I think rewriting history here shouldn't be a problem because others aren't working with `B`. – user5965026 Jan 24 '22 at 18:45
  • 1) Why does the order of the commits matter enough to bother changing it? 2) Are you talking about inserting a commit in a shared repo, or just the one you work from yourself? (Rebasing is a lot easier when it doesn't affect other people.) 3) Does it need to be a separate commit, or could you just amend commit B? – Caleb Jan 24 '22 at 18:45
  • @Caleb 1) It makes more sense for someone looking at the history to see the change in `C` first than `B` 2) Just my local repo for now, which will be pushed up later. 3) My colleague wants it to be a separate commit for easier history tracking. – user5965026 Jan 24 '22 at 18:48
  • 1
    Does this answer your question? [Reordering of commits](https://stackoverflow.com/questions/2740537/reordering-of-commits) – Medamine Jan 24 '22 at 19:06

1 Answers1

1

Just my local repo for now, which will be pushed up later.

Since these changes only affect your repo, you can reorder the commits any way you like. The easiest way is to just make the change you need, commit it, and then use interactive rebase (git rebase -i ...) to reorder the commits. You can then force-push (git push -f ...) to change your remote branch, and your pull request should be updated automatically.

Caleb
  • 124,013
  • 19
  • 183
  • 272