Vannkorn

Full Stack Web Developer

Close

Git: forgot to add files to the previous commit

I happen to forget to add untracked files to the stage quite a lot, especially on a hot fix day.

Ads: Register now via this link to receive $300 credit from Vultr

I usually meet this situation on some hot fixes days so I decided to take a note here in case I need to look for the solution again.

It happens to me a lot with the newly created files, untracked files, since they are not grouped with the changed files.

To add some files to the previous commit, do the following steps:

git add file1
git add file2
git commit --amend --no-edit

The --no-edit flag allows to make an amendment to the commit without changing the commit message.

After that, if you already pushed the previous commit to the remote repository, your next push will get the error:

! [rejected] ... non fast-forward

To solve that, you will need to force the push:

git push origin <branch> --force

That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *