Search code examples
gitgit-commitunstage

Remove unstaged, uncommitted files in git when checking out a different branch


I deleted both the local and remote branches for a particular branch

git branch -d branchName
git branch --delete --remotes origin/branchName

When I checkout out a different branch, I am still seeing the untracked/uncommitted files when I run git status.

Those files don't have any changes that I want to keep or stage or commit. I don't want to see them sitting in the area when I run git status on the different branch

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   ../../../../path/to/folder/file.html
    modified:   ../../../../path/to/folder/file.js

I want to see this when I run git status on any branch I checkout

On branch proj/branch/#
Your branch is up to date with 'origin/proj/branch/#'.

nothing to commit, working tree clean

Is there a way to do this, if so, how is that done?


Solution

  • Typically, to undo the changes on all files and restore them to their last commited state, you'd do :

    git reset --hard
    

    (here HEAD is implied)(doc)

    Warning however : this is not undoable.

    You could also just git stash, this would also get rid of the changes, but in case you want to get them back later or just inspect them, you'll be able to, either with a simple git stash show <stashRef>, or with git apply <stashRef>, where the <stashRef> is to be found with git stash list.


    To answer your comment about stash, in case you stashed something and don't need it any more, you have two ways of cleaning old stash entries :

    # global
    git stash clear
    
    # or specific
    git stash drop <entryRef>
    

    (where <entryRef> is found through git stash list)