axis
axis

Reputation: 872

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 ?

Upvotes: 17

Views: 21945

Answers (3)

Stephen Connolly
Stephen Connolly

Reputation: 14116

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

Upvotes: 26

Ikke
Ikke

Reputation: 101261

If you haven't committed anything, then git reset --hard is just enough.

Here is a good resource explaining how git reset with its different modes work (--hard, --mixed and --soft).

Disclaimer:

git reset --hard is an unsafe operation. It removes any uncomnitted changes from the working tree, and cannot be undone.

Upvotes: 0

sleske
sleske

Reputation: 83645

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

Upvotes: 1

Related Questions