How do I restore a previous version as a new commit in Git?

git reset
git revert commit
git revert to previous commit and push
git move head to previous commit
git checkout previous commit
git revert file to previous commit
git checkout <commit
git reset branch

First, I have seen the answer to this question here before, but it is buried by so many "answers" which don't answer my question properly, that I cannot find it again.

So here it goes: How do I restore in Git to a previous version in my history, so that it becomes the new commit on top of my current history?

Basically, this is the most fundamental thing I would like to do with a version control system. Simply doing a reset doesn't work, because it throws away my history, a simple revert doesn't work either, because sometimes it gives me error messages (what I want to do should be possible without any error messages).

Edit: If I just do a git revert this error happens:

git revert HEAD~1
error: could not revert f1b44e3... Towards translating API to kernel.
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

How do I restore a previous version as a new commit in Git?, What happens if we want to roll back to a previous commit. Because this adds a new commit, Git will prompt for the commit message: Revert� To reset to a previous commit, before any changes were made: git reset ––hard [hash] This command wipes the slate clean back to the previous commit. Any changes you made will be lost after using the reset --hard command.

Edit: I see that you want to keep the history, so my answer below is nullified. However, it is still useful. You can reorder the lines in the editor, then proceed.

Now, back to the original answer.

You could try the rebase command.

git rebase -i HEAD~n

Where n is about one more than the number of commits between current and the one you want to revert to. So let's say, deleting the last 3 commits:

git rebase -i HEAD~4

Once you're there, it will open in VIM or Nano (or other editor). Simply delete the lines of the commits to remove, and exit the editor.

In VIM that would be esc and type :exit enter.

Now, simply push it. That will result in an error, so do a force push.

git push -f

You might have to specify branch name and upstream too. That should do it!

This method completely deletes the bad commits, so it won't just add a new commit with the revert changes.

Here's the docs: https://git-scm.com/docs/rebase

How to reset, revert, and return to previous states in Git , A common use is to restore a file from a previous commit, and you can also rewind your entire tape reel and go in an entirely different direction. We moved the pointer and reset the code back to a previous commit, but it did not delete any commits. This means that, as long as we know the original commit we were pointing to, we can "restore" back to the previous point by simply resetting back to the original head of the branch: git reset <sha1 of commit>

Use git revert https://git-scm.com/docs/git-revert

git revert HEAD~1

How to restore older file versions in Git, The only way to find and recover these unreferenced commits is with git reflog . The reset command has three different options, two of which we'll describe here: $� by admin. 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.

Take a note of the commit hash of current HEAD

  1. git reset --hard <old-commit>
  2. git reset --soft <previous-HEAD>
  3. `git add . && git commit -m "message"

Done!

Git: Revert to a Previous Commit, There are couple different things we might want to do with this ability. Right- click on the previous commit, and you'll see the option to revert this commit. # get the version of the file from the given commit git checkout <commit> path/to/file # and commit this modification git commit (The checkout command first reads the file into the index, then copies it into the work tree, so there's no need to use git add to add it to the index in preparation for committing.)

Version Control with Git: Exploring History, You can then commit the old version of the file in a new snapshot as you would any other changes. So, in effect, this usage of git checkout on a file, serves as a� git checkout git checkout <commit_id> git checkout -b <new branch> <commit_id> git checkout HEAD~X // x is the number of commits t go back This will checkout new branch pointing to the desired commit. This command will checkout to a given commit. At this point, you can create a branch and start to work from this point on. # Checkout a given commit.

Git, If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous� 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.

How to Undo the Last Commit. In this post I will show how I…, The checkout command can copy any snapshot from the repo to the working directory. 01 Getting hashes for the previous versions. Run: git hist. Note: Do not forget If you want to create a new branch to retain commits you create, you may do� You previously saw how to use git checkout to undo the changes that you made since the last commit. This command can also be used to go back even further into a file's history and restore versions of that file from a commit. In this way, you can think of committing as saving your work, and checking out as loading that saved version.

Comments
  • What's the error when reverting?
  • git revert means "back out one single, specific commit", not "go back to a previous version". Mercurial's name for this operation is better: hg backout. Mercurial has an hg revert with the meaning you expected here! Meanwhile Dai's answer is correct.
  • Argh. This won't work either. I want the old stuff committed on top of my last commit. Please guys, don't spam with wrong answers if you don't know how to do what I want.
  • @StevenObua But that's what this does - it will take the old snapshot, then when you commit it, it will be the new HEAD. I've modified my answer to include the final git commit.
  • Ok, will try that out when I am home again!
  • That's exactly what I wanted Dai. Thank you very much, and I am sorry for not reading your answer careful enough. In my mind checkout always created a new branch for the specified version, I didn't know about the other meaning.
  • If you, like me, missed the trailing . and end up in a detached HEAD state, then git checkout master (or presumably substitute master for whatever branch you were working on) will essentially undo that command.
  • Nope. That command sometimes gives errors where you need to do further things. I don't want that.