Occasionally, when using the version control system Git, you may find yourself committing a file you didn't intend to. For example, you might want to commit two files separately, with different log messages (making it easier to roll back unconnected changes separately), but accidentally use the usually helpful shortcut git commit -a and commit both at once. Happily, there is a simple solution to this:
git reset --soft HEAD^
This command removes the most recent commit from the git repository, while leaving the changes in the working directory intact. (You can check this with git status.)
Note the --soft option in the first command. If you replace that with --hard, you'll both remove the last commit from the repository, and delete it from the working directory. This means all changes will be lost, so use with caution! It can be useful when you've made a mess of your directory, though.
After you've wound back the commit, you can then add your files separately, as you originally planned:
git add file1 git commit -m "file1 message" git add file2 git commit -m "file2 message"
Alternatively, you might add a file to the next commit, and then realize (before actually committing) that you didn't want to do that after all. To remove it again, use
git reset HEAD filename
Finally, if all you really want to do is change the log message of the last commit, you can use
git commit --amend -m "new message"
to amend the existing commit. Another win for the flexibility of Git!
Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of "Linux System Administration Recipes: A Problem-Solution Approach" (Apress, 2009).