a simple bash script to perform git squash without pain
-
Make sure you have Git installed on your machine.
-
Download or clone the script to your local machine.
-
Open a terminal window and navigate to the directory where the script is located.
-
Make the script executable by running the following command:
chmod +x git_squash.sh
-
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
. -
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
. -
Source your
.profile
file by running the following command:source ~/.profile
This will reload your
.profile
file and make thegit-squash
alias available in your terminal.
To use the script, follow these steps:
-
Open a terminal window and navigate to the directory where your Git repository is located.
-
Checkout the feature branch that you want to merge into the main branch.
-
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
-
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.