How to merge/squash commits in Git

It always best practice to combine commit for one change, and the reason for that is so that you can easily revert changes in case you need to and also the commits are more meaning full.

  1. The image the case that you are have committed your changes and you notice that something needs to be fix after pushing to up stream.

2. At this point you are have one commit and you can change the new files and commit new changes

git commit -am 'fix'

3. Once you have the add you latest commit now lets merge it with the old commit and for that you only need to use git rebase as shown below.

Note: the command below assume you only need squash one commit that’s why we are using HEAD~2 which will bring the last 2 commits for review

git rebase -i HEAD~2

4. Once you ran the command you will see instruction i shown below

pick 10ae1xt Old commit
pick nbb4268 fix
# Rebase 23eabdb..aeb5269 onto 23eabdb
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~

6. Now all you need to do is change the pick on the “fix” to “s” for squash and type “:wq” ( This will write and quit )

pick 10ae1xt Old commit
s nbb4268 fix
# Rebase 23eabdb..aeb5269 onto 23eabdb
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

7. Now you get prompt to choose you final commit message

# This is a combination of 2 commits.
# The first commit's message is:
Old commitfix# This is the 2nd commit message:# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
#
# Changes to be committed:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# modified: Dockerfile
# modified: README.md

8. ignore the comments “#” you only need to change the now comment lines as shown below

# This is a combination of 2 commits.
# The first commit's message is:
Adding a new API Endpoint to Dockerfile# This is the 2nd commit message:# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
#
# Changes to be committed:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# modified: Dockerfile
# modified: README.md

9. Now you just need to force push override the upstream

Note: notice the “+” sign which indicated force push

git push origin +<branch-name>

--

--

--

Senior DevOps Consultant, a tech enthusiast and cloud automation expert that helps companies improve efficiency by incorporating automation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Stable Abstractions Principle (SAP)

What to Look at When Choosing a Web Scraping API — Examples Included

Polymorphism, in my Associations?

Lawsuits and Trials

lawsuits and trials

🎉🎉NFT MARKETPLACE WILL BE OPENED AFTER THE PUBLIC SALE☀️

Design Patterns in Python: Builder Pattern

Python Serialization Benchmarks

Most young or inexperienced developers appear to become trapped or irritated when learning software…

img

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam A

Sam A

Senior DevOps Consultant, a tech enthusiast and cloud automation expert that helps companies improve efficiency by incorporating automation

More from Medium

HTTP Pipelining & Multiplexing

Installing SQL Server and Microsoft SQL Server Management Studio on Windows Server 2019 VM.

SAP — Logging onto SAP System

How to load a CSV file into a Rails API