7

I have some staged and some unstaged changes in git, as well as some untracked files. I would like to keep the unstaged changes and untracked files, and discard the staged changes.

Whats the easiest way to do this on the command line?

Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105

3 Answers3

8

I have some staged and some unstaged changes in git, as well as some untracked files. I would like to keep the uncommited changes and untracked files, and discard the commited changes.

As you clarified in your comment, you mean "... and discard the staged changes" (not the committed changes). Normally, you would use git reset to undo the git add. But in your case you want to keep your unstaged changes, so:

git commit     # move staged to a commit, does not touch unstaged changes/unadded files

git checkout HEAD^    # checkout the previously committed version while keeping unstaged changes/unadded files   

git branch yourbranchname --force    # to grab the branch and move it back as well
AnoE
  • 8,048
  • 1
  • 21
  • 36
  • 1
    Actually, the question does make sense in my use case. So I don't want to unstage, but really unstage and discard the staged changes, without losing the (currently) unstaged changes... – Jan Rüegg Jun 22 '16 at 13:43
  • 1
    Yes, but you wrote "discard the committed changes". You mean "the staged changes". Important difference here, you might want to update your question. – AnoE Jun 22 '16 at 13:45
  • Oh, there you're right of course, that was a typo. Thanks for the hint, I updated the question! – Jan Rüegg Jun 22 '16 at 14:16
1
git reset HEAD^

Reset HEAD to its parent, and now the commit changes are discarded in the index but kept in the work tree.

git checkout -- paths_of_files_whose_changes_are_to_be_discarded_in_the_work_tree

Discard the changes in the work tree.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Sorry there was an error in the question so I updated it (I wrote "commited" instead of "staged" mistakenly). So I think your answer doesn't fit anymore... – Jan Rüegg Jun 22 '16 at 14:17
  • @JanRüegg Then, `git reset -- path_of_file` can discard the changes of the file in the index. A further `git checkout -- path_of_file` can discard the chages in the work tree. – ElpieKay Jun 22 '16 at 14:29
  • Nice idea... however, there are quite a lot of files in the index, so I went for the other solution to save some typing ;) – Jan Rüegg Jun 22 '16 at 14:38
  • @JanRüegg it's okay as long as it could help. – ElpieKay Jun 22 '16 at 14:40
0

To revert unstaged changes and keep staged changes, add this to your ~/.gitconfig:

[alias]
    revert-unstaged = "!sh -c '{ git commit -m=tmp && git reset --hard HEAD && git reset --soft HEAD^ || git reset --hard HEAD; } > /dev/null 2>&1; git status'"

Then you can do git revert-unstaged, which does the following:

  • Create a local commit if there are staged changes

  • Delete any unstaged changes using git reset --hard HEAD

  • If changes were staged, revert the local commit to restore them

  • Run git status for an overview of the new state

aleclarson
  • 18,087
  • 14
  • 64
  • 91