1

New newbie here. This is related to manipulating staging area of git.

Say I have a file named cat.txt.

It's git committed version has a single line in it. Then I edited cat.txt added a second line and staged it. Then edited again, added third line and staged that too. My working copy and staging area reference to it contains three lines.

Now I decided the to commit the version with only two lines in it. How do I revert cat.txt in staging area back to its two lines version? Thanks

BBDG
  • 365
  • 4
  • 11
  • Possible duplicate of [GIT add revert in my case (keep changes)](http://stackoverflow.com/questions/8388519/git-add-revert-in-my-case-keep-changes) – perror Jan 29 '16 at 14:54

2 Answers2

0

remove it from the index and add only 2 lines:

# remove form index 
git rm --cached cat.txt

# add only the 2 desired lines
git add -p 

# choose the split method and select the content to add

'git reset'

Another option is to use git reset

git reset

This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>, which must be one of the following:

--soft

Does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do).
This leaves all your changed files "Changes to be committed", as git status would put it.

--mixed

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

--hard

Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

If your case is truly as simple as the example you gave, and you want to continue work with the 3 line version, then simply make a backup of cat.txt, edit it to contain only the two lines, add it to the index, then restore cat.txt in your working directory to the 3 line version.

However, I suspect the example was exactly that: just an example, and your current situation involves more changes, and you are looking for a more general "git way" of restoring the index to a previous state. Unfortunately, that is not possible: the index is simply a holding area for a snapshot of the code, it itself is not versioned.

Going forward, I would recommend committing your incremental changes rather than just adding them to the index. This would give you the versioning you desire, and you can always flatten those commits before you push to the repository.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54