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?
The general rule (for both scenarios) would be: Commit as often as possible. If you think "it's not ready yet" (because it'll break the build or simply isn't done yet) then create a branch and commit to that branch but make sure you do commit.
Stage your files to add to the commit with git add . or whatever the filenames are. Then, do git commit --amend or git commit --amend --no-edit (if you don't want to edit the commit message). This will amend the commit you chose to edit. Finally, run git rebase --continue .
Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.
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 ;)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With