tags:

views:

631

answers:

5

How do I undo parts of my unstaged changes in git but keep the rest as unstaged? The way I figured out is:

git commit --interactive
# Choose the parts I want to delete
# Commit the changes
git stash
git rebase -i master # (I am an ancestor of master)
# Delete the line of the most recent commit
git stash apply

This works, but it would be nice if there were something like git commit --interactive only for reverting changes. Any better methods?

A: 

you can do git checkout and give it name names of the parts you want to undo.

Andrew
A: 

How about

  1. Back out the affected files with changes from the index
  2. use git add -p to only add back the changes that you want to the index.
Abizern
+2  A: 

You could do

git checkout master -- path/to/file

For each file you want to reset.

Drew Hoskins
Or use HEAD in place of master if working on a branch. As it says in the 'git status' report (at least in recent versions).
Jonathan Leffler
+2  A: 

You can use git checkout -p, which lets you choose individual hunks from the diff between your working copy and index to revert. Likewise, git add -p allows you to choose hunks to add to the index, and git reset -p allows you to choose individual hunks from the diff between the index and HEAD to back out of the index.

$ git checkout -p file/to/partially/revert
# or ...
$ git checkout -p .

If you wish to snapshot your git repository beforehand to preserve these changes before reverting them, I like to do:

$ git stash; git stash apply

If you use that often, you might want to alias it:

[alias]
    checkpoint = !git stash; git stash apply

Reverting individual hunks or lines can be even easier if you use a good editor mode or plugin, which may provide support for selecting lines directly to revert, as -p can be a bit clumsy to use sometimes. I use Magit, an Emacs mode that is very helpful for working with Git. In Magit, you can run magit-status, find the diffs for the changes that you want to revert, select the lines you wish to revert (or simply put the cursor on hunks you wish to revert if you want to revert a hunk at a time instead of a line at a time), and press k to revert those specific lines. I highly recommend Magit if you use Emacs.

Brian Campbell
I think this is about as complex as the solution I originally came up with, but I think my original solution has the benefit of saving the removed lines for 30 days because they are committed. I'm thinking maybe it would be nice to have a shell script written around it, though.
asmeurer
Sorry, I just realized that `git checkout` also has a `-p` flag, which does exactly what you were asking for in a single command. Apologies for the previous complex set of steps; you can just use `git checkout -p`. As far as saving stuff goes, before doing something potentially destructive I often do a `git stash; git stash apply` (or create an alias that does that as `git checkpoint` or something) to record the current tree in a stash so I can get back to it if something goes wrong.
Brian Campbell
There you go, that's the solution! As far as the alias, I am thinking something like `git commit -a -m "Backup Commit" --edit; git reset HEAD^`would be better, because then it would not dirty my stash state, which I may be using for something else. Then, as long as you have the SHA1 you can cherry-pick it within the next 30 days. The --edit lets you add info to the commit message to assist you in finding the SHA1 later if you want it. On the other hand, this would dirty up the git reflog, so I guess it's a tradeoff based on what you do.
asmeurer
+1  A: 

When I run 'git status', it says:

$ git status
# On branch fr/fr.002
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   makefile
#
no changes added to commit (use "git add" and/or "git commit -a")
$

So, to cancel the unstaged edits, it tells me to run:

git checkout -- makefile
Jonathan Leffler
I think that he was wondering about how to revert individual hunks, not an entire file at a time. This is, of course, the solution if you just need to undo all the changes in a file.
Brian Campbell
Yup - I suspect you're correct. The 'git checkout -p' and 'git add -p' options seem likely to be what is wanted - as you said.
Jonathan Leffler