How do I use 'git reset --hard HEAD' to revert to a previous commit? [duplicate]
Asked Answered
I

2

1256

I know that Git tracks changes I make to my application, and holds on to them until I commit the changes.

To revert to a previous commit, I used:

$ git reset --hard HEAD

HEAD is now at 820f417 micro

How do I then revert the files on my hard drive back to that previous commit?

My next steps were:

git add .
git commit -m "revert"

But none of the files have changed on my hard drive...

Inspectorate answered 2/3, 2012 at 6:36 Comment(3)
What do you mean by reverting the files on my hard drive back to that previous commit? If 820f417 is your desired commit, the files should now have the exact content in that commit.Montelongo
If you want to undo all changes, after git reset --hard, you should git checkout <branch>.Inanition
I really don't get the idea of [duplicate] then ask a new question, when the answers are not satisfactory. It's a recipe for disaster in terms of more duplicates....Delamination
P
1443

First, it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git status is clean (that is, empty) before using it.

Initially you say the following:

So I know that Git tracks changes I make to my application, and it holds on to them until I commit the changes, but here's where I'm hung up:

That's incorrect. Git only records the state of the files when you stage them (with git add) or when you create a commit. Once you've created a commit which has your project files in a particular state, they're very safe, but until then Git's not really "tracking changes" to your files. (for example, even if you do git add to stage a new version of the file, that overwrites the previously staged version of that file in the staging area.)

In your question you then go on to ask the following:

When I want to revert to a previous commit I use: git reset --hard HEAD And git returns: HEAD is now at 820f417 micro

How do I then revert the files on my hard drive back to that previous commit?

If you do git reset --hard <SOME-COMMIT> then Git will:

  • Make your current branch (typically master) back to point at <SOME-COMMIT>.
  • Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT>.

HEAD points to your current branch (or current commit), so all that git reset --hard HEAD will do is to throw away any uncommitted changes you have.

So, suppose the good commit that you want to go back to is f414f31. (You can find that via git log or any history browser.) You then have a few different options depending on exactly what you want to do:

  • Change your current branch to point to the older commit instead. You could do that with git reset --hard f414f31. However, this is rewriting the history of your branch, so you should avoid it if you've shared this branch with anyone. Also, the commits you did after f414f31 will no longer be in the history of your master branch.
  • Create a new commit that represents exactly the same state of the project as f414f31, but just adds that on to the history, so you don't lose any history. You can do that using the steps suggested in this answer - something like:

    git reset --hard f414f31
    git reset --soft HEAD@{1}
    git commit -m "Reverting to the state of the project at f414f31"
    
Pelvic answered 2/3, 2012 at 8:32 Comment(14)
git reset --soft HEAD@{1} really messed up my local repository. It thinks all the files are now. What do I do now?Knighten
@Mark Longair: the answer you point to at the end does not user --hard, but instead simply git reset.Presbyterian
@MiniQuark: I'm using the equivalent and slightly shorter variant that I suggested in the comments on the linked answer.Pelvic
Thanks for the tip about git reset --soft HEAD@{1}. I haven't thought of that way of reverting commits, even though I'm experienced in git.Corposant
One can also consult git reflog in the event that git log does not immediately show the relevant commit you wish to revert to.Breuer
I don't understand. When I want to undo my last commit, I simply do git reset --hard HEAD This would undo my uncommit my commit ie it won't delete it, it will keep the code, but place it in a tracked state. Yet you said all it would do is throw away any uncommitted changes you have. Can you clarify this?Affirmative
Just to be clear, when you say "throws away all your uncommitted changes" this refers to editing or deleting existing files, or also to new untracked files? I'm not sure if adding a new file to a repository is an "uncommitted change", I suppose it might be.Ethos
Other safety way is to revert the last commit but preserving those changes locally. For that use: git reset HEAD^https://mcmap.net/q/11718/-can-i-delete-a-git-commit-but-keep-the-changesEadie
community.atlassian.com/t5/Sourcetree-questions/… $ git push --forceWonderwork
the arcane magic of these git commands is lost on me every time. I've resorted to doing a git reset --hard, copy the files to another dir, got a git pull and past then over. That seems to work best even though it might not be clean.Cressy
Can I use git status --porcelain to check is the repo is clean?Mayenne
Also, the commits you did after f414f31 will no longer be in the history of your master branch. This solved my confusionMouthwash
Also, the commits you did after f414f31 will no longer be in the history of your master branch. - by master branch it means the origin branch of you current branch, right? If I am making git reset on feature-branch which tracks origin feature-branch, it means the the commits after f141f31 will no longer be in the history of feature-branch branch (and master branch in such case won't be affected, even after push to feature-branch), correct?Giguere
Why is nobody talking about git revert?! Please use 'git revert' instead of 'git reset'. Git revert will make a new commit so you're changes are always kept in the history and it's clear later on, what you did. Also you're code never gets lost.Kalila
L
307

WARNING: git clean -f will remove untracked files, meaning they're gone for good since they aren't stored in the repository. Make sure you really want to remove all untracked files before doing this.


Try this and see git clean -f.

git reset --hard will not remove untracked files, where as git-clean will remove any files from the tracked root directory that are not under Git tracking.

Alternatively, you can do the following (beware though - that removes all ignored files too)

  • git clean -df
  • git clean -xdf CAUTION! This will also delete ignored files

Explanation of Flags:

-d deletes all files in directories recursively

-f

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.

-x Don't use standard ignore rules but ones specified by -e. This can be used to start a clean build.

Source: Man pages

Lillalillard answered 2/3, 2012 at 6:48 Comment(15)
This is what I get from terminal: Not removing app/views/relationships/ Not removing tmp/sessions/ Not removing tmp/sockets/ No change. Hmm... everything I do seems to not work. Not your fault.Inspectorate
After much trial and mostly errors, I ended up creating a new app and loading all the files in manually from a backup. I make a lot of backups, but I really need to learn to rely on git, which is to say I need to learn to use git better.Inspectorate
@BrianMcDonough - all too common a statement. I make so many mistakes with git only because I don't understand it, and the time it needs to ramp up is beyond what I can afford. I mostly use it as close to SVN as I can with a lot of personal backups. God forbid a git expert gets on my computer to 'fix that mess'.Squib
This is the first git command I've come across so far that's irreversible... added a warning so others don't get too trigger happy...Deadradeadweight
Use n instead of f first, to see a list of what will be removed, e.g: git clean -xdnTetradymite
remember to be careful with clean because git clean -f will remove any environment/gui project folders (e.g. .idea (phpstorm) or .vagrant (vagrant))Litch
Goddamnit, git clean -xdf even removes all ignored files!!!Roesch
Remember to do npm install afterwards, as git clean -xdf removes ignored files ie. any node_modules. That's why everything looks broken after running the command :)Kieger
git clean -df is enough. The -x flag ignores the .gitignore file.Neuropath
The question mentions git-clean but means git clean. Due to poor SO design decisions I can't fix it, but perhaps the answerer can.Slavey
@user1406043 the man pages (and some shells) support both. You can say man git-clean and the shell will display the documentation for the command git cleanToomey
It should be considered to move this answer since it is completely off topic.Differ
warning... read before running above command, My files are gone after running this command.Maibach
git's version of rm -rf /Amrita
Finally, a command that actually cleans things!Overkill

© 2022 - 2024 — McMap. All rights reserved.