How do I discard staged files using git?
I have a bunch of staged changes and a bunch of unstaged changes. Is there a git command to completely discard all my staged changes and in the same time keeping my unstaged changes as is?
I don't want the staged changes to exist anymore. I just want them out of this world, but I wanna keep my unstaged changes as is.
Assuming staged and unstaged changes are non conflicting,
Here's how you can achieve this:
git stash save --keep-index git reset --hard git stash pop
First command [stash with --keep-index] will stash only unstaged files. Second [ reset --hard ] will get rid of all existing changes (which now include only staged files). The third [stash pop] one will unstash the previously unstaged files.
Alternate approach can be: Commit the staged files, stash the remaining changes, remove the top commit, pop the stashed changes. Like below:
git commit -m "temp commit" git stash git reset --hard HEAD~1 git stash pop
How to remove files from git staging area?, Right below the “Changes to be committed” text, it says use git reset HEAD <file>. .. to to discard changes in working directory) modified: CONTRIBUTING.md. The git stash command shelves changes made to your working copy so you can do another work, get back, and re-apply them. It takes both staged and unstaged changes, saves them for further use, and then returns them from your working copy. You can delete the stash with git stash drop. To remove all the stashes, you should use git stash clear. Cleaning Files¶ The git clean is an undo command that completes other commands like git reset and git checkout. Unlike the other commands, this command
The simplest solution - which does not involve creating tmp commit (git stash
actually creates tmp commit too):
First, create a patch between the Index and the HEAD
commit, save to a tmp file:
git diff --cached -R > ~/tmp.patch
Notice the -R
option to reverse the direction of the diff.
Second, reset the Index:
git reset
Finally, apply the patch to the Working Dir:
git apply ~/tmp.patch
Undoing Things, To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The git rm command� How do I discard changes in my working copy that are not in the index? Another quicker way is: git stash save --keep-index After that, you can drop that stash with a git stash drop command if you like.
To unstage all files (see comments : not what OP wanted) simply
git reset
(HEAD
is implied when no ref is given)
Your local changes are kept, but the index (staging area) is cleared.
Recording Changes to the Repository, How to remove stuff in Git. Remove files from the tree. Scenarion 1: make a commit and notice a stray� Git Diff Unstaged. Shows the changes between the Working Directory and the Staging Area: $ git diff Git Diff Staged. Shows the changes between the Staging Area and the HEAD: $ git diff --staged - or - $ git diff --cached - or - $ git status -v Create an alias git diffs, if you need to check these changed often:
Git: removing files from the staging area and the tree – clubmate.fi, Make changes to the hello.html file in the form of an unwanted comment git status # On branch master # Changes to be committed: # (use "git reset HEAD� Note that staging is when you use git add [file] to add a file to Git’s staging area in preparation for committing it. If you want to discard all unstaged changes, you can do so with this command: git clean -f This will revert all those files that are unstaged back to their old state.
15. Cancel Staged changes (before committing), To remove files from stage use reset HEAD where HEAD is the last commit of the current branch. This will unstage the file but maintain the modifications. git reset� Unstage files using git reset The easiest way to unstage files on Git is to use the “git reset” command and specify the file you want to unstage. git reset <commit> -- <path> By default, the commit parameter is optional : if you don’t specify it, it will be referring to HEAD.
Unstage, Undo changes before they have been staged; Unstage modified files after they have been staged; Revert back to a� You can reset the staging area in a few ways: Reset HEAD and add all necessary files to check-in again as below: git reset HEAD ---> removes all files from the staging area git add <files, that are required to be committed> git commit -m "<commit message>" git push. share. Share a link to this answer. Copy link.
Comments
- Your unstaged changes are, in a sense, based on the staged changes. I don't know that there is a simple way to do what you want. The most straightforward might be to commit the two separately, then do an interactive rebase to remove the first commit (containing the original staged changes) . This might entail resolving some merge conflicts for the second commit (which contain the original unstaged changes).
- Actually,
git stash save --keep-index
saves exactly the same thing asgit stash save
: both make two commits, one for the index and one for the work-tree. The difference is thatgit stash save
ends withgit reset --hard
andgit stash save --keep-index
doesn't do thegit reset --hard
. So you could justgit stash save; git stash pop
here. But that's equivalent to justgit reset
with no arguments. - (The second method, committing the index first, will do what the OP apparently wants.)
- @torek
git reset
will take files from staging area back to working area. If there are 2 staged files and 3 changed unstaged files, doinggit reset
will simply put 2 staged files back in to working area which will now have 5 changed files. What OP wants I guess is removing files in staged area entirely while keeping the 'just changed [And not staged]' files intact. git stash save --keep-index
only stashes working area files keeping the files in the staged area intact. Hence, the first solution works as in a 3 step process, it 1) Stashes working area files 2) Removes staging area files 3) Brings back originally stashed working area files. Doing justgit reset
will not achieve the goal.- No,
git stash save --keep-index
makes the same two commits as any othergit stash
. What the--keep-index
flag does is change the final act ofgit stash
so that instead ofgit reset --hard
(to matchHEAD
), it adjusts the index and work-tree to match the saved index commit. Thegit stash
code is being converted away from shell script, so this is clearer in older versions of Git, but it really doesn't change the saved commits at all. - Reset the index with
git diff reset
? Was it a typo forgit reset
? - I think the staged changes should be discarded completely, not simply folded back into the set of unstaged changes.
- @chepner Oh. Now that I reread the question, I think you might be right. Let's hope OP will precise it.
- Yes.. I should've read your comments. Now all my changes are mixed up but still kept :-/
git reset --mixed
(orgit reset
with no arguments) should do what was asked-for. The accepted answer achieves exactly the same thing, after all.- @NghiaBui: I think I understand: if, say, file F in the commit reads "I am original F" and in the index reads "I am new file F" and in the work-tree reads "I am new file F" (i.e., both index and work-tree copies match but neither matches the HEAD commit), the OP wants file F in index and work-tree to go back to matching the committed version. It's true that
git reset --mixed
won't achieve that—but neither would git stash.