How to Undo Local Changes to a Single File in Git
- Roll Back a Previously Committed File in Git
- Undo Changes to Staged and Unstaged Files in Git
-
the
git restore
Command
In this article, we will discuss how we can use commands like git checkout
and git reset
to roll back our files to a version of our liking. Although Git makes it easy to reset files and repositories, the concept of undoing changes in Git is a bit more complicated than you may think.
Undoing changes in a repo is different from undoing changes to a set of files you are working on locally.
The git checkout
, git reset
, and git restore
commands come in handy when rolling back files to a previous version. But before diving into these commands, we must ask ourselves; what does resetting a file mean?
It is worth noting that resetting a file can be different for you under different circumstances. You may find that a git checkout
command does not match your expectations.
It is different for people who want to roll back already committed changes and those who want to undo changes such that the file syncs with the repo.
Here are some terms you should look out for to understand the differences.
- Working tree
- Staging area
- Repository
Roll Back a Previously Committed File in Git
A common case is that we have committed a file and want to undo the changes. We will use the git checkout
command in such a scenario.
We previously used the command to switch branches, but that is just a part of what it can do.
We can use the command to undo changes to a file we have already committed. It will match the files in our working tree to the file at any point in our repo’s history.
We can specify a branch, tag, and commit.
Below is the best way to run the command.
$ git checkout [commit ID] -- path/to/file
When running the command above, we point Git to a specific commit ID in our repository. Additionally, we have added the path to a single file.
Running the command above will only update the file in our working tree. We will have to stage the file and make a fresh commit.
Undo Changes to Staged and Unstaged Files in Git
Our staging area contains the files we want to commit. To undo changes in a staged file, run the git reset
command to unstage it.
$ git reset HEAD path/to/file
We run the command below to undo local changes before staging a file.
$ git checkout -- path/to/file
The command is similar to our first one, except it omits our commit ID.
the git restore
Command
Newer versions of Git allow us to use the git restore
command to undo changes to a single file.
Here are some of the ways we can use the command.
-
Undo a change to the repo
$ git restore --source [commit id] path/to/file
-
Unstage a file
$ git restore --staged path/to/file
-
Undo changes to a working copy
$ git restore path/to/file
We often find ourselves rolling back commits rather than resetting a single file. However, when you need to, it is fairly straightforward.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn