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
23
votes
3 answers

git reset vs git reset HEAD

Every time a file has been staged, Git offers helpful instructions in the event you needed to unstage a file: (use "git reset HEAD ..." to unstage) However the decent Git Tutorials by Atlassian simply say: git reset This seems more…
skube
  • 5,867
  • 9
  • 53
  • 77
23
votes
5 answers

Reverting to a specific commit without losing history

I know this type of a question has a lot duplicates, but I wanted to open a new one because I didn't found in all of the other questions the explaination of the best way to do it as I want. I know i can revert and keep the history by doing: git…
Aviv Paz
  • 1,051
  • 3
  • 13
  • 28
23
votes
2 answers

How to go back to the last commit in the history after I used git reset to go to an older changeset?

Suppose my history goes that way : A - B - C - D (master) If I do git reset B, I'll got : A - B (master) Trouble is, git log now show me only the history from A to B, and I can't see C and D anymore. How can I go back to D ?
Bite code
  • 578,959
  • 113
  • 301
  • 329
23
votes
2 answers

Git reset and checkout by single command

Consider I've staged for committing a file (e.g. db/schema.rb) that I didn't intended to change. I need to do: git reset db/schema.rb git checkout db/schema.rb Can I do it by single command?
Alexey
  • 9,197
  • 5
  • 64
  • 76
21
votes
1 answer

What's the difference between 'git rm --cached', 'git restore --staged', and 'git reset'

I have come across the following three ways in order to unstage the files that were staged by the command 'git add' git rm --cached git restore --staged git reset Their behaviors looked completely same when I ran those commands…
sekai_no_suda
  • 413
  • 4
  • 11
20
votes
4 answers

What happens to orphaned commits?

I have a repo with four commits: $ git log --oneline --decorate 6c35831 (HEAD, master) C4 974073b C3 e27b22c C2 9f2d694 C1 I reset -- soft to the C2 commit and now I have a repo like so: $ git reset e27b22c --soft $ git log --oneline…
BanksySan
  • 27,362
  • 33
  • 117
  • 216
20
votes
2 answers

git reset --merge vs git reset --keep

I have read the documentation, however I am having a hard time understanding the difference between git reset --merge And git reset --keep Please provide a simple explaination and/or example.
Zombo
  • 1
  • 62
  • 391
  • 407
19
votes
4 answers

What are typical use cases of git-reset's --merge and --keep flags?

In a recent answer in which he details the typical use cases of git-reset's three most commonly used options (--hard, --mixed, and --soft), torek mentions in passing that git-reset also offers two relatively esoteric flags, called --merge and…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
17
votes
1 answer

egit replacing single file - HEAD Revision vs File in git index

In the egit git eclipse plugin, I know I can "reset" a single file by right clicking on it and selecting Replace With > HEAD Revision. What is the difference between replacing with HEAD revision vs File in git index?
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
16
votes
5 answers

Remove other peoples' commits on my branch after rebase gone wrong

I got myself into some git-funk here. I need to git-fu myself out of this. I joined a new team and created a feature branch: git checkout -b feature_branch Made some changes and then committed/pushed them up to the branch. git commit -am…
bob_cobb
  • 2,229
  • 11
  • 49
  • 109
16
votes
1 answer

Undo a git reset --hard origin/master

Working on local master branch: git commit -m "Lots of important commits" git reset --hard origin/master How can I retrieve the commits that have been lost as a result of the git reset (from the remote)? EDIT: note this is not about retrieving…
chrisjleu
  • 4,329
  • 7
  • 42
  • 55
16
votes
3 answers

How can I move commits from the trunk to a branch in Git?

I made a bunch of commits to the master and realized after the fact that they should have been in a branch. I've looked at various things about rebasing and merging and resetting the master. But no attempts at manipulation have yielded a history…
Danny
  • 13,194
  • 4
  • 31
  • 36
16
votes
2 answers

What does the double-dash [--] option do on git reset?

I've seen commands like: git reset e542 -- readme.txt I understand this command puts in the index the contents of the file readme.txt from commit e542. But what's the -- option doing there? The git reset man page lists it as optional for the first…
AngraX
  • 1,803
  • 2
  • 18
  • 30
15
votes
3 answers

How can I reset a Git repository to its newly-cloned state?

Is there a git statement that has the same result as a fresh git clone? So even if there are extra branches, extra files, other files, local tags anything... which command can accomplish this?
byenary
  • 301
  • 2
  • 3
  • 7
13
votes
6 answers

Can't reset a file to a specific commit using Git

I have a modified file which I want to rever to whatever is in the latest commit but it's "stuck" there always being marked as modified. $ git status # On branch master # Changed but not updated: # (use "git add ..." to update what will be…
Julian
  • 8,808
  • 8
  • 51
  • 90