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
Instructor: [00:00] In my repository here, I have added a new Angular component, which is that shared folder here, which I've added to that shared module. I am just adding all of this, and I am committing it. If I take a look at the log, we can see that we have added the commit here on top of master.
[00:19] Now, I forgot to add that app folder component, which I just created also in my app component. Let's jump over to the editor and correct that. In the editor here, I'm opening up that app component. Rather than having here the folder, I'm now using the new component I just created.
[00:33] Back in our terminal, we can see here the modified file. What I would like to have is, of course, add this to that last commit, to add new app folder. It makes sense to have that included in there. That is already committed, however.
[00:48] What I can do is I can use the git commit amend command. First of all, I am adding my app folder to the staging area. Now, it's ready to be committed. Now, I do a git commit, and I add that amend. If I hit enter, will open up the editor again.
[01:08] We now see that we got that modified file included in our commit changes as well. We can leave the message as it is. I'm just saving. Now, if I do a git log, we can see that the commit message is still here. It didn't change, but our app component modification has been integrated in that last commit.
@Kostiantyn you are welcome 🙂
how to add a file I’ve forgotten to add to my last commit when is already pushed?
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.