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
434 questions
7624
votes
41 answers

How do I revert a Git repository to a previous commit?

How do I revert from my current state to a snapshot made on a certain commit? If I do git log, then I get the following output: $ git log commit a867b4af366350be2e7c21b8de9cc6504678a61b` Author: Me Date: Thu Nov 4 18:59:41 2010…
Crazy Serb
  • 76,496
  • 8
  • 33
  • 46
3568
votes
33 answers

Delete commits from a branch in Git

I would like to know how to delete a commit. By delete, I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch. I read git help, and I think the command I should use is git…
hap497
  • 129,793
  • 42
  • 77
  • 91
3387
votes
9 answers

Move existing, uncommitted work to a new branch in Git

I started some work on a new feature and after coding for a bit, I decided this feature should be on its own branch. How do I move the existing uncommitted changes to a new branch and reset my current one? I want to reset my current branch while…
Dane O'Connor
  • 67,996
  • 36
  • 114
  • 164
1334
votes
11 answers

Can I delete a git commit but keep the changes?

In one of my development branches, I made some changes to my codebase. Before I was able to complete the features I was working on, I had to switch my current branch to master to demo some features. But just using a "git checkout master" preserved…
tanookiben
  • 16,365
  • 7
  • 24
  • 25
1294
votes
18 answers

How can I undo git reset --hard HEAD~1?

Is it possible to undo the changes caused by the following command? If so, how? git reset --hard HEAD~1
Paul Wicks
  • 55,730
  • 52
  • 115
  • 144
1253
votes
13 answers

Why are there two ways to unstage a file in Git?

Sometimes git suggests git rm --cached to unstage a file, sometimes git reset HEAD file. When should I use which? EDIT: D:\code\gt2>git init Initialized empty Git repository in D:/code/gt2/.git/ D:\code\gt2>touch a D:\code\gt2>git status # On…
Senthess
  • 15,020
  • 5
  • 20
  • 28
1131
votes
14 answers

How to revert uncommitted changes including files and folders?

Is there a git command to revert all uncommitted changes in a working tree and index and to also remove newly created files and folders?
MEM
  • 27,721
  • 40
  • 115
  • 190
1082
votes
8 answers

How to uncommit my last commit in Git

How can I uncommit my last commit in git? Is it git reset --hard HEAD or git reset --hard HEAD^ ?
richard
  • 11,153
  • 3
  • 16
  • 9
1055
votes
2 answers

How do I use 'git reset --hard HEAD' to revert to a previous commit?

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: When I want to revert to a previous commit I use: git reset --hard HEAD And Git returns: HEAD is now at…
Brian McDonough
  • 11,931
  • 4
  • 16
  • 22
964
votes
14 answers

git undo all uncommitted or unsaved changes

I'm trying to undo all changes since my last commit. I tried git reset --hard and git reset --hard HEAD after viewing this post. I responds with head is now at 18c3773... but when I look at my local source all the files are still there. What am I…
Antarr Byrd
  • 20,852
  • 26
  • 85
  • 154
862
votes
4 answers

Undo a particular commit in Git that's been pushed to remote repos

What is the simplest way to undo a particular commit that is: not in the head or HEAD Has been pushed to the remote. Because if it is not the latest commit, git reset HEAD doesn't work. And because it has been pushed to a remote, git rebase…
Lakshman Prasad
  • 76,135
  • 46
  • 128
  • 164
807
votes
6 answers

How to cancel a local git commit

My issue is I have changed a file eg: README, added a new line 'this for my testing line' and saved the file, then I issued the following commands git status # On branch master # Changed but not updated: # (use "git add ..." to update what…
Amal Kumar S
  • 14,139
  • 19
  • 51
  • 83
471
votes
6 answers

What's the difference between "git reset" and "git checkout"?

I've always thought of git reset and git checkout as the same, in the sense that both bring the project back to a specific commit. However, I feel they can't be exactly the same, as that would be redundant. What is the actual difference between the…
prosseek
  • 155,475
  • 189
  • 518
  • 818
371
votes
6 answers

Git: can't undo local changes (error: path ... is unmerged)

I have following working tree state $ git status foo/bar.txt # On branch master # Unmerged paths: # (use "git reset HEAD ..." to unstage) # (use "git add/rm ..." as appropriate to mark resolution) # # deleted by us: …
mklhmnn
  • 3,930
  • 2
  • 14
  • 8
360
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
  • 188,810
  • 39
  • 339
  • 353
1
2 3
28 29