Home » Softwares » Git » Git Adding a File to a Previous Commit

Git Adding a File to a Previous Commit

Imagine that you are doing a commit and you forgot to bring a file or multiples files. You can do a second commit but this would have the consequence to have two commits for something that in fact would have been in one. Git allows you to alter a past commit with the amend parameter.

For example, here is a small commit of 1 file.

git add main.cs
git commit -m "This is my first commit"

This is how to add a second file to this first commit.

git add util.cs
git commit --amend --no-edit

The –no-edit parameter specify that we do not want to modify the message. If you are doing a git log, you will see a single commit.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.