Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going back to a previous commit in Gitkraken

My programming partner and I, being fairly new to Git and Gitkraken, made some mistakes, did some pushes pulls and commits in the wrong order, and got things fairly bungled up. I would like to go get a snapshot from several commits back, and manually compare it to the current state of the code. A command along the lines of

"Take the working directory state to this historic commit" or better yet, "give me the contents of this historic commit into a different working directory, which I will name"

The options given to me don't look that way, exactly. I see "reset master to this commit" in several flavors, but none of those titles convince me that I'm going to get to look at the previous state without damaging the path back to the current state. Am I missing something? What is the best way to do what I'm trying to do?

like image 336
Joymaker Avatar asked Aug 14 '17 21:08

Joymaker


2 Answers

I suggest you tu use git reflog, this command contains a local history of your commits and changes, with this log you will see all the events from your local repo.

After finding the exact commit that you want to recover, you can set that commit as starting point with git reset --soft HEAD@{n}

what --soft does is

Does not touch the index file or the working tree at all (but resets the 
head to <commit>, just like all modes do). This leaves all your changed
files "Changes to be committed", as git status would put it.

After this, you'll have all the changes in your stage area, then you can either decide if commit them again or change something.

I'd suggest you to read this section from Git's book and specially this section about undoing things.

PS. as an extra, you can use git reset as many times as you want, because all the commands that you use are saved in git reflog and because of that, if you do something that you don't want to, you can just undo it too.

Hope it helps

like image 140
Christopher Díaz Riveros Avatar answered Oct 11 '22 10:10

Christopher Díaz Riveros


...but none of those titles convince me that I'm going to get to look at the previous state without damaging the path back to the current state...

First of all, you can always go back. GitKraken provides the handy Undo button that lets you revert most things you do with a simple button klick. If you want to be sure you can go back easily, you can just create a backup branch. If things go awry, check it out and you're back.

Thus, you can relax and just try things out without the need to worry about breaking anything. The question now is what you actually want to do:

  • "Take the working directory state to this historic commit"

    If you just want to return to a previous state (i.e. commit), you can use the reset <branch> to this commit - hard. You'll be back at the selected commit as if nothing happened. If your prevous state is still reachable from any branch (this is the case if you created a backup branch), you can still look at all the commits and their changes.

    Be aware that you loose unstaged and uncommited changes when doing a hard reset.

  • "give me the contents of this historic commit into a different working directory, which I will name"

    Git does only know one working directory, so this is not directly possible. You can, however, compare your current state to a previous one, in the way @Christopher Díaz Riveros suggested. Instead of a hard reset, you can perform a soft one. This will reset your working directory to the selected commit, but keep all changes (compared to your previous state) as staged changes. This lets you easly review them, since they appear in the bottom right part of GitKraken and you can discard or commit as needed.

like image 31
kowsky Avatar answered Oct 11 '22 09:10

kowsky