I’m working on a project where I have a pull-request of another contributor that contains quite a number of commits (>15), some with really minor changes. In essence, the pull-request boils down to changes on only three files, and the cumulative changes could easily be contained in one commit per file. I like the changes, but I don’t want to clutter our repo with an unnecessary high amount of commits and I am tempted to squash the whole pull request, but that again I feel would be too coarse of a change history.
Is it possible to completely rewrite the pull-request to three commits, one for each file? Or simply accept only changes to certain files done by that pull-request, squashing the pull request first to commits for file 1, the again for commits for file 2 and a third time for commits for file 3?
Disclaimer: I am not that experienced with pull requests yet, this question might seem naive to some. I figure the “granularity” of commits is a question of style, but seeing that it can play quite a big role in terms of “rewinding”, cherry-picking and/or simply reconstructing what has changed, I find it is worth spending the time to ensure a “clean and readable” commit history.
1
Is it possible to completely rewrite the pull-request to three commits, one for each file?
Sure, although it’s worth noting that this question really isn’t about pull requests; it’s just a question about rewriting your commit history in git.
Reset you feature branch to the base branch (I’m assuming main
for the purpose of this example):
git reset main
Now commit the changes in file1
:
git add file1
git commit -m 'These are the changes in file1'
And then repeat that for file2
and file3
:
git add file2
git commit -m 'These are the changes in file2'
git add file3
git commit -m 'These are the changes in file3'
Now force-push your changes to your remote to update the pull request:
git push -f
As always, operate on a new branch or a copy of your repository if you are unsure that this will do what you want.
4