Reputation: 14419
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
Upvotes: 0
Views: 267
Reputation: 480
Follow these commands in order.
git add .
git commit --amend
git push origin <branch> -f
Upvotes: 0
Reputation: 4855
Try:
git reset --soft HEAD~2
git commit -m "Your new message"
git push --force
What this does:
HEAD
) to antepenultimate commit (the one before the penultimate, represented by HEAD~2
) but leaves the index and the working tree.--force
allows you to push this new commit and force an override.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.
Upvotes: 1
Reputation:
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.
Upvotes: 2
Reputation: 23137
git add file.txt
git commit --amend
git push --force
The usual warnings about --force
apply.
Upvotes: 1