I have downloaded a repository with
git clone <address>
i have made some modification and have edited some files, now i want to discard everything and just be sure that what i have on the disk is nothing else than the original remote version of the codebase: what is the right command ?
Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.
Here is the long explanation so that you understand what is going on:
Assuming the remote is called origin
git reset --hard HEAD
git checkout origin/master
git branch -D master
git checkout -b master
What this does is:
(Optional if git status says no modified files) Discard any modified files on the disk (that's why reset --hard
)
Checkout the remote master branch (note: you will be in a “detatched head” state)
Delete the local master
branch (throwing away all your local changes)
Call the current head as the new master
branch
Now you probably want to do something slightly different... i.e. don't throw away your changes , just put them on another named branch... after all you never know when you'll need them again
git checkout -b my-silly-changes
git branch -D master
git checkout -b master origin/master
That saves your current changes in a new local branch called my-silly-changes
and then removes the old local branch called master
and finally recreates it from the remote head.
And here is the explanation for people who think they know what they are doing:
The single command:
git reset --hard origin/master
Will discard any local changes and re-point the current branch to the most recently fetched state of origin/master
. This has the exact same effect as the four commands at the start of this answer, but without looking behind the curtain
If you are certain you want to discard all files, and lose all your modifications (!), you can do:
git reset --hard origin/master
This will reset the state of your local copy of the "master" branch and of your working copy to the last version that you fetched from your upstream (original remote).
Note: This assumes that
git clone
)master
branchIf you are working in a branch other than "master" , adjust the command:
git reset --hard origin/branch1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With