-3

Is there a way to craft a commit that adds or removes files without reading or writing to the staging area?

Note that you can for tracked files: git commit supports the same functionality as git add:

git commit --patch tracked.txt

Except if that involves adding or removing files – this doesn't work (yet):

git commit --add new.txt --rm old.txt

I would like to just never use the staging area at all.

The problem with the staging area is that lots of commands change it implicitly, it makes committing stateful and it's invisible in the diff. It trips me up more than I like, and I have no use for it (a commit stack is a much better superset of a staging area).

user2394284
  • 5,520
  • 4
  • 32
  • 38
  • 1
    If you want a storage mechanism without a history it's called drives or folders. And the commands are `cp` and `mv` – slebetman Jun 23 '23 at 23:31
  • 1
    Actually, committing with Git is "recording the index state", so you definitely want one, as it also serves to determine if a file has been changed or not. What you see with `git status` is determined by comparing the states of all three index, working directory and last commit (under HEAD). Maybe you want to consider it from a different angle : https://stackoverflow.com/questions/59179722/what-is-the-use-of-staging-area-in-git/59180489#59180489 – Obsidian Jun 24 '23 at 00:09
  • 1
    If you deny the index you deny Git. Here's what Git is: https://www.biteinteractive.com/picturing-git-conceptions-and-misconceptions/ Take it or leave it. – matt Jun 24 '23 at 00:14
  • 1
    Note that the index and the staging area are exactly the same thing. – matt Jun 25 '23 at 22:33

1 Answers1

1

Of course, it's possible to synthesize the objects yourself and write them into the repository with git hash-object. However, even the plumbing command git write-tree uses the index, so you need to have some index if you want to use something a little higher level than that.

If you need to work with a repository without modifying the regular index, you can do that with a temporary index using the environment variable GIT_INDEX_FILE. Typically you'll want to do a git read-tree to load in some existing commit or tree, and then update it appropriately using git update-index, before writing it with git write-tree and then committing with git commit-tree. That is a way you can write objects into the repository without creating a working tree.

Note that git restore updates the index and therefore is not stateless.

bk2204
  • 64,793
  • 6
  • 84
  • 100