0

What would be the easiest way to combine a bunch of commits for an issue into a single one if there is also commits done for another issue in between the first issue?

I was trying to use rebase:

git checkout ISSUE-006
git rebase -i HEAD~13

Which gives me:

pick 5796b58 ISSUE-006: Fix case 1
pick 4d836b2 ISSUE-006: Fix case 2
pick cae225b ISSUE-006: Bump version
pick 0006f82 ISSUE-006: Fix case 3
pick 98263c2 ISSUE-006: Bump version
pick 8bcc4d4 ISSUE-018 Blahblahblah
pick 6b18711 ISSUE-018 minor changes
pick 0924c16 ISSUE-018 Blahblahblah
pick 0e720f9 ISSUE-006: Bump version
pick bc66908 ISSUE-006: Remove comments
pick 49930b9 ISSUE-006: Proper encoding
pick 8f1196e ISSUE-006: Add tests
pick 2bcbfef ISSUE-006: Bump version

I would like every commit belonging to ISSUE-006 to be squashed into a single commit. If I understand it correctly I can simply use the default editor and change pick to squash?

I also tried to use the merge with --squash without any success:

git checkout ISSUE-006
git branch -m ISSUE-TMP
git checkout master
git branch -b ISSUE-006
git merge --squash ISSUE-TMP
git branch -d ISSUE-TMP

I am not sure it is relevant, but the reason why I would like to do this is because I would like my pull request to be clean and only have a single commit.

Cyclonecode
  • 26,179
  • 11
  • 66
  • 86

1 Answers1

1

If I understand it correctly I can simply use the default editor and change pick to squash?

Not quite, since there are commits for ISSUE-018 in between the commits for ISSUE-006. Commits get squashed to the previous commit, so you will also have to reorder the commits in your editor, either to move all of the commits for ISSUE-018 to the end or possibly to the beginning.

Furthermore, the first commit for ISSUE-006 should be picked, not squashed.

For example:

pick 5796b58 ISSUE-006: Fix case 1
squash 4d836b2 ISSUE-006: Fix case 2
squash cae225b ISSUE-006: Bump version
squash 0006f82 ISSUE-006: Fix case 3
squash 98263c2 ISSUE-006: Bump version
squash 0e720f9 ISSUE-006: Bump version
squash bc66908 ISSUE-006: Remove comments
squash 49930b9 ISSUE-006: Proper encoding
squash 8f1196e ISSUE-006: Add tests
squash 2bcbfef ISSUE-006: Bump version
pick 8bcc4d4 ISSUE-018 Blahblahblah
pick 6b18711 ISSUE-018 minor changes
pick 0924c16 ISSUE-018 Blahblahblah

Note that simply reordering commits like this can generate conflicts, some of which can be painful to resolve. In the future, if you create a new branch for each issue you may be able to save yourself a headache.

Of course, merging or rebasing those branches can also generate conflicts. But you shouldn't get any if you're just squashing the commits of a branch without any reordering. git merge also includes a --squash flag to simplify this workflow during a merge.

Another benefit of keeping your commits in separate branches is that it simplifies merging / pull request submission. Pull requests and merges both operate between branches: you can't create a PR with your current branch that doesn't include changes for both issues.

I would like to do this is because I would like my pull request to be clean and only have a single commit.

Cleanliness is subjective, and in many cases multiple granular commits can be clearer and easier to review than one big commit. A better guideline is to follow the guidelines of the project you're contributing to, so your contributions are consistent with the rest of the project.


Edit: In a comment you mentioned that commits 8bcc4d4, 6b18711, and 0924c16 (the ones for ISSUE-018) already exist in the target branch. In that case you probably want to remove them from this branch, e.g. by doing something like this in your interactive rebase:

pick 5796b58 ISSUE-006: Fix case 1
squash 4d836b2 ISSUE-006: Fix case 2
squash cae225b ISSUE-006: Bump version
squash 0006f82 ISSUE-006: Fix case 3
squash 98263c2 ISSUE-006: Bump version
squash 0e720f9 ISSUE-006: Bump version
squash bc66908 ISSUE-006: Remove comments
squash 49930b9 ISSUE-006: Proper encoding
squash 8f1196e ISSUE-006: Add tests
squash 2bcbfef ISSUE-006: Bump version
Chris
  • 93,263
  • 50
  • 204
  • 189
  • I do have a separate branch for each issue, but since the some issues has been merged to master after I initially created my own branch I get this problem since I always merge my own branch with master from upstream. – Cyclonecode Oct 23 '18 at 11:52
  • @Cyclonecode, do you mean that commits `8bcc4d4`, `6b18711`, and `0924c16` here (the ones for `ISSUE-018`) are already in the branch you're targeting in your PR? – Chris Oct 23 '18 at 12:45
  • Yes exactly they are – Cyclonecode Oct 23 '18 at 12:57
  • In that case you probably want to remove those lines entirely in your interactive rebase. I'll update my answer. – Chris Oct 23 '18 at 13:01