0

During the development process it is common (at least for me) to do several add commands without a commit command between them. From this, I would like to know if git supports undo changes in a file in relation to the last add command performed on it? Here goes a illustrative example:

Suppose you on a clean working tree that has the unique file foo.txt. You change some contents in this file and so you add it to the stage area with the add command. But, instead to do its commit in the sequence, you decide to perform more changes in the file foo.txt. After while, you realize that the changes are unnecessary and you want to undo the changes, not you do not want to undo all changes but just the ones that was done after the add command.

How to do accomplish this?

Thanks

Randerson
  • 775
  • 1
  • 5
  • 19
  • Have you tried this http://stackoverflow.com/questions/52704/how-do-i-discard-unstaged-changes-in-git#12184274 ? – Alissa Jan 12 '17 at 15:59

2 Answers2

0

If the file is git added :

git checkout file

will revert file on disk to the added state.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

I can start by saying that it appears git does keep a history of different versions of a file added to the staging area. I'm not a git expert by a long shot, but here's what I did to see what git does internally.

I created an empty git repository with git init. I then created a single file in this directory called "one" and placed the text "1" inside it. I then did a git add .. Jumping into the .git\objects folder, I see one folder for objects (I'm excluding the info and pack folders for now). In my case the folder was f3. I went into there, saw a single file called 3dfa25aa16a97ca941fe1d9cabdc3689179f3f so I displayed it's contents:

git cat-file blob f33dfa25aa16a97ca941fe1d9cabdc3689179f3f

And the content was "1".

I then repeated the process above in a new git repository but this time, I did what you had mentioned: edited a file, did a git add, then edited the file some more and did another git add on the same file. Now when I got into the .git\objects folder I can see two folders (again, ignoring info and pack for now). Checking the contents of each blob under the two folders shows the different content I had edited.

So does git keep some kind of a linked list of these different objects in the staging area? That's an interesting question that I unfortunately can't answer.

The moral of ths story: commits are cheap in git so commit early and commit often.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54