Remove local changes from a git branch

When you just want to start fresh, you can always re-clone the repo. However, if you have local feature branches hanging around, then you may just want to clean up a particular branch.

If you do a git status, you will see two sections. One is unstaged local changes (before commit), the other is local untracked files.

For unstaged local changes (changes to files already checked in before)

# discard and save changes for possible re-use
git stash

# discard changes of a file
git checkout <filename>

# discard all changes to all unstaged local files
git reset --hard

After you run the above commands, the untracked files will still stay put. It’s annoying if you have a long list of files/directories.

# to see what would be removed (just preview)
git clean -n

# to remove them
git clean -f

# or do the usual
rm -rf <file | dir>

ref:
https://docs.gitlab.com/ee/topics/git/numerous_undo_possibilities_in_git/
https://koukia.ca/how-to-remove-local-untracked-files-from-the-current-git-branch-571c6ce9b6b1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s