0

How do I combine 2 commits into one when the previous one was already pushed?

Step 1

git add file.txt
git commit
git push

Step 2

git add file.txt
git commit 
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • 3
    Be careful with this if you have other developers who may have pulled the published commit. Best practice is not to alter commits that have already been pushed. Doing so will “rewrite history” and require extra work for others working on the project. – Justin Wager Oct 11 '18 at 23:50
  • Possible duplicate of [how to combine commits and push to the remote](https://stackoverflow.com/questions/50491446/how-to-combine-commits-and-push-to-the-remote) – phd Oct 12 '18 at 01:14
  • https://stackoverflow.com/search?q=%5Bgit%5D+combine+commits – phd Oct 12 '18 at 01:14

4 Answers4

1

Try:

git reset --soft HEAD~2
git commit -m "Your new message"
git push --force

What this does:

  1. Reset your current branch (moves HEAD) to antepenultimate commit (the one before the penultimate, represented by HEAD~2) but leaves the index and the working tree.
  2. This allows to re-stage these changes and commit them in a brand new commit.
  3. --force allows you to push this new commit and force an override.

NOTE

As others have commented on your original question, beware that --force will most likely annoy other people that have already pulled the previous version of this branch. You could "squash" these changes into a new branch and publish it separately:

git reset --soft HEAD~2
git checkout -b my-new-clean-branch
git commit -m "Your new message"
git push -u origin HEAD

This way you don't need to --force anything.

Fernando Espinosa
  • 4,625
  • 1
  • 30
  • 37
  • You should probably include an explanation about the effects this will have on other developers who may have pulled the branch. – Code-Apprentice Oct 11 '18 at 23:55
  • Squashing into a new branch just duplicates the changes in two different commits. The OP and other visitors should just be aware that a force push can cause problems for others who pulled the branch. Squashing should only be used when no one else is on the same branch – Code-Apprentice Oct 12 '18 at 00:19
  • @Code-Apprentice The idea here is that you could "abandon" the previous branch and start a new independent history. Also, you are truly committing the combined changes of 2 commits into a single one, which is what was asked how to achieve. – Fernando Espinosa Oct 12 '18 at 00:24
1

You can do it using rebasing. Squash the last two commits with git rebase -i HEAD~2. Then do a force push with git push --force**.

** Generally you should prefer --force-with-lease over --force. If someone else were to push to the branch you are pushing --force would overwrite their changes. --force-with-lease would only force push if no one else has pushed to that branch.

Rashmirathi
  • 1,744
  • 12
  • 10
1
git add file.txt
git commit --amend
git push --force

The usual warnings about --force apply.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
0

Follow these commands in order.

git add .

git commit --amend

git push origin <branch> -f

Aakash Pahuja
  • 469
  • 4
  • 10