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

Undo git reset --hard with uncommitted files in the staging area

I am trying to recover my work. I stupidly did git reset --hard, but before that I've done only get add . and didn't do git commit. Please help! Here is my log: MacBookPro:api user$ git status # On branch master # Changes to be committed: # (use…
eistrati
  • 2,314
  • 6
  • 26
  • 35
100
votes
3 answers

I need to pop up and trash away a "middle" commit in my master branch. How can I do it?

For example, in the following master branch, I need to trash just the commit af5c7bf16e6f04321f966b4231371b21475bc4da, which is the second due to previous rebase: commit 60b413512e616997c8b929012cf9ca56bf5c9113 Author: Luca G. Soave…
Luca G. Soave
  • 12,271
  • 12
  • 58
  • 109
91
votes
3 answers

How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?

We have all heard that one should never rebase published work, that it’s dangerous, etc. However, I have not seen any recipes posted for how to deal with the situation in case a rebase is published. Now, do note that this is only really feasible if…
Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
69
votes
4 answers

How do I move master back several commits in git?

I have a git repository which holds a Drupal site. I spent the last day trying to build a feature using several different modules. I have given up on my current approach and have decided to try a different combination of modules. However, my…
Hoytman
  • 1,722
  • 2
  • 17
  • 29
68
votes
1 answer

git rm --cached file vs git reset file

I'm trying to learn Git. I'm confused between git rm --cached file and git reset file both of the commands seem to take the file from staged to un-staged area. How do the commands differ?
Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
62
votes
1 answer

Move commits from master onto a branch using git

I'm trying to learn how to use Git effectively and I'm wondering how I should (good practice/bad practice?) solve the following case: Say I have the following chain of commits in master: Initial commit Commit 1 Commit 2 Commit 3 Then I realize…
Paul
  • 4,239
  • 6
  • 28
  • 29
48
votes
1 answer

How to undo git reset --soft to get my changes back?

I had some local changes in my directory that I committed using git commit. Later I realized that by mistake my changes broke my build. So I did a git reset --soft on my repo. But I screwed up, as I did not back up the changes that were all undone,…
dennis
  • 680
  • 1
  • 5
  • 11
47
votes
3 answers

Is there a difference between "git reset --hard hash" and "git checkout hash"?

While reset and checkout have different usages most of the time, I can't see what difference there is between these two. There probably is one or nobody would have bothered adding a --hard option to do something the basic checkout can do. Maybe…
Bite code
  • 578,959
  • 113
  • 301
  • 329
43
votes
3 answers

Atom editor does not reload changed file

I am using Atom editor on Windows 7. On the git shell when I run the command, git reset --hard , all the other editors reset/undo all the modified files. but if I am using ATOM editor, changes are retained. If I try to close the file in editor,…
SharpCoder
  • 18,279
  • 43
  • 153
  • 249
41
votes
5 answers

Reset/revert a whole branch to another branches state?

I have a branch A and a branch B (and some other branches). Lets say A's commit history looks like: commit 5 commit 4 commit 3 ... And B's commit history: some other commit commit 4 merge of other stuff from branch C (into branch B) commit…
daniel451
  • 10,626
  • 19
  • 67
  • 125
34
votes
11 answers

Is it still possible to restore deleted untracked files in git?

let's say yesterday I did some changes on my master branch, and I forgot to add, commit them. and in the morning i did git reset --hard is it possible to restore deleted files in this situation ?
Said Kaldybaev
  • 9,380
  • 8
  • 36
  • 53
31
votes
1 answer

Difference between git reset --hard and git clean

Hi I am curious about the difference between these two commands. When they introduce here: https://www.atlassian.com/git/tutorials/undoing-changes Looks like git reset --hard also sets both the staging and the working directory to match the latest…
AlexWang
  • 369
  • 1
  • 4
  • 7
28
votes
3 answers

Reset without losing already committed files

When I accidentally committed a file to the wrong branch, I normally use git reset --hard HEAD~1. However, using this method I generally lose all the files committed. Is there a way to reset a commit, without losing the edited files?
cherrun
  • 2,102
  • 8
  • 34
  • 51
27
votes
2 answers

git: Your branch and 'origin/somebranch' have diverged - how to throw away local commits

I have a question that most closely matches my problem was made, but could not add a comment. git: Your branch and 'origin/master' have diverged - how to throw away local commits? I ran the tips and answers to the question above, but nothing…
Paulo Victor
  • 327
  • 1
  • 4
  • 12
23
votes
1 answer

Revert only a single file of a pushed commit

Below is the the pushed commits history. Commit Changed Files Commit 1| (File a, File b, File c) Commit 2| (File a, File b, File c) Commit 3| (File a, File b, File c) Commit 4| (File a, File b, File c) Commit 5| (File a, File b, File c) I…
Aki T
  • 612
  • 2
  • 7
  • 17
1 2
3
33 34