Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo "roll back to this commit"

Tags:

git

I am new to git and github and I have some trouble.

I am using github for windows to manage a small project I have. I think I've lost some changes I recently did to my code. What I did was:

  1. Changed some files.
  2. Accidentally committed the changes.
  3. Reverted the commit.
  4. Then I pressed "roll back to this commit".

Now all changes are gone, including some new files I added. Can I save them? I have also installed git (command line) so I can execute any command you think will help.

like image 517
nautilus7 Avatar asked May 31 '12 22:05

nautilus7


People also ask

Can you rollback undo a commit?

To undo changes associated with a specific commit, developers should use the git revert command. To undo every change that has happened since a given commit occurred, use git reset.

How do I retract a commit in git?

With GitLens installed, open your repo in VS Code and right-click on the commit from the Commits view in the sidebar to get the Revert Commit option. You can also access this option through the command palette by typing >GitLens: Git Revert or from the commit details' quick pick menu.

What happens if you revert a commit?

The git revert command is a forward-moving undo operation that offers a safe method of undoing changes. Instead of deleting or orphaning commits in the commit history, a revert will create a new commit that inverses the changes specified.


2 Answers

One of the nice things about git is that it's REALLY hard to lose anything, unless you're doing something weird.

What you need to do is to find the point which represents the state at which you want to be.

Let's say you examine git log and determine that hash 01dbc4... is the point in time to which you want to return. To do so, simply do:

git reset --hard 01dbc4

Note that if you have uncommitted changes, you'll need to stash them first.

Now, if that commit is missing the extra files you want and they're in a later commit, it gets a bit more complicated. But what you can do is set your master branch to the point from which you want to start, and then set a branch to the point which has the new files. You can then copy or or selectively merge the missing information from the new branch to your master branch, making your master branch look how you want it.

Note that even if you move a branch's pointer around like when using git reset --hard above and orphaned later commits, you get them back by examining git reflog and reseting to them, or creating a branch that points to them.

like image 148
wadesworld Avatar answered Oct 03 '22 10:10

wadesworld


Note that now (March 2013), with GitHub for Windows, you can undo a commit without having to type any git command:

See "Undo Button in GitHub for Windows"

we've added Undo support for Discards, Commits, Rollbacks, and Merges:

Undo button

like image 44
VonC Avatar answered Oct 03 '22 10:10

VonC