# Git Squash: Squishy Commits for a Cleaner Code Pasture!

Many commits can be made in a single branch. Git Squash is the technique we can use to combine all these commits as one which will lead to maintaining a clear commit history.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692811565212/322cddb5-2fcb-4c30-a44e-9c478d192680.png align="center")

### Follow these simple steps:

**Let's create a repo on GitHub**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692812591854/ed98912e-3d2a-48ce-84c6-ef2c3a821a07.png align="center")

---

**Clone the repo and add a change**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692812782450/79e362f4-54d6-4c37-b56d-7987463971a4.png align="center")

---

**Make the first commit**

Type these commands to make the first commit.

```plaintext
git add .
git commit -m 'first commit'
```

Type this command to see your git history.

```plaintext
git log
```

Commit history should look like this. There you can see the latest commit that we made.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692813007003/1152a39b-9446-41aa-b898-94593cd57754.png align="center")

---

**Make another commit**

Changes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692813293228/60f5c3c8-767d-474c-9e0e-5a7e6a2aff8d.png align="center")

Commit history.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692813328713/679d9852-6fd3-4f2e-8d07-b2dbe85b7198.png align="center")

---

**Repeat again**

Changes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692813389361/9fa0afa2-2535-4bb9-ab96-db09e98da4ad.png align="center")

Commit history.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692813435852/2dcbf545-7525-4671-9bff-da3615aa8fe7.png align="center")

---

**Now let's try to combine the first, second and third commits**

Run this command to start the squshing process.

```plaintext
git rebase -i HEAD~3
```

We are considering the last three commits. Therefore, we use `HEAD~3`.

After running above, you should see this.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692813621941/2d469c84-1611-42b1-a1ae-57e7fadb1c0c.png align="center")

Pick the commits you are going to squash. In our case, it is the second and the third. Mark them like the below. Remove the `pick` and add `s` in front of each commit you need to squash.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692813809531/3d54923a-950e-4898-9161-5048bff6eecb.png align="center")

Now save it and exit. (On Ubuntu terminal it is `ctrl+x` and type `y`).

Now you will see a screen like this. Change the commit message here.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692814004537/36f1b6c2-073f-4082-bb2d-565cec651b31.png align="center")

Let's only keep the message for the first commit and save. If it is successful you will see a message like below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692814209593/d58c2086-e83e-423e-8fc7-f35e6b7cb134.png align="center")

---

**Verify the commit history and changes**

You should only see the first commit with all the changes you made in different commits.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692814271732/e567e23b-9dbe-4359-9638-f12112ce6ae4.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692814249293/6cec5a56-b0fa-4ac5-a3b7-dc179293ff30.png align="center")
