Cleaning up the local working directory in Git
This article explains how to clean up your local working directory in Git. We will see how to eliminate uncommitted changes and untracked files.
git clean
We will also remove untracked folders
using the command. Let’s jump right in.
Cleaning up the local working directory in Git
Let's take the following working directory as our working directory.
Let's say we want to clean up everything in our working directory. This means discarding changes made to the README.md file and all untracked files and folders.
What should we do?
There are several commands we can use to process the modified files.
-
We can use
git checkout
the command. -
git restore <file>
Order. -
git reset --hard
Order.
See the example below.
$ git checkout README.md
The above command will reset our README.md file to the last committed state in our repository. It will eliminate the uncommitted changes.
Example 2:
$ git restore README.md
Git recommends using this command. It will discard the changes made to the README.md file in our working directory .
Example 3:
$ git reset --hard
This command comes in handy when we have dozens of files in our working directory. git reset --hard
Instead of checking or restoring them one by one, you can run the command.
It will reset our repository to the state of our last commit.
We have seen how to clean up the staging area. What about untracked files and folders?
Before deleting untracked files, it is recommended to do a back check. You can run it clean
the command with dry run option to see which files and folders will be discarded.
To do this, run:
$ git clean -n -d
Once you are sure you want to remove everything under untracked, run the following command.
$ git clean -f
The above command will delete the files. To delete the folder, run:
$ git clean -f -d
To remove ignored files, run:
$ git clean -f -X
Here is an example.
In short, we can use a few commands to clean up the working directory in Git. git clean
The command processes all the untracked files and folders.
Always run in dry run mode git clean
to check what will be deleted.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git authentication
Publish Date:2025/03/28 Views:163 Category:Git
-
This article demonstrates connecting a local repository to a remote repository on GitHub/Gitlab without getting 身份验证失败 error messages. Creating a local repository from scratch in Git To create a local repository from scratch, fo
Log graphs in Git
Publish Date:2025/03/28 Views:96 Category:Git
-
This article shows you how to use git log the command to graphically view the commit history in Git. Viewing log graphs in Git The command git log displays all the repository history at once snapshots(commits) . This command has a default f
Git refresh remote branch
Publish Date:2025/03/28 Views:93 Category:Git
-
Git is considered to be the most accurate and the most used software by developers in their projects and can be operated by multiple developers simultaneously. It provides many unique and quirky features to the developers which are very dif
Updating Git on Mac
Publish Date:2025/03/28 Views:181 Category:Git
-
When working on Git, you should stay updated with the latest version to get its latest features. This article will discuss how to install and update the latest version of Homebrew and Git on your personal computer. Homebrew on Mac Homebrew
Enable Git Tab Auto-Complete
Publish Date:2025/03/28 Views:109 Category:Git
-
This tutorial demonstrates how to enable git tab autocompletion. Importance of enabling Git Tab auto-completion When developers work with source code, they mostly prefer Git as it is a very familiar and convenient platform for developers th
Restoring a repository in Git
Publish Date:2025/03/28 Views:158 Category:Git
-
Sometimes while using Git, we come across a situation where we want to pull the latest changes from the remote repository and it conflicts with the existing modifications or files, then we have to push those files to the storage. Git provid
Undo Git Stash Pop conflicts
Publish Date:2025/03/28 Views:178 Category:Git
-
You can undo this using the solutions in this article git stash pop with merge conflicts . We show you how to abort an erroneous stash pop operation and return to a clean state. But we also demonstrated a git stash pop way to resolve the co
Resolving Git stash conflicts without committing
Publish Date:2025/03/28 Views:72 Category:Git
-
This article outlines the steps you should follow to resolve Git stash conflicts without reverting or creating commits. For a simpler context, we will simulate a situation where the git stash pop command results in a conflict and try to res
Git Stash needs to be merged
Publish Date:2025/03/28 Views:128 Category:Git
-
Git is a stylish platform that provides us with many features, one of the main ones is storage. With this unique feature, we can accumulate a lot of unchanged work that we don't want to commit to our repository when the code is checked in;