Skip to content

Instantly share code, notes, and snippets.

@IcodeNet
Last active December 18, 2020 15:45
Show Gist options
  • Save IcodeNet/d28b563007e09c2d6c301741d0fd46d1 to your computer and use it in GitHub Desktop.
Save IcodeNet/d28b563007e09c2d6c301741d0fd46d1 to your computer and use it in GitHub Desktop.
squash rebase workflow #git

What is the squash rebase workflow? It’s simple – before you merge a feature branch back into your main branch (often master or develop), your feature branch should be squashed down to a single buildable commit, and then rebased from the up-to-date main branch. Here’s a breakdown.

Pull master branch

git pull origin master

Create bug/feature branch

git checkout -b branchName

Make changes as needed with as many commits that you need to. Make sure the final commit is buildable and all tests pass.

Get the number of commits from the start of your branch. There are a couple of ways to get this. You can simply git log and count your commits, or

git log --graph --decorate --pretty=oneline --abbrev-commit

which will show a graph of your commit log history and may be easier to visualize your commits. Sometimes you will have large enough number of commits that counting can become troublesome. In that case grab the SHA from the last commit that your branch branches from.

Squash to 1 commit.

git rebase -i HEAD~[NUMBER OF COMMITS]

OR

git rebase -i [SHA]

If you have previously pushed your code to a remote branch, you will need to force push.

git push origin branchName --force

Checkout master branch

git checkout master

Pull master branch

git pull origin master

Checkout bug/feature branch

git checkout branchName

Rebase from master

git rebase master

Handle any conflicts and make sure your code builds and all tests pass. Force push branch to remote.

git push origin branchName --force

Checkout, merge, and push into master

`git checkout master

git merge branchName

git push origin master`

Rename the local branch to the new name

git branch -m <old_name> <new_name>

Delete the old branch on remote - where is, for example, origin

git push <remote> --delete <old_name>

Or shorter way to delete remote branch [:]

git push <remote> :<old_name>

Prevent git from using the old name when pushing in the next step.

Otherwise, git will use the old upstream name instead of <new_name>.

git branch --unset-upstream <old_name>

Push the new branch to remote

git push <remote> <new_name>

Reset the upstream branch for the new_name local branch

git push <remote> -u <new_name>

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