Add a file I’ve forgotten to add to my last commit

Share this video with your friends

Send Tweet

You committed your work to the repository and just realized that you’ve forgotten to include some of the files. Let’s see how we can fix that by leveraging git --amend to include the missing files as well. Note: This only holds if you did not yet push your changes to the remote repository

Kostiantyn Hryshyn
Kostiantyn Hryshyn
~ 5 years ago

Nice thanks!

Juri Strumpflohner
Juri Strumpflohner(instructor)
~ 5 years ago

@Kostiantyn you are welcome 🙂

~ 3 years ago

thanks!

how to add a file I’ve forgotten to add to my last commit when is already pushed?

Juri Strumpflohner
Juri Strumpflohner(instructor)
~ 3 years ago

thanks! how to add a file I’ve forgotten to add to my last commit when is already pushed?

You can totally do that, but beware that "amending" a commit changes it's SHA, basically it drops and re-recreates that specific commit. As a result, to push it to the remote, you'll have to do a git push --force (or short -f). Doing a "force push" should only be done on your own branch and never on a shared branch because it will disrupt the ability to properly merge in the updates for other people working on the same branch.

That said, if this is your own feature branch which you've pushed up and created a PR, then it's fine. If it is a branch where others also commit, then I'd highly recommend against doing it.