Languages
[Edit]
EN

Git squash last 2 commits into 1 commit

1 points
Created by:
Mohammad-Oneal
327

Quick solution:

git rebase -i HEAD~2

 

Flow:

  • git rebase --interactive HEAD~2
  • change first word of second line from "pick" to "squash"
  • :wq (git bash write and quit)
  • 2 commit messages will show up
  • usually we remove second message and leave only first message or we change first message
  • push our local branch to remote with force - git push -f

 

Example

First command:

$ git rebase -i HEAD~2

pick 7357fca Commit 1
pick fc8bc9b Commit 2

Change pick to squash:

$ git rebase -i HEAD~2

pick 7357fca Commit 1
squash fc8bc9b Commit 2

 

Change time of last commit to current time

git commit --amend --date="now"

See more: Git squash - change time of last commit to current time

 

Git bash / vim - replace replace pick with squash

2,17s/pick/squash/g

Above command will replace pick with squash from 2nd to 17th line (commit).

If we have 5 commits to squash then:

2,5s/pick/squash/g

TODO: find pick only at the beginning of the line.

 

See also:

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Git - rebase & squash

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join