0

Say I have pushed two commits and I and try to squash them.

Instead of doing git rebase -i HEAD~2 - I accidentally do git rebase -i HEAD~3

Here the 3rd last commit is from someone else but it would show up in my PR and say "Other author and R11G committed". How to avoid this?

How to salvage this misstep? Any commands that can reverse or fix this?

R11G
  • 1,941
  • 8
  • 26
  • 37
  • What does squashing commits have to do with a pull request? – mkrieger1 Jul 14 '22 at 22:23
  • `git rebase -i HEAD~3` does the same as `git rebase -i HEAD~2` if you just use `pick` for the earliest of the 3 commits. – mkrieger1 Jul 14 '22 at 22:25
  • [Reflogs](https://git-scm.com/docs/git-reflog) are handy when you think you have destroyed something in Git. – Benjamin W. Jul 14 '22 at 22:26
  • @mkrieger1 The commit from other person is now showing up in my Pull Request as "Other author and R11G committed". Those commit changes basically. And I want to get rid of them. – R11G Jul 14 '22 at 22:43
  • As you've been told, the rebase is completely undoable. So undo it (using the reflog) and do it correctly the next time. It's as simple as that. Don't overthink it; just undo the rebase and now you can redo it the way you wanted. – matt Jul 15 '22 at 01:47

1 Answers1

1

This does not seem to be an issue with git but with whatever source code management tool you are using, that created the PR. Here's a simple test:

My original branch (with --format=fuller):

commit 18cabfe932ad37e210bebf14c3c5084f65629962 (HEAD -> master)
Author:     Sam Varshavchik <mrsam@courier-mta.com>
AuthorDate: Thu Jul 14 20:09:48 2022 -0400
Commit:     Sam Varshavchik <mrsam@courier-mta.com>
CommitDate: Thu Jul 14 20:09:48 2022 -0400

    Commit 3

commit 4a12c468a08fcb17945ca08aa2032594f6988724
Author:     Sam Varshavchik <mrsam@courier-mta.com>
AuthorDate: Thu Jul 14 20:09:24 2022 -0400
Commit:     Sam Varshavchik <mrsam@courier-mta.com>
CommitDate: Thu Jul 14 20:09:24 2022 -0400

    Commit 2

commit ff0b2a68da3c22179279ef2b009616f659f4ffd1
Author:     Sam Varshavchik <mrsam@courier-mta.com>
AuthorDate: Thu Jul 14 20:09:02 2022 -0400
Commit:     Sam Varshavchik <mrsam@courier-mta.com>
CommitDate: Thu Jul 14 20:09:02 2022 -0400

    Commit 1

commit 5f9cf33419421f45313ccec60dad105337de4d6f
Author:     Sam Varshavchik <mrsam@courier-mta.com>
AuthorDate: Thu Jul 14 20:08:31 2022 -0400
Commit:     Sam Varshavchik <mrsam@courier-mta.com>
CommitDate: Thu Jul 14 20:08:31 2022 -0400

    Initial commit

After performing the rebase, and squashing only the last two commits:

$ history
...
 1003  git rebase -i HEAD~3
 1004  git log --format=fuller
 1005  history

The end result:

commit 544d3deeeea682b195f64421b8ef33b2e91043da (HEAD -> master)
Author:     Sam Varshavchik <mrsam@courier-mta.com>
AuthorDate: Thu Jul 14 20:09:24 2022 -0400
Commit:     Sam Varshavchik <mrsam@courier-mta.com>
CommitDate: Thu Jul 14 20:10:26 2022 -0400

    Commit 2
    Commit 3

commit ff0b2a68da3c22179279ef2b009616f659f4ffd1
Author:     Sam Varshavchik <mrsam@courier-mta.com>
AuthorDate: Thu Jul 14 20:09:02 2022 -0400
Commit:     Sam Varshavchik <mrsam@courier-mta.com>
CommitDate: Thu Jul 14 20:09:02 2022 -0400

    Commit 1

commit 5f9cf33419421f45313ccec60dad105337de4d6f
Author:     Sam Varshavchik <mrsam@courier-mta.com>
AuthorDate: Thu Jul 14 20:08:31 2022 -0400
Commit:     Sam Varshavchik <mrsam@courier-mta.com>
CommitDate: Thu Jul 14 20:08:31 2022 -0400

    Initial commit

As can be seen here "Commit 1", the third oldest commit, is completely unchanged and still carries the same commit hash.

git rebase is smart. If it sees that the initial set of commits that get pulled into the rebase are not getting modified, in any way, the rebase starts only with the first altered commit.

You'll need to look at your SCM in order to determine why it is including another commit in your PR.

If you can't figure out why your SCM is doing what it's doing, and what to do about it: note the hash of your surviving commit, cancel the branch, create a new branch, cherry-pick the orphaned commit and attempt to do another PR.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148