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 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.
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 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
© 2022 - 2024 — McMap. All rights reserved.