Amending Git Commits
Harry Bellamy
- One minute read - 177 wordsWhen using Git sometimes it’s more appropriate to amend an existing commit rather than adding an additional commit. This can happen, for example, if you’ve forgotten to add a file to a commit or you are making lots of small commits (this can happen when trying to setup build pipelines and the only way to test changes is to push changes and wait for a new build to run).
The following commands establish a basic workflow to edit a commit.
Ensure that you have the latest version of your branch. Without doing this, we risk losing remote commits.
git pull --rebase
Add the files you want to add to the head commit.
git add <whatever you need to add to the head commit>
Amend the head commit. Note that the --no-edit
flag means that the commit message will not change. If you wish to edit the commit message, omit this flag.
git commit --amend --no-edit
Finally, push the commit. Force push will need to be used here as the commit history is being rewritten.
git push --force