brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: add a forgotten file to your last commit with --amend --no-edit

Commit
~/tips/tip-git-commit-amend-no-edit

git add forgotten-file.txt

Adds your staged changes to the previous commit without touching its message.

You commit, you look at the result, and you notice a file is missing. Please do not make a second commit called "oops".

git add forgotten-file.txt
git commit --amend --no-edit

--amend replaces the tip of the current branch with a new commit. The new commit starts from your old message and your old author data, plus whatever you have staged now. --no-edit tells git to use that message as is, without opening your editor. The docs use this exact combination as their example for amending without changing the message.

It works for small fixes too. Staged a small fix to a typo in a file you already committed? Same two lines. And if you do want to change the message, leave out --no-edit and the editor opens with the old one filled in.

The caveat is the usual one. An amended commit is a different commit with a different hash. If you already pushed the old one, you now have to force the push, and anyone who pulled it will have to deal with that. On a branch only you use, that is fine. On a shared branch, make the extra commit and move on.

It only touches the last commit. For anything older, look at fixup commits and autosquash.

Further reading: git commit documentation, On this site: sort your branches by last commit with branch.sort, On this site: fetch.prune cleans up branches that no longer exist remotely

More Commit tips if ! head -n1 "$1" | grep -qE '^[A-Z]'; then git commit -C ORIG_HEAD git commit --fixup=reword:HEAD~1 git config --global commit.cleanup scissors git commit -m "Fix login redirect" -m "The old code sent signed-out users to a 404." All Commit tips (7) All topics

Link