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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With