Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "reset" command for a git cloned repository?

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 ?

like image 293
axis Avatar asked Oct 01 '12 07:10

axis


People also ask

What is reset command in git?

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.


2 Answers

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:

  1. (Optional if git status says no modified files) Discard any modified files on the disk (that's why reset --hard)

  2. Checkout the remote master branch (note: you will be in a “detatched head” state)

  3. Delete the local master branch (throwing away all your local changes)

  4. 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

like image 146
Stephen Connolly Avatar answered Oct 23 '22 12:10

Stephen Connolly


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

  • your upstream repository is called "origin" (the default for git clone)
  • you are working in a clone of the remote master branch

If you are working in a branch other than "master" , adjust the command:

git reset --hard origin/branch1
like image 35
sleske Avatar answered Oct 23 '22 12:10

sleske