Skip to contentSkip to conversation

understanding index

DISCUSSION Nicolas Sebrecht 4 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hey list.

I'm having some understanding trouble with git index. As I understand, 'git rm --cached' is not strictly the opposite of 'git add'. It's a little embarrassing in this particular case : what if you want to commit and did a wrong 'git add -u' command ?

Here's a minimalist sample :

% ls
foo bar

[ hack, hack, hack on both files ]

% git status
[...]
modified: foo
modified: bar
[...]
% git add -u foo bar

[ optional hack on foo ]
[ damn, you realize you don't want to commit changes on foo at all ]

% git rm --cached foo
% git status
[...]
deleted: foo
modified: bar
[...]

If committed as is, foo will be marked as deleted (in 'git log --name-status' at least, which is not wanted).

How to retrieve the state before the wrong 'git add -u' command _and_ keep the working tree as well (including last hacks) ? Is there any command which is the exact opposite of 'git add -u' ?

Cheers,

Signature · 1 line
Nicolas Sebrecht
#1 lore raw

2009/1/31 Nicolas Sebrecht <[email protected]>:

Quoted 22 lines

Hey list.

I'm having some understanding trouble with git index. As I understand, 'git rm --cached' is not strictly the opposite of 'git add'. It's a little embarrassing in this particular case : what if you want to commit and did a wrong 'git add -u' command ?

Here's a minimalist sample :

% ls
foo bar

[ hack, hack, hack on both files ]

% git status
[...]
modified: foo
modified: bar
[...]
% git add -u foo bar

Quoted 3 lines

[ optional hack on foo ]
[ damn, you realize you don't want to commit changes on foo at all ]

You omitted the help message of git status, where it says how to unstage:
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)

So to not commit foo at all:

git reset HEAD foo

HTH,
Santi

Quoted 24 lines

% git rm --cached foo
% git status
[...]
deleted: foo
modified: bar
[...]

If committed as is, foo will be marked as deleted (in 'git log --name-status' at least, which is not wanted).

How to retrieve the state before the wrong 'git add -u' command _and_ keep the working tree as well (including last hacks) ? Is there any command which is the exact opposite of 'git add -u' ?

Cheers,

--
Nicolas Sebrecht

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 7 lines · Santi Béjar

You omitted the help message of git status, where it says how to unstage:
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)

So to not commit foo at all:

git reset HEAD foo

Thanks. I didn't omitted the help message of git status. The "unstage" action is what I was looking for but I was wrongly presuming (and I've read git docs !) that 'HEAD' is a kind of shortcut to the last commit of the current branch (not to the last state of the working tree).

Working on branch master for example, we have in .git/HEAD :
ref: refs/heads/master
And in .git/refs/heads/master, the hash of the last commit of branch master.

I'm missing something here. But what ?

Signature · 1 line
Nicolas Sebrecht
#3 lore raw
Quoted 16 lines · Nicolas Sebrecht
Quoted 7 lines · Santi Béjar

You omitted the help message of git status, where it says how to unstage:
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)

So to not commit foo at all:

git reset HEAD foo

Thanks. I didn't omitted the help message of git status. The "unstage" action is what I was looking for but I was wrongly presuming (and I've read git docs !) that 'HEAD' is a kind of shortcut to the last commit of the current branch (not to the last state of the working tree).

Quoted 6 lines

Working on branch master for example, we have in .git/HEAD :
ref: refs/heads/master
And in .git/refs/heads/master, the hash of the last commit of branch master.

I'm missing something here. But what ?

You're missing the fact that you want to reset the index entry to the state from HEAD. The working tree state is the modified one, and you get that into the index using "git add". But you want the index entry to be back at the state from HEAD. So HEAD is what you need to pass to reset (well, actually, you can skip "HEAD" there, it's the default).

Björn

#4 lore raw
In reply to Nicolas Sebrecht

2009/1/31 Nicolas Sebrecht <[email protected]>:

Quoted 7 lines · Santi Béjar
Quoted 5 lines

You omitted the help message of git status, where it says how to unstage:
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)

Quoted 5 lines
Quoted 3 lines

So to not commit foo at all:

git reset HEAD foo

Thanks. I didn't omitted the help message of git status.

These help messages (helpful messages?) were not in old version, but in current git you get this indications when you execute "git status", and it was what you put as "[...]".

$ git status
# On branch next
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   Makefile
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#	modified:   Makefile
#
Quoted 4 lines

The "unstage"
action is what I was looking for but I was wrongly presuming (and I've read git docs !) that 'HEAD' is a kind of shortcut to the last commit of the current branch (not to the last state of the working tree).

By default git reset only acts on the head of the branch and the index, so with:

git reset HEAD foo

what you are saying is, use the HEAD as the head of the branch (don't change my branch), but put in the index the state of foo in HEAD, so in brief reset the index state of foo.

Quoted 5 lines

Working on branch master for example, we have in .git/HEAD :
ref: refs/heads/master
And in .git/refs/heads/master, the hash of the last commit of branch master.

Right.

HTH,
Santi

#5 lore raw
Quoted 7 lines · Santi Béjar

By default git reset only acts on the head of the branch and the index, so with:

git reset HEAD foo

what you are saying is, use the HEAD as the head of the branch (don't change my branch), but put in the index the state of foo in HEAD, so in brief reset the index state of foo.

Thank you all for the explanations.

Signature · 1 line
Nicolas Sebrecht
#6 lore raw
In reply to Santi Béjar

2009/1/31 Santi Béjar <[email protected]>:

Quoted 10 lines

2009/1/31 Nicolas Sebrecht <[email protected]>:

Quoted 5 lines

I'm having some understanding trouble with git index. As I understand, 'git rm --cached' is not strictly the opposite of 'git add'. It's a little embarrassing in this particular case : what if you want to commit and did a wrong 'git add -u' command ?

So to not commit foo at all:

git reset HEAD foo

Or look at git gui, which allows you to select the not-to-commit parts visually