Dev ❤ Ops

How to squash all commits into one

Git makes Developer’s life easy, we can’t even imagine how difficult Dev and Ops life can be without git. Git is a configuration management tool, It provides a way to manage and tracking of code change. This topic is to explain the git squash feature.

What is git squash?

Git squash provides a way to merge multiple commits into one git commit.

It provides a way to:

  • Clean git history.
  • Easy tracking of large code changes.

Squash all commits into one

Following is the process to squash commits,

Step 1: Clone the Git repository and go inside the directory.
[DevOpsForU@localhost ~]$ git clone -b demo https://pradeeppandeyy:XXXX@github.com/pradeeppandeyy/squash.git
Cloning into 'squash'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
[DevOpsForU@localhost ~]$ cd squash/
Step 2: Add some commit if this is a test.

(this is an example to create some test commits).

[DevOpsForU@localhost squash]$ for x in {a..e}; do touch ${x} && git add . && git commit -m "[Demo][ADD] adding file ${x}" && git push; done
[DevOpsForU@localhost squash]$ ls
a  b  c  d  e LICENSE README.md
Step 3: Check all the commits through the git log.
[DevOpsForU@localhost squash]$ git log --oneline
5cd0c05 [Demo][ADD] adding file e
07fc8f3 [Demo][ADD] adding file d
36183ca [Demo][ADD] adding file c
fbc95bd [Demo][ADD] adding file b
8205e8c [Demo][ADD] adding file a
e5b1a82 Initial commit
Step 4:  Get the commit hash(#)  and reset the commit history to that particular hash(#).
[DevOpsForU@localhost squash]$ git reset --soft e5b1a82
Step 5: Validate git history again.
[DevOpsForU@localhost squash]$ git log --oneline
e5b1a82 Initial commit
Step 6: Add the commit message to update the new squashed commit.
[DevOpsForU@localhost squash]$ git commit -m "[Demo][Change] commits are squashed"
[demo 79d6831] [Demo][Change] commits are squashed
14 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
create mode 100644 b
create mode 100644 c
create mode 100644 d
create mode 100644 e
Step 7: Check the status and push code forcefully to update the repo.
[DevOpsForU@localhost squash]$ git push -f
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 366 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://pradeeppandeyy:ghp_XXXX@github.com/pradeeppandeyy/squash.git
+ 8258d94...79d6831 demo -> demo (forced update)

Conclusion: Squash is the way to merge all the commits into one, it makes commit history clean and clear.

This article is created based on experience but If you discover any corrections or enhancements, please write a comment in the comment section or email us at contribute@devopsforu.com. You can also reach out to us from Contact-Us Page.

Follow us on LinkedIn for updates!

Leave a comment

Your email address will not be published. Required fields are marked *