84

I am trying to use GitHub Desktop (i.e. the GUI application - NOT command line) to go back to a previous commit (on the same branch). Something that I would have thought is a core feature, since it's the primary reason for using source control in the first place.

I can see that it's possible to revert a commit, but this is not really what I want as it creates a new commit. I would just simply like to go back with the option of going forward again, in the same way that I can just hop to a different branch.

Is this possible or is it a limitation of github desktop and I need to use the cmd line for that?

SevenEleven
  • 2,326
  • 2
  • 32
  • 32
morishuz
  • 2,302
  • 4
  • 21
  • 21
  • 1
    it depends on what you want to do. If you want to go back locally, for example to create a new branch for example, you can probably just double click on the commit, or look for an option that should be called "checkout". If you want to cancel the changes from that commit, then reverting it is the way to go, because otherwise you would be changing history, which is wrong. – njzk2 Jan 14 '16 at 17:00
  • Still can't in GitHub Desktop. Use "git checkout" (or the equivalent in SourceTree or other GUI). See https://stackoverflow.com/a/4114122/199364 – ToolmakerSteve Sep 05 '18 at 18:58
  • still not possible: https://github.com/desktop/desktop/issues/10858 – mb21 Jan 24 '21 at 17:20
  • Now possible: https://github.com/desktop/desktop/pull/12160 – Jon Jun 26 '22 at 15:23

6 Answers6

79

In general, you can go back to a commit in your history with git reset.


This is not possible with GitHub Desktop. GitHub Desktop is more of a tool to synchronize your repositories and not a full featured GUI client.
But that doesn't mean you have to use the command line, since there are alternatives. You can find a list here. To mention a few (that support git reset):


Here is how you do it on command line. Most clients provide this in their UI using the same vocabulary (usually, you are able to select a commit and reset to it via context menu).

You will go back to the previous commit with

git reset HEAD^

or some more commits (for example 3) by

git reset HEAD^3

or to a specific commit by

git reset f7823ab

Have in mind that, by default, the option --mixed is passed to git reset. So, all changes made, since that commit you reset to, will still be there.

To get the original state of the commit that you want to 'revert', you have to pass --hard. For example:

git reset f7823ab --hard
Community
  • 1
  • 1
SevenEleven
  • 2,326
  • 2
  • 32
  • 32
  • 4
    ok thanks, that is definitely useful, but i was hoping to be able to do this in GitHub Desktop i.e. using the GUI – morishuz Jan 14 '16 at 15:20
  • AFAIK GitHub Desktop is more a tool to synchornize your repositories. For your needs, I would suggest to use another GUI client like [TortoiseGit](https://tortoisegit.org/) or [SourceTree](https://www.sourcetreeapp.com/). There is also a list of GUI clients [here](http://git-scm.com/downloads). They usualy have the same vocabulary as mentioned above. – SevenEleven Jan 14 '16 at 17:11
  • I strongly disregard to use GUI-Clients for everyday git-operations. I found myself learning git way fast and also discovered that some gui-clients add senseless parameters to commands. Also the console-commands never change on any platform. – JDurstberger Jan 15 '16 at 11:27
  • is there anyway that I can just search and see based on hash value? I can't find any search option! – mfaani Sep 29 '16 at 19:38
  • Thanks for helping with the `git` commands instead of just saying "GitHub Desktop doesn't do that" :) – rinogo May 10 '17 at 21:29
  • 6
    Use "git checkout" rather than "git reset", if you simply want to examine an earlier state, but ultimately will want to return to the most recent commit node. See https://stackoverflow.com/a/4114122/199364 – ToolmakerSteve Sep 05 '18 at 18:57
14

If you have a commit that you have not pushed, it is easy to undo the commit. The "undo" button appears when you have such a commit. It removes the commit from the branch's history and places the files back into the Changes area.

Undo button below commit button

Amy B
  • 108,202
  • 21
  • 135
  • 185
12

This is a comment regarding @SevenEleven's answer. A strong caveat should be given before considering using git reset. git reset is a destructive command that deletes changes following the target commit (commit-hash when running git reset [commit hash] or the latest commit when running git reset).

If I understood the question correctly, git reset violates what's asked for in the original question, as quoted: "I would just simply like to go back with the option of going forward again".

git checkout will be the more appropriate command for this scenario, by allowing to observe and branch out of a previous commit, while keeping all the changes and history intact.

after performing git checkout [older-commit-hash], you can go forward again by performing the command git checkout [newer-commit-hash] or git checkout [branch-name]


for a nice explanation of the difference between git checkout/reset/revert you can check out (no pun intended) this resource:

https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting


If you unintendedly performed a git reset and want to restore your changes, you can refer to this Stack Overflow thread:

Recover from git reset --hard?

ItaHeld90
  • 121
  • 1
  • 3
6

I found a solution within Git Desktop that worked for me, without having to use the command line.

Go to history, right-click the commit you want to go back to and click "Create branch from commit"

Helped me recover some data I lost in a more recent commit. I just deleted the branch afterwards and went back to the main branch.

Ramon
  • 61
  • 1
  • 1
2

(EDIT: Github Desktop lacks the requested command; below are instructions for a somewhat different action, that you may find useful.)

1. Click History.  
2. In the commit history list, click the commit you'd like to revert.  
3. Right-click the commit and click Revert This Commit.  

Documentation from GitHub

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Alex Albracht
  • 309
  • 2
  • 8
  • 14
    Reverting a commit and resetting to a specific commit does 2 very different things: Resetting resets the HEAD to an older commit and thereby removes the newer commits. Reverting creates a new commit which does the opposite of the commit in question. – Benjamin Hammer Nørgaard Aug 29 '18 at 11:51
  • 1
    As Benjamin explains, this is NOT an answer to the question that was asked. I've edited the answer to make that clear. Specifically, the question is about "roll back": returning to an earlier point in history. That would mean reversing the actions of ALL commits newer than the desired one. This answer is about undoing the changes from a single commit. – ToolmakerSteve Sep 05 '18 at 18:29
  • 3
    Thanks for your answer, but it is destructive and does NOT answer the question. In the best interest of the community, you should delete this answer. – rinogo Jun 19 '19 at 05:32
  • 2
    This answer still works for me, you should not delete this answer, but you should change to make it more clear that the commits will still be there, stating that you still made some changed and reverted them back. This is the preferred way so that everyone can see what really happened here, as you don't destroy or hide the history of changes. This link provides some official screenshots https://help.github.com/en/desktop/contributing-to-projects/reverting-a-commit – Jose Serodio Apr 10 '20 at 11:41
0

Ramon is right. From the right click menu, use [Create branch from commit]. Create the branch but don't Push it. When finished, delete the branch. Fast and easy.

Using command line with GitHub Desktop probably means that you did it wrong somewhere. Keep it simple. (We simplified our processes and don't need command line anymore). Making git easy for everyone.