How to Squash Commits in Git
Sometimes your commits at a branch reach a high number, "fixing the issue", "fixing security", and "covering with more test cases", and your changes become a little bit disorganized. In that case, squash commit can help you group changes and describe the commit.
I'm not a big fan of significant changes, I know that occurs more than expected.
Okay, my discovering and describing tasks failed.
Stopping talking about regrets and thoughts. Let's do a quick sample.
Firstly, time to create the master branch and make the first commit.
mkdir /tmp/git_squash_commit
cd /tmp/git_squash_commit
git branch -m master
echo "printf 'my message'" > some.bash
git init
git add some.bash
git commit -m 'adds some.bash'
Let's create another branch to make some changes, "I wrote some bugs, nothing new."
git checkout -b squash_test
echo "printf 'my message fixing'" > some.bash
git add some.bash
git commit -m 'adds some bash'
Okay, now time to make more changes, "Sorry, I forgot to create tests. 😅"
echo 'if bash some.bash | grep -q "missing"; then echo "ok"; else echo "failed"; fi' > test_some.bash
git add test_some.bash
git commit -m 'adds test_some bash'
Oh no! I broke the test, wrong grep. "If tests work, time to create pull request."
echo 'if bash some.bash | grep -q "my message"; then echo "ok"; else echo "failed"; fi' > test_some.bash
git add test_some.bash
git commit -m 'fixing wrong grep'
Ouch, so many commits! I need to squash commits and push them to the task branch.
git rebase -i master
vi (useful)
i – Insert at cursor (goes into insert mode)
ESC – Terminate insert mode
dd – Delete line
3dd – Delete 3 lines.
x – Delete character at the cursor
ESC and :x -> save
ESC and :q! > quit without save
As you can see, the first commit keep as "pick" and others as "squash."
Close editor and save commit ESC and :x.
Create your pull-request
That kind of story might happen in your developer day, so I hope I can help you to update your kernel bye!
Time for feedback!