Squash and Merge commits within a branch
Tags: git, github, vscode
Published: 2022-03-30 22:32:49

## Introduction

So I found myself in a situation where I was unable to merge to main because I had multiple commits within a branch that were not verified (new laptop, forgot to set up [gpg commit signing](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification). I was faced with the problem - how do I gather all the unsigned commits into one verified commit?

This is one example use case, however you may want to squash and merge commits within a branch because there are many "changed spelling" or "fixed indentation" commits that are minor changes where retaining a history adds noise to the git commit history.

## Steps

### Step 1 - Use VS Code!

Why? well lets face it, vim is tough to use!

1. Stemming from [this stack overflow post](https://stackoverflow.com/questions/30024353/how-to-use-visual-studio-code-as-default-editor-for-git), ensure you can use the `code` command in the command line.
2. Once that's verified, simply run `git config --global core.editor "code --wait"` to set VS Code as the default editor for git commands.
 
### Step 2 - Git Rebase

This is where it gets both scary and where the stuff is done.

1. Navigate to the repository in question, and open a terminal instance within the root.
2. Run `git log` to see a list of commits (this will still open in vim, hit `:q` to exit)
2. Run `git rebase -i HEAD~3` in the command line (where the number 3 represents the number of previous commits to look at on your branch).
3. A new file tab will open in VS Code

### Step 3 - Choose your Options

Here you'll be choosing what you want to do with all your previous commits.
1. Go through the list, choosing the actions as needed (more info available in [github docs](https://docs.github.com/en/get-started/using-git/about-git-rebase) )
  - `pick` to include it in the rebase (can change the order depending on  
  - `drop` to exclude it from the rebase (leave it alone)
  - `squash` to squash/merge the commit
  - `edit` to change/modify the commit
  - `reword` to change the commit message and `pick` it
  - `fixup` to discard the commit message and `squash` it
2. Close the file or hit cmd+a (if using a mac)

As an example, here's what I was choosing to do:

![Example of Choosing Actions for git rebase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytytnr72bmvhypsj2r3a.png)

### Step 4 - Perform follow-up tasks

Depending on the actions chosen in the previous step there will be some supplementary file tabs that will open.

Follow the steps in each window, closing them as you make changes.

For Example, this is my prompt when I chose to `edit` a previous commit during rebase.

![Edit a commit for Rebase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/excgskp9g1p3vlkoek98.png)

### Step 5 - Pull/Push

This is the last step! Once all the actions have been completed, you'll be prompted to pull from origin, and then push any changes you've made. This can be done via command line, or in the Github desktop client.

![Pull/Push example with Github Client](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7i6fbchl1qaut4sny89u.png)

## Final words

Using rebase can help a resolve a great deal of issues, as well as clean up a commit history. It is "rewriting history" so to speak, so as Uncle Ben in Spiderman says: _"with great power comes great responsibility"_. 

I hope that this has been a helpful article! Thanks for reading!