Questions tagged [git-reset]

Sets the current Git repo head to a specified commit and optionally resets the index and working tree to match.

If you've made a mistake in the current working tree, and haven't committed, you can revert the entire working tree to the last commit state with the following:

$ git reset --hard HEAD

Synopsis

git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
git reset [-q] [<commit>] [--] <paths>...
git reset --patch [<commit>] [--] [<paths>...]

Options

--mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

--soft
Does not touch the index file nor the working tree at all, but requires them to be in a good order. This leaves all your changed files "Changes to be committed", as git status would put it.

--hard
Matches the working tree and index to that of the tree being switched to. Any changes to tracked files in the working tree since <commit> are lost.

--merge
Resets the index to match the tree recorded by the named commit, and updates the files that are different between the named commit and the current commit in the working tree.

--keep
Reset the index to the given commit, keeping local changes in the working tree since the current commit, while updating working tree files without local changes to what appears in the given commit. If a file that is different between the current commit and the given commit has local changes, reset is aborted.

-p, --patch
Interactively select hunks in the difference between the index and <commit> (defaults to HEAD). The chosen hunks are applied in reverse to the index. This means that git reset -p is the opposite of git add -p (see ).

-q, --quiet
Be quiet, only report errors.

<commit>
Commit to make the current HEAD. If not given defaults to HEAD.

See also

Reference

$ git reset --help
509 questions
430
votes
3 answers

Reset all changes after last commit in git

How can I undo every change made to my directory after the last commit, including deleting added files, resetting modified files, and adding back deleted files?
Dogbert
  • 212,659
  • 41
  • 396
  • 397
403
votes
22 answers

Unstaged changes left after git reset --hard

After git reset --hard, git status gives me files within the Changes not staged for commit: section. I've also tried git reset ., git checkout -- . and git checkout-index -f -a, to no avail. So, how can I get rid of those unstaged changes? This…
Norswap
  • 11,740
  • 12
  • 47
  • 60
380
votes
7 answers

What's the difference between Git Revert, Checkout and Reset?

I am trying to learn how to restore or rollback files and projects to a prior state, and don't understand the difference between git revert, checkout, and reset. Why are there 3 different commands for seemingly the same purpose, and when should…
haziz
  • 12,994
  • 16
  • 54
  • 75
371
votes
4 answers

What is the meaning of git reset --hard origin/master?

I did a git pull and got an error: The following working tree files would be overwritten by merge... Please move or remove them before you can merge. To resolve this I did the following: git fetch git reset --hard origin/master Now when I do…
jimcgh
  • 5,637
  • 4
  • 27
  • 37
336
votes
6 answers

Git, How to reset origin/master to a commit?

I reset my local master to a commit by this command: git reset --hard e3f1e37 when I enter $ git status command, terminal says: # On branch master # Your branch is behind 'origin/master' by 7 commits, and can be fast-forwarded. # (use "git pull"…
Hesam
  • 52,260
  • 74
  • 224
  • 365
329
votes
6 answers

How to discard local commits in Git?

I'd been working on something, and decided it was completely screwed...after having committed some of it. So I tried the following sequence: git reset --hard git rebase origin git fetch git pull git checkout At which point I got the message Your…
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
308
votes
8 answers

How can I move HEAD back to a previous location? (Detached head) & Undo commits

In Git, I was trying to do a squash commit by merging in another branch and then resetting HEAD to the previous place via: git reset origin/master But I need to step out of this. How can I move HEAD back to the previous location? I have the SHA-1…
timpone
  • 19,235
  • 36
  • 121
  • 211
293
votes
10 answers

How to git reset --hard a subdirectory

UPDATE²: With Git 2.23 (August 2019), there's a new command git restore that does this, see the accepted answer. UPDATE: This will work more intuitively as of Git 1.8.3, see my own answer. Imagine the following use case: I want to get rid of all…
krlmlr
  • 25,056
  • 14
  • 120
  • 217
267
votes
2 answers

How to undo the last commit in git

By mistake, I did git add . and git commit in the develop branch. But luckily, I did not do git push. So I wanted to revert it back to original state. I tried git reset --soft and git reset HEAD --hard but looks like I have messed it up. How do I…
chintan s
  • 6,170
  • 16
  • 53
  • 86
238
votes
5 answers

Undo “git add ”?

I mistakenly added files using the command "git add dir". I have not yet run "git commit". Is there a way to remove this dir and everything contained within it from the commit? I have tried git reset dir, but it didn't work. Apparently git reset…
neoneye
  • 50,398
  • 25
  • 166
  • 151
224
votes
4 answers

What is the `git restore` command and what is the difference between `git restore` and `git reset`?

When I want to unstage a staged file, all of my Git tutorials show something like: $ git add * $ git status On branch master Changes to be committed: (use "git reset HEAD ..." to unstage) renamed: README.md -> README modified: …
Thinh NV
  • 3,182
  • 3
  • 18
  • 17
190
votes
8 answers

Why git can't do hard/soft resets by path?

$ git reset -- can reset by path. However, $ git reset (--hard|--soft) will report an error like below: Cannot do hard|soft reset with paths.
yao
  • 1,993
  • 2
  • 12
  • 10
181
votes
3 answers

"git rm --cached x" vs "git reset head --​ x"?

GitRef.org - Basic: git rm will remove entries from the staging area. This is a bit different from git reset HEAD which "unstages" files. By "unstage" I mean it reverts the staging area to what was there before we started modifying…
Pacerier
  • 86,231
  • 106
  • 366
  • 634
150
votes
4 answers

Git: How to reuse/retain commit messages after 'git reset'?

As Git user I regular come across the situation, that I need to rework one or more commits in a way which do not fit into --amend or rebase -iwith fixup commits. Typically I would do something like git reset HEAD~1 # hack, fix, hack git commit -a #…
bentolor
  • 3,126
  • 2
  • 22
  • 33
144
votes
5 answers

What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'?

I tried to undo my commit in git. Is it dangerous to use git reset --hard HEAD~1? What is the difference between different options for git reset?
mesutali
  • 1,655
  • 2
  • 12
  • 9
1
2
3
33 34