Suppose we have to deliver a feature using our feature branch and we have multiple commits on the same. To make our git history clear, it is advisable to squash commits. Git squashing can be done using many different approaches. We condense multiple commits into one to keep our history clean and more meaningful. The feature branch will have a parent branch and squashing of commits on the feature branch will be cleaner and easy to understand.
Steps To Squash Commits In Feature Branch
In this post, I will explain one approach to squash commits using the parent branch. We will already have a PR raised from our feature branch and we have multiple commits on the same. Using the below steps, we will update our PR with a single commit which will have all our required changes. The squashing of commits makes our history cleaner and easy to understand.
1. Checkout to the parent branch. Say the parent branch name is development.
git checkout development
2. Pull the latest code.
3. Now checkout to the specific feature branch.
git checkout [feature branch name]
4. Now reset the feature branch to the parent branch i.e. development in our case to the specific feature branch and resolve any conflicts at this stage.
git reset development --soft # Resolve conflicts here if necessary
5. Now add your files to commit and carefully check all your changes. No other files should be added which is not required.
git add .
6. Commit your changes.
git commit -m [commit message]
7. Push your changes.
git push origin HEAD -f
After doing all the above steps, our existing pull request will be updated with a single commit and all our changes.