DEV Community

Cover image for Git and GitHub: How to Revert a Single File.
Joseph Trettevik
Joseph Trettevik

Posted on • Updated on

Git Revert File Git and GitHub: How to Revert a Single File.

Song of the Week

Introduction

Once you start collaborating with other developer it's going to be important to know how to revert a single file to a certain commit. This need arises because you sometimes need to change files not related to you're pull request in order to test the feature you're working on. However, manually changing each line of code in those files back to their original state and doing a new commit can lead to a messy commit history. Reverting the file is a much cleaner what to handling it.

Find the Commit ID

First you need to go to the shared repository on GitHub and find the file that you want to revert. Once you navigate to the file, right above the file you should see this:
Alt Text
On the right hand side you can see a 7 digit commit ID and a date. That is the commit ID for the most recent commit in which that file was modified. Either write this commit ID down, or copy it to your clipboard.

Find the File Path

The next thing you need is the path to the file from the working directory. This part is easy because the path to the file is on the same GitHub screen where you found the commit ID for the file.
Alt Text
Above you can see the same screenshot from before, but this time I've underlined the file path. Notice I only underlined part of the path. The first directory listed is the working directory name, and will be the directory you're in when using this file path. Because of this, you only want the underlined portion.

Revert the file.

All that is left is to revert the file. Once you've opened a terminal and changed to the working directory, you use git checkout to revert the file. The format of the git command will look like this:

git checkout [commit ID] -- path/to/file

If I were going to revert the file in the screenshots above, that would look like this:
Alt Text

Commit the Change

I know what you're thinking, "Wait a minute, I thought the whole point was to not create a new commit?" Well that's half true. We didn't want a new commit for the file we reverted. But once we revert the file, we need to commit that change. In this case, the change is a revert of a single file. This done with the standard commit command:

git commit -m 'commit message'

Then you can push that commit to the remote so that the version of your branch on GitHub matches your local version.

Takeaways

To revert a single file to a specific version do the following:

  • Find the commit ID of the version of the file you want to revert to.
  • Find the path to the file you want to revert from the working directory.
  • In the terminal, change directories to the working directory.
  • Type git checkout [commit ID] -- path/to/file and hit enter.
  • Commit the change to the reverted file.

References

Cover Image
How can I reset or revert a file to a specific revision? - Stackoverflow

Top comments (14)

Collapse
 
gerrytan profile image
Gerry Tan

In most cases you want to checkout to the commit before the most recent ones. So the command should be:

git checkout [commit ID]~1 -- path/to/file

~1 here is reference to the commit's first parent, more info.

Collapse
 
connor11528 profile image
Connor Leech

You can also leave the commit hash out, to for instance go back to the most recent commit (the HEAD):

git checkout -- path/to/your/file
Enter fullscreen mode Exit fullscreen mode
Collapse
 
radley112 profile image
radley112

Takeaways: @geometry dash
Find the commit ID of the version of the file you want to revert to.
Find the path to the file you want to revert from the working directory.
In the terminal, change directories to the working directory.
Type git checkout [commit ID] -- path/to/file and hit enter.
Commit the change to the reverted file.
(this is the first result i found)

Collapse
 
peterhenry9999 profile image
Peter Henry

I'm trying this, and all is great...until the last step of trying to push. I get this error,
fatal: You are not currently on a branch (but I am, I can clearly see I'm on the branch that has the commit hash as stated above)
To push the history leading to the current (detached HEAD)
state now, use
git push origin HEAD:

and when I do that....
! [rejected] HEAD -> name of my branch <non-fast-forward)
error: failed to push some reds to 'bitbucket.org/company/more paths'

The red errors with fail and rejected is not confidence inspiring. What am I doing wrong?

Collapse
 
shahjapan profile image
Japan Shah

Thanks @lofiandcode for the nice article - I directly jumped to Takeaways section and it worked.

Collapse
 
nathan-wells profile image
Nathan wells

In the world of version control, mistakes happen. Fortunately, Git offers several ways to revert changes, including reverting a single file. This article will guide you through two methods to revert a single file in Git and GitHub:

1. Using Git Checkout:

This approach allows you to revert to a specific commit for a single file.

Steps:

  1. Find the commit ID of the version you want to revert to.
  • Use git log to view the commit history of the file.
  • Navigate to your local repository.

  • Run the command: git checkout [commit ID] --

  • Replace [commit ID] with the actual commit ID you found.

  • Stage the changes: git add

  • Commit the changes: git commit -m "Revert changes to "

  • Push your changes to GitHub: git push origin HEAD

2. Using Git Reset:

This method allows you to discard changes made to a single file and revert it to the state in the working directory or the index.

Steps:

  • Navigate to your local repository.

  • Run the command: git reset HEAD
    This resets the file to the state in the HEAD commit.

  • Alternatively, to revert to the state in the index: git reset HEAD^

  • Stage the changes: git add

  • Commit the changes: git commit -m "Revert changes to "

  • Push your changes to GitHub: git push origin HEAD

Remember:

  • Always back up your repository before making any significant changes.

  • Consider using a commit message that clearly describes the revert action.

  • These methods only revert changes locally. You need to push your changes to GitHub to reflect them on the remote repository.

Additional Resources:

Git documentation on checkout: git-scm.com/docs/git-checkout
Git documentation on reset: git-scm.com/docs/git-reset
GitHub Help article on reverting commits: docs.github.com/en/desktop/managin...
By understanding these methods, you can easily revert changes to a single file in Git and GitHub, ensuring your code base remains clean and reflects your desired state as shoviv.

Collapse
 
zirkelc profile image
Chris Cook
git show some_commit_sha1 -- some_file.c | git apply -R
Enter fullscreen mode Exit fullscreen mode

stackoverflow.com/a/7196615/1967693

Collapse
 
viniciusrio profile image
Vinicius Rio

Simple and useful! Thanks

Collapse
 
pradeepkatiyar007 profile image
pradeepkatiyar007

So, while the guide focuses on a specific task, the underlying techniques offer a powerful way to explore and navigate the history of your code, like a developer's own time machine!

Wonderful for OST vs PST!

Collapse
 
victoreijkhout profile image
Victor Eijkhout

Please proofread. Second sentence "you're" s/b "your". Last sentence of that paragraph "what" s/b "way". Et cetera.

Collapse
 
jodoesgit profile image
Jo

This is good know-how. Thank you Joseph!

Going to bookmark it, and keep the knowledge of the possibilities in my brain bin.

Collapse
 
rajarajacholank profile image
Rajarajacholan

You nailed it, it works well! So much details yet essential. Appreciate your passion and patience!!!

Collapse
 
mandyedi profile image
Eduard Mandy

The new commit can be avoided with amend or squash.
E.g. If I revert a file from the last commint I just

git commit --amend
Enter fullscreen mode Exit fullscreen mode

after the checkout.

Collapse
 
Sloan, the sloth mascot
Comment deleted