Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert staged changes, keep unstaged changes

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?

like image 222
Jan Rüegg Avatar asked Jun 22 '16 13:06

Jan Rüegg


2 Answers

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
like image 109
AnoE Avatar answered Sep 18 '22 00:09

AnoE


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.

like image 44
ElpieKay Avatar answered Sep 18 '22 00:09

ElpieKay