59

I am really new to git and source control.

I am using visual studio tools for git with vs2012.

I am on some commit and want to go back to some previous commit but i cannot seem to do it how. When i go to the commit details the revert button seems to have been grayed out.

I have stuck on this problem for the last 2 hours. I have researched the internet but to no use. Please can somebody tell me how to revert to a previous commit.

Thanks.

Win Coder
  • 5,948
  • 11
  • 48
  • 78

4 Answers4

60

Visual Studio 2015 Update 2 adds support for GIT "Reset", which is what you probably want to do:

  • open history
  • right click the commit you want to revert to
  • reset -> reset and delete changes

GIT Reset in Visual Studio 2015 Update 2

GDavid
  • 76
  • 2
  • 12
Runar Jordahl
  • 892
  • 1
  • 7
  • 6
38

You don't want to do a revert - revert just takes a commit and undoes it.

If you want to go back to a previous commit - there are two options:

If you want to permanently go back, do a git hard reset, which rolls back the code to a specified commit. You can do this via:

git reset --hard {commit number}

If you want to temporarily go back, you can create a branch from that commit. which will essentially keep you current path in the code history, and create another path from the point in history where that code was committed.

Community
  • 1
  • 1
Oved D
  • 6,244
  • 8
  • 43
  • 65
  • 14
    I suspect that OP wants to do the `reset` through the Visual Studio GUI. I can't find any way to do that either. – Klas Mellbourn Jun 23 '13 at 07:41
  • A hard reset will also lose the changes you made, including other files that you haven't committed. If you still want the changes, do a soft reset (omitting the --hard flag). Both are valid options, you just need to know which one you want. – Brian J Nov 06 '14 at 15:35
  • 2
    This is not an answer to the question, which is in regard to Visual Studio. – tintyethan Jan 26 '15 at 14:38
  • Thanks guys, this helped me a lot! I didn't realize I had to specify the parent commit that I wanted to roll back to, I kept trying to reset the commit that I actually wanted to erase – Chris Schaller Aug 08 '17 at 22:49
18

Well to those newbies who have the same problem, the best way not to waste 3 hours of your life is simply not to use visual studio tool for GIT. At least not in its current form.(23 Jun 2013)

After wasting much time i found out from an obscure link that the vs extension only supports a few of the GIT function with reset not being one of them.

Win Coder
  • 5,948
  • 11
  • 48
  • 78
  • 1
    Sadly, that is my conclusion too. For now, it is better to use the "Git Source Control Provider" http://visualstudiogallery.msdn.microsoft.com/63a7e40d-4d71-4fbb-a23b-d262124b8f4c (which is not from Microsoft) – Klas Mellbourn Jun 23 '13 at 09:37
  • 1
    @Klas Mellbourn. Still not perfect that one either sadly. – Stígandr Feb 25 '14 at 10:56
  • 1
    The function is supposed to exist in VS per document, but I couldn't get it to work : http://msdn.microsoft.com/en-us/library/vstudio/dn237244.aspx#revert – Patrick J Collins Apr 07 '14 at 14:44
  • 7
    @PatrickJCollins It works as of Feb. 2015. Took me a while to find the history page though.. Go to the branches page, right click on your branch and select view history. From there you can right click on any commit and hit revert changes. – user1599328 Feb 21 '15 at 21:55
  • I think that Visual Studio, during the simplifying momentum, omits so much useful information about git "inner operations", that now it is impossible for a skilled developer to find out the right method. – sentenza Dec 11 '15 at 08:36
  • This post is no longer relevant as VS Team Explorer is the only GIT tool I need or use. It's how I train everyone. – Daniel Jackson Jul 12 '19 at 15:28
11

In Team Explorer -> Branches then select Actions -> Open command prompt and use the git commands either:

  • git checkout your commit id
  • git reset your commit id

check this out to understand the difference What's the difference between "git reset" and "git checkout"?

I think you'll also need to have git extension installed in Visual Studio, I have it, but not sure it's required to do that.

enter image description here

Sometowngeek
  • 571
  • 6
  • 24
nest
  • 1,274
  • 1
  • 13
  • 33