broslav
broslav

Reputation: 615

How to revert changes to just a block of code in Visual Studio using Git

I am used to VisualSVN (Visual Studio plug-in for SVN) feature to select a part of code and revert changes to it (by shortcut CTRL+ALT+Z) and not affect the changes in other parts of the same file.

Is there something similar for Git? Or I have to revert a whole file?

Upvotes: 13

Views: 15371

Answers (3)

broslav
broslav

Reputation: 615

In Visual Studio 2022 there are improved Git controls, so while looking at current file diff one can now select a range (e.g. select lines that include one or more changes) and revert them all at once using context menu item Git -> Revert Selected Range.

By default, there is no shortcut, but you can configure a specific keyboard shortcut for that menu item in "Tools -> Options -> Keyboard" settings.

Note: This is currently available only while in diff window, not in the general text editor.

enter image description here

Upvotes: 1

Veja
Veja

Reputation: 121

Some IDE such as Android Studio the shortcut CTRL+ALT+Z to revert on block of change is default function, you can use it since the IDE was installed.

But unfortunately, if you want to do the same thing in the VS Code, you should set this by yourself.

  1. File > Preferences > Keyboard Shortcuts. (Code > Preferences > Keyboard Shortcuts on macOS)
  2. Search "revert" as keywords
  3. "Git: Revert selected Ranged" will be found
  4. Allocate shortcut CTRL+ALT+Z to it
  5. Enjoy!

Upvotes: 8

Andy A
Andy A

Reputation: 41

I know this question is a little old, but this is what worked for me in Visual Studio Code (not sure if this would work in Visual Studio as they are separate applications):

  1. Open the Source Control panel (shows the list of files with changes).
  2. Make sure the file that contains the block of code you want to revert is NOT staged. If it is staged, unstage it. You can easily stage it again.
  3. Right click on the file from #2 and select "Open File".
  4. Find the block that you want to change.
  5. Locate the blue bar to the left of the code block (should be just to the right of the code line number/s).
  6. Left (single) click the blue bar from the previous step.
  7. You should see a section pop up that shows the diff. If you don't see it, you may need to repeat #6 - I'm guessing the window pane needs focus first or something like that.
  8. Towards the right you should see some icons that look like a Plus, Undo, Down Arrow, Up Arrow, Close.
  9. Click the "Undo" icon to revert the code block. Follow any prompts to confirm.

The code block should be reverted. Hopefully this helps someone even if it doesn't answer the actual question.

Upvotes: 0

Related Questions