fancy
fancy

Reputation: 51363

add minor changes to a commit?

I just made a commit and push it to master. There was a minor typo and I want to fix it as part of this commit, not another one. Is it possible to append this change to the current and pushed commit?

Upvotes: 6

Views: 7768

Answers (2)

AD7six
AD7six

Reputation: 66169

Rewriting public history is a bad idea

Be aware that rewriting history can cause quite a few problems. If there's a possibility of another developer/checkout having already pulled your typo-commit then DO NOT do what you're asking. The problem is that if I, say, pull from your master right now - I have your borked commit. If you rewrite history and I try to pull again - my checkout is nolonger on the timeline for your master branch. At the absolute minimum that's confusing. If I fix that the normal way (rebasing) and push to master - the borked commit will be put back in the master branch's history, and on your next pull - it's back in your history too.

Commit amend

Caveat out of the way...

To update the last commit you do this:

git checkout master
git pull

hack hack hack

git commit -va --amend
#................^

This will update the contents of your last commit.

To update your remote so that it matches your local master branch

git push
# READ THE WARNING

git push -f

This will force-update the remote's history to include your updated commit - and REMOVE the older version of the "same" commit. That's the thing, in this circumstance, you lose and git is warning you about.

Upvotes: 5

KingCrunch
KingCrunch

Reputation: 131831

You could

git commit --amend
git push -f

but you shouldn't. Once pushed to any remote server you should not change the history. You should either fix the typo with the next commit, or make it a commit on it's own, what I would prefer. There is nothing wrong with a commit, that contains a single change and on the other side a commit should usually cover only a single "topic". You could build a "many typos"-commit ;)

Upvotes: 5

Related Questions