Revert staged changes, keep unstaged changes
Asked Answered
W

3

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?

Warrigal answered 22/6, 2016 at 13:10 Comment(0)
C
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
Covering answered 22/6, 2016 at 13:27 Comment(3)
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...Picture
Yes, but you wrote "discard the committed changes". You mean "the staged changes". Important difference here, you might want to update your question.Covering
Oh, there you're right of course, that was a typo. Thanks for the hint, I updated the question!Picture
S
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.

Slur answered 22/6, 2016 at 13:53 Comment(4)
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...Picture
@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.Slur
Nice idea... however, there are quite a lot of files in the index, so I went for the other solution to save some typing ;)Picture
@JanRüegg it's okay as long as it could help.Slur
G
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

Gnathic answered 7/11, 2018 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.