Pages

Friday, May 20, 2016

Moving your changes to a new branch in Git with Visual Studio

I am pretty sure this scenario happens to everyone a couple times during a development cycle, if they are working with source control and branching. You make changes, you commit those changes, you merge those changes to a different branch. You make more changes, you go to commit those changes, you realize you are still on that different branch. Now what?

In Visual Studio you cannot simply switch branches with changes that need to be committed. Because of the way Git manages the commit history, it could break or lose changes if this happened. Instead, you are presented with a lovely message:

Thankfully, there is a pretty simple approach that seems to work well for just this case: Stashing.


What is Stashing?

You can read up on Stashing in the Git Documentation (https://git-scm.com/book/no-nb/v1/Git-Tools-Stashing), but the TLDR is that it's a way to save the "dirty" state of your changes to apply at a later time. It allows you to switch branches, or work on something new in the same branch without losing or applying the changes you stashed.
In our scenario of making changes in the wrong branch, stashing allows us to save the changes made without committing to that wrong branch, after which we can switch to the correct branch and apply those changes.

Stashing in Visual Studio

You can't. Well, as of right now I am using Visual Studio 2013 and can confirm it's not possible there, and from what I read for Visual Studio 2015, the feature is under "review" or "consideration", which indicates you can't in 2015 either.

However, all is not lost, and it's actually pretty simple to perform this task starting within Visual Studio. 

Step 1: Open the commend prompt. 

This is actually pretty easy. In Team Explorer, switch to Branches, Changes, or Unsynced Commits and, click the Actions menu and select "Open Command Prompt".


Step 2: Check Git Status

This is more of a "measure twice, cut once" step to confirm the prompt shows the branch your expecting. Type the command "git status" and press Enter and verify the branch named matches your branch in Visual Studio. Additionally, below that you will see a list of the changes waiting to be committed. 


Step 3: Stash It

Execute the command "git stash" in the command prompt, and Git will save the changes and provide a confirmation message. You can also go back to Visual Studio, and it will prompt you to reload files that have changed as a result of the stash. If you click on "Changes" in Team Explorer, you should see an empty list. 


Step 4: Switch Branches

You can do this two ways:
  1. In the command prompt type the command "git checkout [branch name]" and press Enter. Provided you spelled the branch name correctly, you will be presented with a nice confirmation message:

  2. In Visual Studio, you can go into Team Explorer, select Branches from the menu, and switch to the branch you prefer. This is useful if you forget which fancy, special characters you used when you named that "L337 Dev-Br4nch!" for fun:
Fun fact: If you bring the Visual Studio window up in the background behind your Command Prompt with Team Explorer open to one of the Changes, Unsynced Commits, or Branches tabs, and run the "git checkout [branch name]" command, you can watch the Team Explorer window update as you switch branches.


Step 5: Stash Apply

If you want to "measure twice, cut once", you can repeat step 2 to double-confirm the branch you are on, or even run the "git stash list" command to list all stashes and confirm your changes are there.

However, at this point, the last thing to do is apply the stashed changes. Simply execute the command "git stash apply" and the last stashed changes will be applied to your current branch. Again, you'll get a nice confirmation message, and you can check the Changes tab in Team Explorer within Visual Studio for a list of the changes.

All Done!

From here you can continue development, or you can commit those changes if you need to. There are plenty of more advanced tricks you can do with the "stash" commend, and you can read those in the Git documentation. At this point, though, you are now operating in the branch you needed to be in, and your changes are with you. 

Enjoy!

1 comment:

  1. Thanks a lot for the tutorial cam. Came in very handy and works at the command line anyway in VS 17

    ReplyDelete