I added too many files to my commit
Suppose you have committed some work:
But later you look at the commit and discover you have committed sekrit, a file that should not have been committed.
As long as this commit has not been pushed to origin, you can remove the file from that commit. I’ll show two different ways to remote the file. Which way you pick depends upon whether you want to delete the file from disk ask well as from the commit, or keep it on disk and just remove it from the commit.
To delete the file from disk and from the commit
If you want the file deleted from disk as well as from the commit, just delete the file and then stage it:
Or you could use this git shortcut for “delete a file and stage the deletion”:
Either way, git status
will show that the file is staged for
deletion:
Now amend the previous comment:
The commit will no longer have that file:
And it is gone from disk:
To delete the file from the commit but not from disk
To remove that file from the commit but keep it on disk, we’ll tell git to undo the previous commit, but leave all of that commit’s changes on disk. But first, remember the sha1 of the commit:
Having that sha1 will keep us from having to type the commit message again. Now, undo the previous commit:
Now stage the commit again, but this time only add the files that should be in the commit:
And finally, commit. The -C
switch tells git to use the same commit
message, timestamp, and so on. It’s not necessary to use the -C
switch – you’d just have to type the commit message again.
The commit no longer has file sekrit in it:
But the file is on disk:
How to keep git from committing that file ever again
If the file should never be committed to git, add it to the
.gitignore
file:
Git will no longer consider the file untracked: