How to revert to origin's master branch's version of file
GitGit Problem Overview
I'm in my local computer's master branch of a cloned master-branch of a repo from a remote server.
I updated a file, and I want to revert back to the original version from the remote master branch.
How can I do this?
Git Solutions
Solution 1 - Git
Assuming you did not commit the file, or add it to the index, then:
git checkout -- filename
Assuming you added it to the index, but did not commit it, then:
git reset HEAD filename
git checkout -- filename
Assuming you did commit it, then:
git checkout origin/master filename
Assuming you want to blow away all commits from your branch (VERY DESTRUCTIVE):
git reset --hard origin/master
Solution 2 - Git
I've faced same problem and came across to this thread but my problem was with upstream
. Below git command worked for me.
Syntax
git checkout {remoteName}/{branch} -- {../path/file.js}
Example
git checkout upstream/develop -- public/js/index.js
Solution 3 - Git
Can also be done using git restore
git restore --source origin/master filename
Solution 4 - Git
For the sake of adding an alternative (not necessarily a better one, of course), if you have already committed the file before, but now you need to undo the change, here's what you can do:
git diff HEAD..master -- path/to/file.ext | git apply -
This generates a diff to restore the file to the version in the master
branch, and then applies it. The minus after git apply
tells git
to read the patch from standard input.
You can then commit the file as usual.
Here is the same command expressed as a shell function:
# Git Reset File
function grf() {
git diff HEAD..master -- $1 | git apply -
}
# for example: grf ./someChangedFile.txt
Solution 5 - Git
you need to find the latest commit id and the directory of the file you want to revert.
then using the following commands
git checkout [commit ID] -- path/to/file
git commit -m 'commit message'
will help you to revert the file you want to latest version of that file on remote computer.
Solution 6 - Git
If you didn't commit it to the master branch yet, its easy:
- get off the master branch (like
git checkout -b oops/fluke/dang
) - commit your changes there (like
git add -u; git commit;
) - go back the master branch (like
git checkout master
)
Your changes will be saved in branch oops/fluke/dang; master will be as it was.