When Git Goes Wrong, continued.
I added too many files to a commit
Suppose you have made changes to a few files:
And you create a commit with one of the files, accidentally leaving the other file out:
Then, when doing a git status
, you notice that a file was not
committed:
You confirm that by using git log
to look at the most recent commit:
The wrong way to fix it
You could just make another commit with the missing files:
# Please don't do this
$ git add bar
$ git commit -m'Adding missing file from fix for issue #1'
And, if you have already pushed the previous commit to origin, this is what you’ll have to do. But when you have a choice, please don’t do this. You want the commit’s idea to be represented in a single commit, not in two. Also, it’s unlikely that the first of the two commits would have passing tests.
So let’s fix it
To the file you left out to the commit, you can first stage the file
using git add
:
and then add the staged files to the previous commit by using the
git commit
with the --amend
switch:
This will bring up an editor window which you can use to edit the commit message, if desired:
Fixed issue #1
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Aug 28 11:36:15 2015 -0700
#
# On branch master
# Changes to be committed:
# modified: bar
# modified: foo
#
After you save the file, git will ammend the commit, adding the file that you accidentally left out.
Git means never having to say you’re sorry.