Git. Undo staged changes in working directory, keeping staged area untouched

Suppose I have staged a few changes:

$ git diff --cached # few changes are stashed

+ change 1 new
  blabla
- change 2 old
+ change 2 new
  blabla
- change 3 old
+ change 3 new
  blabla

$ git diff  # no other changes

Now I would like to test my program quickly without change 2. With git reset head -p; git stash I can rid of change 2 completely. But I don’t want to unstage it, this is just a test, I still want to commit it later on. Therefore I would like to make the changes opposite to the changes from change 2 in my working directory. The result should be like this:

$ git diff --cached # 1-3 changes are stashed

+ change 1 new
  blabla
- change 2 old
+ change 2 new
  blabla
- change 3 old
+ change 3 new
  blabla

$ git diff  #change 2 is undone

  change 1 new
  blabla
+ change 2 old
- change 2 new
  blabla
  change 3 new
  blabla

How do I achieve this?

  • Can you stage a file (or at least, a section of a file) DURING a git diff?
  • Git: How to “restage” the same lines which were staged?
  • Where does git status get its information on changed files?
  • Git (SourceTree): gitignore already tracked files without deleting from online repository
  • Git: prevent staging some files, automatically stage all
  • How to undo 'git add' before commit?
  • Getting Git-concept of “stage”
  • Git: How to re-stage the staged files in a pre-commit hook
  • One Solution collect form web for “Git. Undo staged changes in working directory, keeping staged area untouched”

    One of your biggest problems comes from the fact that if your changes are independent(ish), then you should probably commit them independently first and treat them as three different commits. IE, use git commit --interactive (or git add --interactive) to commit your changes in 3 change commits instead of 1. That lets you much more easily revert one (like #2) using git revert, or other change-management magic to deal with just the commit you’re looking for in question.

    Git Baby is a git and github fan, let's start git clone.