Can git commit “empty versions” of new, non-empty files?

前端 未结 2 1510
挽巷
挽巷 2021-02-04 03:43

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

2条回答
  •  萌比男神i
    2021-02-04 04:22

    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:

    1. The pragmatic approach:

      mv file out-of-way
      touch file
      git add file
      mv out-of-way file
      
    2. 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.)

    3. 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
      

提交回复
热议问题