1

So the situation is I cloned a repo from Github and I messed up trying to build it and it added new files and made changes to existing files in the repo. I have no commits and never staged anything. How do I revert to the clean master branch (meaning the actual local files on my PC) of the clone ?

If I wasn't using git commands I would delete the local files from disk and just re-download/clone the repo. However I feel there should be an easy way to do this using Git. I never messed with my Git settings, so git status still lists all untracked files and lists modifications, but I don't want to manually checkout every file one by one and then manually delete each untracked file one by one.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1086516
  • 887
  • 3
  • 9
  • 21
  • 1
    Related: [git reset --hard HEAD leaves untracked files behind](http://stackoverflow.com/questions/4327708/git-reset-hard-head-leaves-untracked-files-behind) – Ajedi32 Nov 01 '13 at 21:20
  • Yes, that's why you need to use the clean command too. – user1086516 Nov 01 '13 at 21:25

4 Answers4

5
git reset --hard # This resets all tracked files to default state
git clean -fd    # this clears working directory of all untracked files

It may be obvious, but please use this commands with caution. They are destructive and will delete your work without asking.

git reset resets current HEAD to default state. The --hard specifies the hard mode, which deletes all the changes. You can also use it with --soft mode, which just resets everything else to specified commit, but keeps all your changes, and --mixed mode, which makes all your changes unstaged. However, this command only works on the tracked files.

git clean cleans the folder of untracked files. -d option makes it work on directories as well as files (please note that git only tracks files — it doesn't that notice folders exist unless you put something in them), and -f option is a simple fuse — it wont work without it (unless your repo is configured otherwise). However, this command still ignores files specified in .gitignore, unless you add -x flag.

Zombo
  • 1
  • 62
  • 391
  • 407
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
0
git reset --hard HEAD

HEAD -> refers to the commit that you started with, before you started making changes (as Simon points out in the comments, it is the last commit that was checked out).

reset --hard -> changes the files in your working directory to match that commit, disregarding any changes that are presently there

You can also use reset --hard to move to different commits, tags, or branches by doing git reset --hard branch-name

Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
0

Pretty simply: git reset --hard

You may need to delete added file manually.

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
0

to remove all local changes

git checkout -- .

if you have added files you want to remove too

git clean

I suggest using githug to start learning git, it makes learning more of a game

https://github.com/Gazler/githug

exussum
  • 18,275
  • 8
  • 32
  • 65