What is TFS Code Shelve and Why do I need It?

Common questions from developers new to TFS or source control in general are:
  • What is Shelving? What does it mean to Shelve code?
  • When would I want or need to shelve my work?

Shelving is a way of saving all of the work and changes on your development machine without actually checking them in and committing these changes to the codebase. The changes are saved on the server and at any point of time you or any of your team-mates can “unshelve” them back onto any one of your machines for review or other uses.

Shelving has several uses and advantages that make them useful:

Shelving CodeSira Anamwong
Sharing Code: If you want to share a piece of code (AKA Changeset) without actually checking it in, making it available for others to access by shelving it. This is useful when:

  1. You are passing an incomplete task to another developer (you are leaving for vacation… re-assigned etc.)
  2. If your team mates need a portion of your code to advance their work (even if it’s not completed yet)
    For example you create SQL script to create/change database tables that are needed by more than one developer to work (one developer works on the ‘Create’ screen and another developer works on the ‘Search’ screen.
  3. Code reviews

Context Switching: Saving the work on your current task so you can switch to another high priority task. Does this story sound familiar: You’re working on a new feature, suddenly your boss rushes in and says “We have these URGENT bug that we need to address NOW!” and you have to drop your current changes on the feature and go fix the bug. That’s when you would shelve your work on the feature, fix the bug, then come back later and unshelve your previous work.
Saving Your Progress: When working on a complex feature or researching and evaluating multiple ways to accomplish a certain objective, it is quite common to create something, save it (shelve), revert and try other options. While working and coding, you may find yourself at a ‘good point’ where you would like to save your progress. This is an ideal time to shelve your code.

Other considerations are: when you shelve your code it is saved and stored safely in a central place, usually enterprises running TFS will make sure that it is backed up routinely; so if your machine dies overnight, you don’t lose all your hard work.