Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix commit order in GitHub pull requests, broken by git rebase?

Tags:

git

github

rebase

When I write code I break it into small logical changes that are easy and quick to review.

To do so, I use git rebase -i (interactive) to squash, drop and change order of commits.

I've noticed this sometimes leads to a different order of commits on a GitHub pull request (though the order is retained on the remote branch).

For example,

  • commit 1
  • commit 2
  • commit 3

might show up in the PR as:

  • commit 3
  • commit 1
  • commit 2

I've searched the internet and only managed to find this GitHub help page: Why are my commits in the wrong order? Their answer:

If you rewrite your commit history via git rebase or a force push, you may notice that your commit sequence is out of order when opening a pull request.

GitHub emphasizes Pull Requests as a space for discussion. All aspects of it--comments, references, and commits--are represented in a chronological order. Rewriting your Git commit history while performing rebases alters the space-time continuum, which means that commits may not be represented the way you expect them to in the GitHub interface.

If you always want to see commits in order, we recommend not using git rebase. However, rest assured that nothing is broken when you see things outside of a chronological order!

Is there a way to work around this?

like image 831
EliadL Avatar asked Jun 22 '17 16:06

EliadL


People also ask

Is it possible to reorder commits in git?

With Interactive Rebase, Git offers a very powerful tool to edit / rewrite / reorder / etc. existing commits. Please note that Interactive Rebase is only available for the currently checked out branch.

What does dropping a commit in rebase do?

(drop) — If you remove a commit from the interactive rebase file, or if you comment it out, the commit will simply disappear as if it had never been checked in. Note that this can cause merge conflicts if any of the later commits in the branch depended on those changes.

How do I rebase a pull request?

To update by rebasing, click the drop down menu next to the Update Branch button, click Update with rebase, and then click Rebase branch. Previously, Update branch performed a traditional merge that always resulted in a merge commit in your pull request branch.


2 Answers

I've managed to work around this by:

  1. Locate the last commit that retained the order
  2. Run git rebase -i <hash of that commit>
  3. Replace all pick with reword
  4. Run git push -f

Before that, I tried changing only the first commit message, which also changes all the following hashes, but that didn't fix it.

I had to do it for every following commit too for it to work.

like image 198
EliadL Avatar answered Sep 17 '22 19:09

EliadL


To automate what Eliad suggested I use a script from Piotr:

git rebase "$(git merge-base HEAD master)" --ignore-date -x 'git commit --amend -C HEAD --date="$(date -R)" && sleep 1.05'

like image 45
nikicc Avatar answered Sep 19 '22 19:09

nikicc