Skip to content

Instantly share code, notes, and snippets.

@anthonyray
Last active August 18, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyray/bd6e5ff1c60828b6b75dbdbc7b1781e0 to your computer and use it in GitHub Desktop.
Save anthonyray/bd6e5ff1c60828b6b75dbdbc7b1781e0 to your computer and use it in GitHub Desktop.
Squashing commits in a branch before merging the branch into master

Let's say you've been working for several hours on a feature branch. In the process, you pushed a important number of commits. If you were to directly merge the feature branch into the master branch, you would mess up the history of commits in the master branch.

Here is how you can squash commits from a feature branch before merging the feature branch into the master branch.

We'll go through this process by following an example :

Let's say we have a feature branch. This branch has an important number of commits you'd like to squash before merging the branch into master.

Because our method can delete commits, we create an extra branch from the feature branch that we will call feature_squashed

git checkout -b feature_squashed

You then need to find the commit SHA from which you'd like your squash to start.

You can visualize your network with the following command :

git log --graph --oneline --all

Let's say you choose the commit with the following SHA : 0b44a68

We are going to squash from this commit. You can do a :

git rebase -i 0b44a68

Your editor will open up a file structured like so :

pick 0b44a68 hotfix 1
pick x536897 hotfix 2
pick c01a668 hotfix 3

Each line represents a commit (in chronological order, the latest commit will be at the bottom).

Modify this file by replacing pick by squash to squash commits together :

pick 0b44a68 hotfix 1
squash x536897 hotfix 2
squash c01a668 hotfix 3

Once you're done, you can push (force) the modification to your remote branch !

git push -f

You're all set ! ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment