HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Discard uncommitted or untracked changes in Git

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
gituncommittedchangesuntrackeddiscard

Problem

Made some changes you don't want to keep? Nothing to worry about, you can simply discard them and move on.
If you have made changes to your files but haven't committed them yet, you can discard them using the git reset --hard HEAD command. This command will reset your working directory to match the latest commit on the current branch, discarding all uncommitted changes.
> [!WARNING]
>
> Be careful when using git reset --hard HEAD, as it is a potentially destructive action. Make sure you don't have any changes you want to keep before running this command.

Solution

# Usage: git reset --hard HEAD

git reset --hard HEAD
# Discards all uncommitted changes


> [!WARNING]
>
> Be careful when using git reset --hard HEAD, as it is a potentially destructive action. Make sure you don't have any changes you want to keep before running this command.
Similarly, if you have untracked files in your working directory that you want to discard, you can use the git clean -f -d command. This command will remove all untracked files and directories from your working directory.
> [!WARNING]
>

Code Snippets

# Usage: git reset --hard HEAD

git reset --hard HEAD
# Discards all uncommitted changes
# Usage: git clean -f -d

git clean -f -d
# Discards all untracked changes

Context

From 30-seconds-of-code: discard-changes

Revisions (0)

No revisions yet.