How to Merge a Git Feature Branch with a Squash Commit
Tags: shell, git, programming, tutorial
Published: 2023-02-16 19:20:54


When working with Git, it's common to create a feature branch to work on a specific feature or fix a bug. Once the feature or bug fix is complete, you'll want to merge the changes back into the main branch. One way to do this is with a squash commit, which combines all of the changes from the feature branch into a single commit on the main branch.

In this post, we'll show you how to use a simple Git script to merge a feature branch with a squash commit. The script creates a backup of the feature branch, creates a temporary branch from the main branch, squashes the feature branch into the temporary branch, and then commits the changes with a single message. It then points the feature branch to the temporary branch, force pushes the updated feature branch to the remote repository, and deletes the temporary branch.

## **Cloning the script**

To use the script, you'll need to clone the GitHub repository to your local machine. You can do this by running the following command in your terminal:


```
git clone https://github.com/esaraviam/git_squash.git
```

Once you've cloned the repository, you can navigate to the directory where the script is located and follow the instructions in the post to create an alias and use the script.



## **Installing the Script**


1. Make sure you have Git installed on your machine.
2. Download or clone the script to your local machine.
3. Open a terminal window and navigate to the directory where the script is located.
4. Make the script executable by running the following command:

    ```
    chmod +x git_squash.sh
    ```


## **Creating an alias**

1. Open your **`.profile`** file in a text editor by running the following command:

    ```
    vi ~/.profile
    ```

   Note: On some systems (such as macOS), the file may be named **`.bash_profile`** instead of **`.profile`**.

2. Add the following line to the bottom of the file:

    ```
alias git-squash="/path/to/script/directory/git_squash.sh"
    ```

   Replace **`/path/to/script/directory`** with the full path to the directory where the script is located. Save and close the file.

   This will create an alias for the script called **`git-squash`**.

3. Source your **`.profile`** file by running the following command:

   

```
 source ~/.profile
```
    

   This will reload your **`.profile`** file and make the **`git-squash`** alias available in your terminal.


## **Usage**

To use the script, follow these steps:

1. Open a terminal window and navigate to the directory where your Git repository is located.
2. Checkout the feature branch that you want to merge into the main branch.
3. Run the script with the following command:

   

```
 git-squash <feature-branch> <main-branch>
```


   Replace  **`<feature-branch>`** with the name of the branch that you want to merge and **`<main-branch>`** with the name of the main branch.

   For example, if you want to merge a feature branch named "feature-123" into a main branch named "main", run the following commands:

   
    

```
git-squash feature-123 main
```
   

4. The script will create a backup of the feature branch, create a temporary branch from the main branch, squash the feature branch into the temporary branch, commit the changes, and then point the feature branch to the temporary branch. It will then **force push** the updated feature branch to the remote repository and delete the temporary branch.

That's it! The script should merge the feature branch into the main branch with a squash commit.