How Can I Reset or Revert a File to a Specific Revision?

Better Stack Team
Updated on June 24, 2024

To reset or revert a file to a specific revision in Git, you can use the git checkout command with the commit hash or branch name along with the path to the file you want to revert. Here's how you can do it:

Reset a File to a Specific Revision

If you want to reset a file to a specific revision (commit), you can use:

 
git checkout <commit> -- <file>

Replace <commit> with the commit hash or branch name you want to reset the file to, and <file> with the path to the file you want to revert.

For example, to reset a file named example.txt to its state in a commit with hash abc123:

 
git checkout abc123 -- example.txt

This command will replace the content of example.txt with the version from the specified commit.

Revert a File to a Specific Revision

If you want to create a new commit that reverts changes in a file to a specific revision, you can use git checkout with the ^ syntax to refer to the parent of the commit you want to revert:

 
git checkout <commit>^ -- <file>

This command will create a new commit that reverts changes in the specified file to the state of the parent commit.

For example, to revert changes in example.txt to the state of the parent of commit abc123:

 
git checkout abc123^ -- example.txt

Note:

  • Remember to replace <commit> with the appropriate commit hash or branch name, and <file> with the path to the file you want to reset or revert.
  • Resetting a file changes its content in your working directory to match the specified commit, while reverting creates a new commit that undoes changes in the specified file.
  • Use git log or other Git history visualization tools to find the appropriate commit hash or branch name.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github