git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: index manipulation -- how?

From
Linus Torvalds <torvalds@osdl.org>
Date
Nov 22, 2005, 05:23 UTC
Message-ID
<Pine.LNX.4.64.0511212113080.13959@g5.osdl.org>
In-Reply-To
<20051122050337.46450.qmail@web31808.mail.mud.yahoo.com>
On Mon, 21 Nov 2005, Luben Tuikov wrote:
> 
> How do I reverse a _single_ "git-update-index" operation?
> Be it --add or just an update.
An "add" is easy enough to undo: just do a
	git-update-index --force-remove filename

which will remove the entry from the index even if the file on your filesystem still remains (so you can "git add" it later when you do want to commit it).

For a file that you had in your old index, but you've updated (either mode or SHA1), you need to figure out what the old mode/sha1 was. USUALLY this would be just the state that you still have in your HEAD tree, but if you want to go back to something else, you'd need to figure out what that was.

If it's the "last commit" state (ie just your HEAD state), then do
	git-ls-tree HEAD filename
which will show you the info, and then you do
	git-update-index --cacheinfo <mode> <sha1> filename
from that state.
You could obviously script something like "git-downdate-index":
	#!/bin/sh
	filename="$1"
	tree_info=$(git-ls-tree HEAD -- "$filename")
	if [ -z "$tree_info" ]; then
		git-update-index --force-remove -- "$filename"
	else
		echo "$tree_info" | while read mode type sha1 name; do
			git-update-index --cacheinfo "$mode" "$sha1" "$filename"
		done
	fi

but the above is totally untested, and "git-downdate-index" is a really sucky name too, so you'd need to rename it and test whether it does what you want.

Hmm?
		Linus
Previous: Luben TuikovNext: Junio C Hamano
Message 2 of 7 in “index manipulation -- how?”
  1. Luben TuikovNov 22, 2005
  2. Linus TorvaldsNov 22, 2005
  3. Junio C HamanoNov 22, 2005
  4. Junio C HamanoNov 22, 2005
  5. Luben TuikovNov 22, 2005
  6. Junio C HamanoNov 22, 2005
  7. Luben TuikovNov 22, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.