In my experience git commit -a
has had equivalent behavior to git commit .
However, recently I have created a pre-commit hook that automatically formats my source code and now git commit .
has some unexpected side-effects: the file that is committed ends up as modified in the working directory and in the index after the commit command finishes. This doesn't happen with git commit -a
. I'm trying to understand what is going on behind the scenes when running git commit .
that is causing this to happen and see if there is a way to handle it properly within my pre-commit hook script.
pre-commit hook:
git_toplevel=$(git rev-parse --show-toplevel)
git --no-pager diff -z --cached --name-only --diff-filter=ACMRT | $git_toplevel/meta/reformat.bash -s files
git --no-pager diff -z --name-only --diff-filter=ACMRT | xargs -0 --no-run-if-empty git add
Currently using git version 1.8.3.1 but am seeing the same behavior in more recent versions.
Here are the sequence of commands for a simple space added at the beginning of a line:
[]$ git status
# On branch eroller/format-clean-filter
# Your branch is ahead of 'origin/eroller/format-clean-filter' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: src/host/cnv/denovo/denovo_cnv.cpp
#
no changes added to commit (use "git add" and/or "git commit -a")
-
[]$ git diff
diff --git a/src/host/cnv/denovo/denovo_cnv.cpp b/src/host/cnv/denovo/denovo_cnv.cpp
index 7cfb8dc..14058e3 100644
--- a/src/host/cnv/denovo/denovo_cnv.cpp
+++ b/src/host/cnv/denovo/denovo_cnv.cpp
@@ -28,7 +28,7 @@ using namespace std;
namespace cnv {
namespace denovo {
-SegmentsBySample LoadCallsForSamples(const vector<string>& callFiles, const ReferenceDictionary& reference)
+ SegmentsBySample LoadCallsForSamples(const vector<string>& callFiles, const ReferenceDictionary& reference)
{
function<SegmentsBySample::value_type(const string&)> loadCalls = [&](string callFile) {
return LoadCalls(callFile, reference);
-
[]$ git commit -m 'test' .
-
[]$ git status
# On branch eroller/format-clean-filter
# Your branch is ahead of 'origin/eroller/format-clean-filter' by 2 commits.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: src/host/cnv/denovo/denovo_cnv.cpp
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: src/host/cnv/denovo/denovo_cnv.cpp
#
-
[]$ git diff
diff --git a/src/host/cnv/denovo/denovo_cnv.cpp b/src/host/cnv/denovo/denovo_cnv.cpp
index 14058e3..7cfb8dc 100644
--- a/src/host/cnv/denovo/denovo_cnv.cpp
+++ b/src/host/cnv/denovo/denovo_cnv.cpp
@@ -28,7 +28,7 @@ using namespace std;
namespace cnv {
namespace denovo {
- SegmentsBySample LoadCallsForSamples(const vector<string>& callFiles, const ReferenceDictionary& reference)
+SegmentsBySample LoadCallsForSamples(const vector<string>& callFiles, const ReferenceDictionary& reference)
{
function<SegmentsBySample::value_type(const string&)> loadCalls = [&](string callFile) {
return LoadCalls(callFile, reference);
-
[]$ git diff --cached
diff --git a/src/host/cnv/denovo/denovo_cnv.cpp b/src/host/cnv/denovo/denovo_cnv.cpp
index 7cfb8dc..14058e3 100644
--- a/src/host/cnv/denovo/denovo_cnv.cpp
+++ b/src/host/cnv/denovo/denovo_cnv.cpp
@@ -28,7 +28,7 @@ using namespace std;
namespace cnv {
namespace denovo {
-SegmentsBySample LoadCallsForSamples(const vector<string>& callFiles, const ReferenceDictionary& reference)
+ SegmentsBySample LoadCallsForSamples(const vector<string>& callFiles, const ReferenceDictionary& reference)
{
function<SegmentsBySample::value_type(const string&)> loadCalls = [&](string callFile) {
return LoadCalls(callFile, reference);
UPDATE: Using the very thorough answer from @torek (thanks!), I decided to give an error in the pre-commit hook if the user tries to use git commit .
or git commit [--only] -- <files>
. Here is the check in my pre-commit script:
if [[ $GIT_INDEX_FILE != *"/index" ]] && [[ $GIT_INDEX_FILE != *"/index.lock" ]] ; then
echo "Error: pre-commit reformatting using unsupported index file ($GIT_INDEX_FILE)." >&2
echo " Are you using 'git commit [--only] -- <files>' to bypass staging?" >&2
echo " Use git commit -a or stage your files before committing using git add -- <files>" >&2
echo " Use '--no-verify' to bypass reformatting (not recommended)" >&2
exit 1
fi