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
13
votes
2 answers

Recover files that were added to the index but then removed by a git reset

I added some files to the index but then by mistake I deleted them with git reset --hard. How do I recover them? Here's what happened: I added all files using git add . I then committed When I checked the status, there were still files that…
Jeff Eisley
  • 193
  • 1
  • 2
  • 10
12
votes
2 answers

Git: Undo local changes; git add . + git rm?

Need help figuring out a couple common workflows with Github. I come from a VS TFS background, so forgive me. Undoing Pending Changes Let's say I have cloned of a git repository to my local file system. At this point, the project's local files match…
user979672
  • 1,803
  • 3
  • 23
  • 32
12
votes
2 answers

Git reset all files with particular extension

I have changes in many types of file like .tsx .scss and .scss.d.ts, and have committed and pushed to my branch. Is there any way I can reset only extension .scss.d.ts with master ? Keep the changes in .tsx and .scss only reset .scss.d.ts with…
ankitd
  • 1,967
  • 3
  • 26
  • 44
11
votes
6 answers

Git cannot undo modified files

I just want to get back to a clean working directory, exactly as it was after my last commit. Git is reporting to me a load of file modifications that I haven't made, so I suspect it's something to do with line endings. I have tried all the usual…
WickyNilliams
  • 5,218
  • 2
  • 31
  • 43
10
votes
1 answer

What is the difference between "git checkout -- ." and "git reset HEAD --hard"?

This is not a general question about what '--' does, as in the marked duplicate. This is a git-specific question asking for clarity on what the operational differences are between the mentioned commands. If I want to clean out my current directory…
M_M
  • 1,955
  • 2
  • 17
  • 23
10
votes
2 answers

Difference between git reset --hard and git checkout

Let's say I want to undo all changes introduced by previous commits. As far as I understand, git reset --hard will remove all commits up until the specified commit, and undo all changes. On the other hand git checkout
Atlas23250
  • 177
  • 2
  • 9
10
votes
3 answers

revert the git last commit and save it in a different branch

Is there a way to rollback the last commit and put it into a separate branch for later testing? I did some changes which I don't want to entirely throw away, I just want to keep them aside in a different branch for further testing. Can anyone help…
Mo J. Mughrabi
  • 6,747
  • 16
  • 85
  • 143
10
votes
1 answer

Right way to discard changes in Git

I've seen many different approaches for discarding changes / reverting to a previous commit using Git. I usually can figure out which works for my situation, but in the process I've gotten quite confused by the different approaches. Most recently I…
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
9
votes
3 answers

Git: What happens to a branch after a master reset

Assume I have a master branch with some commits I want to keep for later documentation and reference but remove them from the master (because I want the master to be on the same state as the upstream repo). My approach now is, to Create a new…
9
votes
3 answers

Can't discard changes in git

A week or two ago I took some files that I had been archiving with a simple find |sed|tar|xz|gpg bash script, unpacked them all, and put their contents in a git repo, commited, put the next archives content in the repo, committed (rinse and repeat)…
Nero gris
  • 562
  • 7
  • 15
9
votes
1 answer

Is "git reset --hard" the same as "git reset --hard HEAD"?

In order to revert changes in a working tree and index, this answer (https://stackoverflow.com/a/5812972/8278160) suggests that one run the following: git reset --hard Would running this be the same as running git reset --hard HEAD?
Gokhan
  • 307
  • 3
  • 9
9
votes
2 answers

What's the difference between `git reset --hard master` and `git reset --hard origin/master`?

I tried a lot of links on Stackoverflow/elsewhere to properly understand behaviour of git reset --hard option I know that: If it is omitted or if it is origin, reset is done on most recent commit on origin If a SHA1 hash is provided, reset is done…
vashishatashu
  • 7,720
  • 7
  • 29
  • 34
9
votes
4 answers

Git reset not working

I made a commit, pulled and merged some changes, and then made a second commit. When I wanted to go back to the first commit, I ran the command git reset --hard While the response was "HEAD is now at ", my code looks just as…
Eric Baldwin
  • 3,341
  • 10
  • 31
  • 71
8
votes
3 answers

How to completely remove a commit from gitlab?

I made a commit in my git repo and pushed it, but accidentally it contained some passwords for our production machines. So I deleted the commit: git reset --hard HEAD~1 git push --force That indeed removed the commit from the list of commits, but…
kramer65
  • 50,427
  • 120
  • 308
  • 488
8
votes
2 answers

`git reset --hard` also reset ignored files?

I have some files that was once in the git repository but later ignored. (mainly configuration files) However, when I run git reset --hard, the changes on these ignore files are also reset, where I am sure the head version has already got those…
cytsunny
  • 4,838
  • 15
  • 62
  • 129