Git stay on same commit but revert files

git revert
git revert commit
git reset head
git undo commit but keep work
git reset --soft
git undo pushed commit keep changes
git unstage file
git revert commit keep changes

I want to get reset files to previous commit but staying on the current commit. So if I do:

git init
touch 1; git add 1; git commit -m "1"
touch 2; git add 2; git commit -m "2"
touch 3; git add 3; git commit -m "3"

This will produce 3 commits. I want to stay on commit #3 but reset the stage to the state I had on commit #2. I tried to following:

  • git reset --(hard|soft|mixed|merge|keep)
  • git checkout
  • git revert

So after some magic command blah I want the following result:

  • git log shows that the HEAD is still on commit #3
  • ls . shows that I have only files 1 and 2 on my filesystem.
  • git diff or git diff --cached shows that file 3 was removed.

I can do git reset --hard <hash-2>, but this will move the HEAD to commit #2.

git revert HEAD -n

With -n or --no-commit it reverts the changes in the working tree and the index but does not create a commit. HEAD can be replaced with other equivalent commit-ish. If you want to commit the reverted, you need to run git commit.

If you want to revert some successive commits, taking the case in your comment for example,

git revert HEAD~3..HEAD -n

Revert last commit but keep all the changes to the files with git reset, Same time you don't want to lose anything you have done in that commit. For this purpose we use git reset --soft HEAD~1 or git reset --soft paste-� The git revert will leave three files on the file system -- alpha.html, beta.html and charlie.html -- and will roll back to the state at which the third commit occurred. The git revert will leave us with four files and remove only charlie.html, the only file associated exclusively with the third commit.

Use this to revert the specific files to a given commit. Your HEAD will stay the same.

git checkout <commit-hash> -- file1/to/restore

Undoing Things, Here, we'll review a few basic tools for undoing changes that you've made. The same commit-message editor fires up, but it already contains the What if you realize that you don't want to keep your changes to the CONTRIBUTING.md file? Git – Revert File to Previous Commit. Sometimes it happens that you make some changes to a file but later realize that it was a mistake and these changes have to be discarded. In Git you can revert the changes made to a file if you haven’t committed them yet, as well as you can revert a file to any previous commit.

Git examples: Resetting, Undoing and Reverting Changes, Restore a file to the way it was on a given commit. This will open up the previous commit message in case you want to edit it or keep the same message To undo adding a file to a commit (but keep it tracked) use $ git reset� A revert is an operation that takes a specified commit and creates a new commit which inverses the specified commit. git revertcan only be run at a commit level scope and has no file level functionality. A reset is an operation that takes a specified commit and resets the "three trees" to match the state of the repository at that specified commit.

You can put any tree-ish (here: that of the previous commit) into the current index like this:

git reset @~ -- .

which gives you

> git status --short
D  3
?? 3

Resetting, Checking Out & Reverting, Compare the most common configurations of git reset, git checkout, and git revert. Keep these mechanisms in mind as you read through this article. The git checkout command can be used in a commit, or file level scope. This does the exact same thing as checking out a branch: it moves the HEAD reference to the� First you need to got to the shared repository on GitHub and find the file that you want to revert. Once you navigate to the file on GitHub that you want revert, right above the file you should see this: On the right hand side you can see a 7 digit commit ID and a date.

How do I undo things in Git?, How do I revert changes introduced by a want to reset to a previous commit, but still have your current changes saved as they are, use the --keep option instead. Using the --hard option, everything is reverted back to the specified commit. This includes the commit history reference pointers, the staging index, and your working directory. This means that by using just this command you'll not only revert to a previous commit, but you'll lose all working changes in the process.

Git: Revert to a Previous Commit, Whether you accidentally commit changes, or just realized your previous This option works much the same way as git reset --hard <hash-or-ref> , but it only So if you have uncommitted changes that you want to keep, then this is likely the � Revert last commit but keep all the changes to the files with git reset --soft HEAD~1 You just want to go one commit back and complete your uncomplete work. Same time you don't want to lose anything you have done in that commit. For this purpose we use git reset --soft HEAD~1 or git reset --soft paste-commit-id-here command.

Introduction to undoing things in git, If you have modified, added and committed changes to a file, and added to commit but untracked files present (use "git add" to track) want to keep track of, you can add that file to a .gitignore file. Worth warning that this will open a confirmation file in vi (assuming you're using the default git editor) for each commit being reverted. If everything looks as expected, type ":wq" to confirm the reverts. You can also add the --no-commit flag to stage each revert without actually creating a revert commit. This is useful when reverting several

Comments
  • What exactly do you mean by "stay on commit #3"? If you reset to commit 2, then you're not on commit 3 anymore, but working towards commit 4 which undoes what was done in commit 3.
  • @CodeCaster I don't want to move the HEAD. If I type git log I wanna see commit #3 on top.
  • That makes little sense to me. Explain your scenario, not your desired end results.
  • I explained the scenario above, I want to continue working with changes I had a few commits ago in my stage. I don't wanna produce new commits or move the head. I want to modify those changes and only after that produce a new commit.
  • So you want to branch off at commit 2, do some other changes, then replay the changes of commit 3 onto those changes? You actually want to "insert" a commit between 2 and 3? That's what I meant by explaining a scenario, not a result.
  • This only reverts single commit. In scenario I have 10 commits, and I want to reset my stage to commit #7 I have to do git revert #10 git revert #9 git revert #8. Thanks anyway I didn't know about --no-commit argument.
  • I saw that answer, but I have no idea what files I had on this commit. Furthermore I had no idea which files were added or removed
  • Yes, this is the way to go. And to restore all files to that commit's state, just git checkout <commitHash> -- .
  • This doesn't remove the files I have now. If I do git checkout hash-commit-2 I would still have files 1-2-3 on my filesystem. I want to fully reset my stage to the commit I had before. The same result I can archive with git reset --hard but this command will move the HEAD
  • What would you do git checkout hash-commit-2 although nobody suggested it? Did you actually try git checkout hash-commit-2 -- .?
  • This one is kinda tricky. It doesn't modify the files. It leaves files the same. But stages the changes were made in commits. So git diff --cached and git diff show opposite changes. E.g. 1) +a and 2) -a