2

As I read through as many sources on how to use git, I haven't come across any article which suggests when you can rollback should the need arise. So for example I have initialized a git directory in my working directory i.e. git init

I then add files to git's index i.e. git add .. This has added all files to git's index. Now rather than committing the files to git's repository i.e. git commit, I make a change to one of the files I added to the index. I realize I made a mistake. Can I rollback to the previous state or is this only possible when I have committed the file? My understanding is that when I run the command git add . a copy of the file or files is made in git's object store. I believe the files in the object store are compressed. Does this mean I can rollback from the object store?

Maic López Sáenz
  • 10,385
  • 4
  • 44
  • 57
PeanutsMonkey
  • 6,919
  • 23
  • 73
  • 103

4 Answers4

2

Yes, you can. Try

git checkout-index -f foo

to checkout foo from index. Or checkout all files by

git checkout-index -fa
dyng
  • 2,854
  • 21
  • 24
2

Use

git checkout -- <file>

to discard changes in working directory while keeping changes to index.

Zombo
  • 1
  • 62
  • 391
  • 407
1

Using git checkout without specifying a tree-ish will checkout from the index.

Maic López Sáenz
  • 10,385
  • 4
  • 44
  • 57
  • Sorry for being a n00b. What does that mean exactly? – PeanutsMonkey Jan 24 '13 at 01:04
  • Usually you use `git checkout ` to checkout a branch, tag, HEAD, or a specific commit. If you dont use that parameter the checked out content will come from the index. For what is a treeish check: http://stackoverflow.com/questions/4044368/what-does-tree-ish-mean-in-git – Maic López Sáenz Jan 24 '13 at 01:13
1

I got intrigued by the question and the answer appears to be "yes", git add puts a copy of the file in the repository and you can rollback the file even if no commit has been made in the repository, tested with the following example.

% mkdir test && cd test
% git init .
% echo "1" > test.txt
% git add test.txt
  - git status shows now "added" file
% echo "2" > test.txt
  - git status shows "added" and "modified" file, test.txt contains number 2
% git checkout test.txt
  - git status shows only "added" again, and test.txt contains the number 1
jhcaiced
  • 716
  • 8
  • 11