Discard Untracked Changes

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

When working with Git, it's common to have untracked changes in your working directory. These changes are files that Git is not currently tracking. Sometimes, you may want to discard these changes and start fresh. In this lab, you will learn how to discard all untracked changes to the current branch.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/clean("`Clean Workspace`") subgraph Lab Skills git/clean -.-> lab-12730{{"`Discard Untracked Changes`"}} end

Discard Untracked Changes

You are working on a project using Git and have made some changes to your working directory. However, you realize that you don't need these changes and want to discard them. You want to discard all untracked changes to the current branch.

To complete this lab, you will use the Git repository named https://github.com/labex-labs/git-playground. Follow these steps:

  1. Navigate to the repository directory:
cd git-playground
  1. Check the status of your working directory:
git status

You should see the following output:

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

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new-file.txt

nothing added to commit but untracked files present (use "git add" to track)
  1. Discard all untracked changes to the current branch:
git clean -f -d
  1. Check the status of your working directory again:
git status

You should see the following output:

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

nothing to commit, working tree clean

The git clean -f -d command has discarded all untracked changes to the current branch.

Summary

Discarding untracked changes is a useful Git command when you want to start fresh and discard any changes that Git is not currently tracking. Use the git clean -f -d command to discard all untracked changes to the current branch.

Other Git Tutorials you may like