Can git commit empty versions of some files? The case in point is that I need new (untracked), non-empty files to first be added and committed as empty files, so as to mark their contents as being new and to be reviewed (the full, untracked file should not be added to the index; git diff
should show the newly added contents by comparing the file to its committed empty version).
There is git add -N file…
, which puts file
with an empty content in the index, but this only says that file
will be added, and git commit
complains that the file has not been added. The thing is that the current, non-empty version is not what has to be added, but only an empty version of the new file.
Is there a way to do this?
PS: This question is asked in the context of a program that automatically adds files to a git repository (my program follows what code students write). Uncommitted code is code that I have yet to approve. Thus, the state in which a program created by a student starts should be the empty state, even though my program just found a new, non-empty program in their home directory; this is handled by automatically committing a new, empty version of any new student program file in a git repository. Thus, new code lines that they write appear as being newly added contents, compared to the last committed git revision.
Pushing an empty commit without adding any staged files to the branch is very easy. It is the same as pushing a regular commit, except that all you need to do is add –allow-empty flag to the command line. The commit is now pushed to your branch without any changes.
Yes, indeed, by design, you can not commit empty directories, containing no files, to a Git repository.
To add and commit files to a Git repositoryEnter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository. For the <commit_message>, you can enter anything that describes the changes you are committing.
Without adding any files, the command git commit won't work. Git only looks to the staging area to find out what to commit. Staging, or adding, files, is possible through the command line, and also possible with most Git interfaces like GitHub Desktop by selecting the lines or files that you'd like to stage.
To be honest, I do not really understand what this is useful for. I would try to fix the review process instead of messing up the history. But if you really want to do this, here are several ways how:
The pragmatic approach:
mv file out-of-way
touch file
git add file
mv out-of-way file
The porcelain approach:
git add -N file
git add -p file
... and just answer "no" when asked whether the single hunk should be added. (Apparently this does not work anymore in 2019.)
The plumbing approach:
First, make sure an empty object exists in the object database:
git hash-object -w --stdin < /dev/null
This will return the SHA1 of an empty blob (which is e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
). You have to create this object only once. Now you can create empty files in the index by
git update-index --add --cacheinfo 0644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With