Going back to a previous commit in Github Desktop
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?
Solution 1:
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
):
- TortoiseGit (Windows)
- SourceTree (Mac, Windows)
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
Solution 2:
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.
Solution 3:
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?
Solution 4:
(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