How to Discard Local Changes in Git

cicubeteam

CiCube

Posted on November 6, 2024

How to Discard Local Changes in Git


cicube.io

Introduction

Managing local changes in Git can be a nightmare, and especially if those changes are superfluous. This article considers various ways to discard the local changes of Git—from uncommitted changes to untracked files. Knowing how to return your project to the required state stands out as key in working with version control efficiently. I'll take you through the most effective commands that clean up your working directory and keep your workflow as smooth as possible.

Removing Untracked Files

First, you need to delete all files that are not tracked in your repository to keep it clean. For this, the necessary command is git clean. You can remove untracked files using:

git clean -f
Enter fullscreen mode Exit fullscreen mode

This command will delete all untracked files, which means those that have not been staged or committed yet. Use it carefully, because these changes cannot be reverted afterwards. If you would like to clean up entire untracked directories, you can extend the command with:

git clean -fd
Enter fullscreen mode Exit fullscreen mode

Here, the -d flag tells Git to remove untracked directories as well as untracked files. As you can imagine, this is a fairly aggressive - and potentially hazardous - command, so you should make sure you don't need anything in the untracked files before running it. You can also make this command ignore specific files or directories by placing them in your .gitignore file. If you really want to nuke all of your untracked files, including ones which are ignored due to .gitignore, you can use:

git clean -fx
Enter fullscreen mode Exit fullscreen mode

This will remove all untracked files and directories, including ones you might want to keep, so be careful.

Temporary Changes Using git stash

There are many circumstances when using Git that you'll want to save your changes without making a commit. This is where git stash comes in. The git stash command enables you to save your uncommitted changes to a safe place, actually refreshing your working directory to the last committed state.

To stash your changes, run:

git stash
Enter fullscreen mode Exit fullscreen mode

It takes your local changes and stashes them onto a stack, in essence giving you a clean slate to work from. This becomes quite useful when you would like to switch branches or do a pull of the latest updates without committing unfinished work.

Later, if you want to get your stashed changes back, you can use:

git stash pop
Enter fullscreen mode Exit fullscreen mode

This will apply the latest stash and remove it from the list of stashes. If you want to keep the change after applying, you can use:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Using git stash elegantly provides a lot of freedom in your workflow. You can easily make temporary changes and still keep your commit history clean.


Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring


Reverting Uncommitted Changes

Managing changes in a Git working directory requires knowledge in how to revert changes efficiently. In order to discard changes in particular files, use the following command:

git checkout <file>
Enter fullscreen mode Exit fullscreen mode

Replace <file> with the actual filename. That'll reset that file to its last committed state, thereby discarding any modifications you made since then. If you want to discard all changes in your working directory, the command couldn't be simpler:

git checkout .
Enter fullscreen mode Exit fullscreen mode

This reverts the status of all the tracked files to the last commit, but remember this will not affect untracked files.

Starting with Git version 2.23, there is a more intuitive way using the command git restore. In case you want to discard changes in a particular file, you can execute:

git restore <file>
Enter fullscreen mode Exit fullscreen mode

And to discard all changes in the working directory:

git restore .
Enter fullscreen mode Exit fullscreen mode

This will increase usability, especially for large changes. These commands are important to learn for keeping a clean project.

Handling Staged Changes

Managing changes that were staged with 'git add' means learning how to unstage and revert them correctly. To unstage a particular file, you would use the command:

git restore --staged <file>
Enter fullscreen mode Exit fullscreen mode

Replace <file> with the actual filename, and this relays the file out of the staging area while keeping your changes in the working directory. If you want to completely rid of those changes, including from the working directory, you could continue by using:

git restore <file>
Enter fullscreen mode Exit fullscreen mode

This reverts the file back to its last committed state. For completeness: if you have lots of files and you want to unstage all changes at once you can use:

git restore --staged .
Enter fullscreen mode Exit fullscreen mode

This helps in resetting the staging area for a clean commit. Knowing how to manage staged changes ensures that your commit history will only show those intended modifications.

Performing a Hard Reset

When you want to discard all the changes from your working directory, both staged and unstaged material, the command is:

git reset --hard
Enter fullscreen mode Exit fullscreen mode

This resets your local repository back to the last committed state, deleting all of the modifications you made since. To perform a hard reset, simply run:

git reset --hard
Enter fullscreen mode Exit fullscreen mode

Note that this command will delete your unsaved work permanently, so you should make sure before running this. One would use a hard reset when one has made many changes but realizes that none of them are worth saving or committing. Say you try out some features and don't plan to keep those changes, running a hard reset cleans up for you. It is also important to note that untracked files, which means files that have not been added into version control, are excluded from this command. So, effectively, you can clean your working directory of unwanted tracked changes while retaining any new files you have created. Knowing what a hard reset does can save you from losing data and make sure you take advantage of your Git workflow accordingly.

Conclusions

In all, discarding changes in Git is a critical skill that will keep you safe from errors and clutter that might build up in your working directory due to accidents. You can manage the state of your project with ease—from simple commands like git checkout and git restore to heavy artillery like git stash and git clean. Always make sure to review what it's going to change or remove so accidental losses of important work do not happen. This makes you more productive and allows you to keep your codebase clean using these techniques.

💖 💪 🙅 🚩
cicubeteam
CiCube

Posted on November 6, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Working with Git Remotes
git Working with Git Remotes

November 12, 2024

Deleting Git Commits
git Deleting Git Commits

November 5, 2024

Git Undo Last Commit
git Git Undo Last Commit

November 5, 2024