Home > Software engineering >  Git squashing commits after a merge
Git squashing commits after a merge

Time:12-19

I have a git branch which recently I had to rebase off of our main branch. This all went well however I would like to submit a PR and I need to get my PR down to 1 commit. With the merge from the main branch I have 2. 1 for the changes I made and 1 for the merge.

When I run rebase and squash the commits that have been added after I originally created my branch I have something like 30 files changed in my PR.

When I run rebase and drop the commits that have been added after I originally created my branch it's like I never merged with the main branch and some tests start failing.

How do I get this down to a single commit without having all the changes since I created my branch be part of my PR?

CodePudding user response:

  1. Checkout the branch that you want to submit the PR for.

  2. Run git log to view the commit history of your branch. Identify the commit that represents the changes you made on your branch, and note its commit hash.

  3. Run git rebase -i <commit-hash>, where <commit-hash> is the commit hash of the commit that represents the changes you made on your branch. This will open a text editor with a list of commits.

  4. In the text editor, change the word "pick" to "squash" for all the commits except the first one, which represents the changes you made on your branch. Save and exit the text editor.

  5. Git will apply the changes in the subsequent commits and squash them into the first commit. It will then open another text editor for you to edit the commit message for the combined commit. Edit the commit message to reflect the combined changes, and save and exit the text editor.

  6. Run git push -f origin <branch-name> to force push the updated branch to the remote repository. This will replace the old commits with the new combined commit.

  •  Tags:  
  • git
  • Related