Git Examples: Reverting a File from a Branch
Last updated:Table of Contents
- Revert file from branch
- Revert file from remote branch (using git log and checkout)
- Revert file from remote branch (using fetch and checkout)
Git version 2.x used
Revert file from branch
Use git checkout
to retrieve a file from my-other-branch
$ git checkout my-other-branch -- path/to/file
Revert file from remote branch (using git log and checkout)
Retrieve the last commit hash of the remote branch
$ git log origin/main commit 123456abcde (origin/main, origin/HEAD, main) Author: John Doe <john-doe@example.com> Date: Mon Jul 3 12:44:25 2023 -0300 some commit message
Revert file to that commit hash with
git checkout
(like Restore file from Previous commit)$ git checkout 123456abcde -- path/to/your/file
Revert file from remote branch (using fetch and checkout)
Use git fetch to make sure your local branches are up to date
$ git fetch --all
Then just use checkout to reset the file to
origin/my-other-branch
$ git checkout origin/my-other-branch -- path/to/your/file