Optimize Your CI/CD Pipeline
Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.
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.
Steps we will cover in this article:
- Removing Untracked Files
- Temporary Changes Using git stash
- Reverting Uncommitted Changes
- Handling Staged Changes
- Performing a Hard Reset
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
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
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
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
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
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
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.
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>
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 .
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>
And to discard all changes in the working directory:
git restore .
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>
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 get completely rid of those changes, including from the working directory, you could continue by using:
git restore <file>
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 .
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
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
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.