Rules to Better Version Control with Git

​Since 1990, SSW has supported the developer community by publishing all our best practices and rules for everyone to see. 

If you still need help, visit Application Lifecycle Management and book in a consultant.

Hold on a second! How would you like to view this content?
Just the title! A brief blurb! Gimme everything!
Rules to Better Version Control with Git
  1. Do You Choose TFS-Git Over Team Foundation Source Control?Unpublished

    Only a madman would write software today without using source control.

    Apart from being the easiest way to back-up your code, and having the ability to go back to a previous state, source control provides a huge number of benefits, especially when integrated with Continuous Integration servers, and Continuous Deployment solutions.

    But which source control solution should I choose?​​

    Figure: Bad Example - Unless you have time-travelled back to 1999, you should not be using Visual Source Safe
    Figure: Bad Example – Git is great, but learning to use ‘pure’ git from the command line can be intimidating for developers not used to working in a console
    Figure: Bad Example - Team Foundation Source Control – Server Workspaces. This is the source control that we have been using in TFS for years. It’s great, even on large code bases, except for when you are not connected to the TFS server. (Image from http://bit.ly/why-tfs​-git​)
    Figure: Bad Example – Team Foundation Source Control – Local Workspaces. It’s great for when you are not connected to the TFS Server, except for when your code-base is very large. (Image from http://bit.ly/why-tfs-git)
    Good Example – TFS Git works fantastically offline and with large code-bases. Visual Studio has support for the basic operations so developers can ease their way into working with git. Once users become familiar with it, it also allows for far more control and flexibility. Note: My Work and Code Review are not currently compatible with TFS Git, and you will need to fall back to the Git command line for more complex source control commands
    Resources:Taking your version control to a next level with TFS and Git by Alexander Vanwynsberghe.
  2. Do you know these important git commands?

    Git Reset – Discard your local changes and revert to your last Commit​

    Figure: When trying to undo a change you will sometimes get errors. The answer is to exit Visual Studio and use the command line
    Figure: use ‘git reset --hard HEAD’ to throw away all your uncommitted changes
    Note: A common cause of Visual Studio failing to undo changes is an incomplete .gitignore file. See http://adamstephensen.com/2014/05/13/update-your-gitignore/​ for more information.
  3. Do you know how to manage NuGet packages with Git?

    ​​​​​​Do you know the best way to manage NuGet packages with Git? You can get into all sorts of trouble by including your packages in source control.


    Do not check packages into Git

    The following are a few issues that are related to having your NuGet packages in source control:

    1. Over time the packages will grow to be too many and cloning the repository will be slow.
    2. You could get duplicate NuGet packages in your packages folder as new versions are updated.
    3. NuGet shows packages to update that have already been updated. This can happen if you have duplicate NuGet packages but they are different versions.
    4. It becomes harder to "clean" your solution of any unused package folders, as you need to ensure you don't delete any package folders still in use.

    Nuget will automatically restore packages with out checking them in to source control
    Beginning with NuGet 2.7, the NuGet Visual Studio extension integrates into Visual Studio's build events and restores missing packages when a build begins. This feature is enabled by default and packages.config will be automatically included in souce control.

    Here's how it works:

    1. On project or solution build, Visual Studio raises an event that a build is beginning within the solution.
    2. NuGet responds to this event and checks for packages.config files included in the solution.
    3. For each packages.config file found, its packages are enumerated and checked for existence in the solution's packages folder.
    4. Any missing packages are downloaded from the user's configured (and enabled) package sources, respecting the order of the package sources.
    5. As packages are downloaded, they are unzipped into the solution's packages folder.

    Support in legacy versions of NuGet

    It is highly recommended that you upgrade to the latest version of NuGet to to avoid having to configure your solution to not check in NuGet pagages to source control.


    You can read more here http://blogs.msdn.com/b/dotnet/archive/2013/08/22/improved-package-restore.aspx?PageIndex=3#comments.

  4. Do you know how to programmatically get Git commits?

    ​​​​​​​​With Visual Studio Online now supporting Git, ​​​​more developers are changing their source control repositories. What happens if an application you developed relies on the TFS Client Object Model to get information out of source control (e.g. changeset comments) and the developers start using Git?​

    That's where the new Visual Studio Online REST APIs come in. You can get a list of commits from your VSO Git repository with only a HTTP request.​


    8-08-2014-9-58-37-AM-compressor.png
    Figure: HTTPS GET commits from your VSO Git repository


    ​Using H​TTPS with basic authentication, make a GET request to a URL as below, substituting in your VSO details. A JSON object will be returned. To quickly create classes from a JSON response, see the rule Do you know how to easily get classes from a JSON response?


    8-08-2014-4-24-34-PM-compressor.png
    Figure: Using the Chrome extension Postman to execute our request with Basic Authentication


    ​For a C# implementation, see this blog post Getting Git Commits with the VSO REST API​​.​

    (This is based on Get started with the REST APIs and VSO Integration Reference)​

  5. Do you know The Levels to Git Mastery?

    ​​​Like most skills, it can take a little while to get your head around Git.
    We rate our devs and the devs that we mentor on the following scale.

    Where are you?

    ** Level 1 - Understanding the basic principles

    At this level, you need to have an understanding of the basic operations (including branching).​

    Your workflow looks like this:

    • init local repository / clone
    • pull
    • <code>
    • commit
    • push  

    ** Level 2 -  Working with Git on a team

    Now that you know the basic Git commands you can start working on projects with more than one developer.

    You should be using local feature branches for your work.

    Your workflow involves:

    • pull 
    • merge
    • push  

    ** Level 3 - Learning to use pull requests 

    Pull requests can be used for code reviews within your team, or to accept suggested changes from people outside your team.

    Pull requests should preferably be used with policies (TFS Git only  - harder with GitHub). 

    ** Level 4 -  Working with a team advanced - Rebasing (harder, but worth it) 

    When working in a team, Git does a pretty good job of merging code together from different branches... but it can be very messy. 
    True Git masters master rebasing​. It lets you keep a much cleaner project history.

    Git process for Git masters:​

    • pull master
    • rebase feature branch on top of remote master
    • push feature branch to remote or create pull request
  6. Do you know to Rebase not Merge?

    When you merge a branch you end up with messy merge commits.

    Rebasing might take a bit to get your head around, but you get a much cleaner project history.

    • it eliminates the unnecessary merge commits required by git merge
    • rebasing also results in a perfectly linear project history - you can follow the tip of feature all the way to the beginning of the project without any forks.

    This makes it easier to navigate your project with commands like git log, git bisect, and gitk.

    rebase1.png
    Figure: When merging: a messy merge commit is created any time you need to incorporate upstream changes from the master branch
    rebase2.png
    Figure: Git Rebase moves your new commits to the end of the master branch. This ensure that you don't end up with messy merge commits and you have a clean linear project history

    Warning: If you don’t follow the Golden Rule of Rebasing, you could end up in a ​world of pain.

  7. Do you know The Golden Rule of Rebasing?

    ​Rebasing is great for ensuring a clean project history... but it can be dangerous in inexperienced hands.

    The golden rule of git rebase is to never use it on public branches. (ie. never rebase master).

    You should never rebase master onto a feature branch. This would move all of the commits in master onto the tip of  the feature branch (not the other way around). 

    Since rebasing results in brand new commits, Git will think that your master branch’s history has diverged from everybody else’s. If you were to Push this to the server... expect lots of pain to fix it up!

    rebase3.png
    Figure: Bad Example: Rebasing master onto a feature branch can cause project history to become confused.    
    rebase4.png
    Figure: To get it wrong in Visual Studio you would need to change the current branch to master and then choose rebase. While this is possible, the VS team have done a good job making it hard to do the wrong thing
    rebase5.png
    Figure: Good Example - Rebase your Feature branch onto Master
  8. Do you know how clean up stale remote branches in git?Unpublished

    ​Do you know that remote branches on your machines are not automatically removed in most of Git clients?
     


    Git pro tip​​

    If you use console for fetching data use this command to fetch and​ prune remotes/origin folder at the same time!

    git fetch -p​

  9. Do you know the best tool to migration from TFVC to Git?

    Git has become the defacto standard for version control systems. It's distributed and decentralized and promotes working disconnected as default. It also takes away the pain of branching and merging and has a built in code review system with pull requests. Everybody should be using Git, and if you're not, you should be migrating the Git using one of the below tools.
    • VisualStudio.com - Import Repository
    • Git-Tf
    • ​​Git-Tfs (recommended)

    ​VisualStudio.com - Import Repository

    ​VisualStudio.com gives you the ability to import from a TFVC repository into a new Git repository. 

    03_29_08.png

    Bad Example - Built in tool has several limitations
    If you don't care about source control history, then this inbuilt tool is the easiest to use. It has the limitations of:
    • ​180 days of history
    • No branches

    TIP - Use​​​​ this if you don't care about source control history

    Git​​-Tf 

    Git-Tf is an open source command line tool ​that works cross platform and use the Java TFS SDK. This tool is useful for migration if you're not on a Windows environment. This tool is not maintained and has issues with migrating branches. 

    TIP - ​Use Git-Tf if you don't have a Windows environment

    Git-Tfs (​​​Recommended)

    Git-Tfs is an open source command line tool that uses the .NET TFS SDK to interface between Git and TFVC. It has the following advantages over the other tools:
    • ​Actively mainained
    • Good support for branches
    • Author mapping
    • Migrates all history​
    Follow the migration guide to import from TFVC to Git.
  10. Do you know to connect to VSTS Git with Personal Access Tokens

    ​​​​When you create a new git repository and need to push it to VSTS you need to provide login credentials.​/p>

    It isn't always clear how to do this.

    vsts-alternative-login.png
    Figure: Bad Example - Alternate  Authentication Credentials should not be used. When you change the password it invalidates all projects and can't be scoped to limit access to your Team Services data

    Instead, you should use Personal Access Token. You can do this in two ways.

    The first option is to make sure your Git for Windows is up-to-date and when cloning the repository, you use Microsoft Account to log in. Personal Access Token for Git will be created for you.

    git-credentials-personal-access-token.png
    Figure: Good Example - Windows for Git credential manager will automatically create Personal Access Token for Git

    Option 2 is to manually create Personal Access Token and use it as a password for Git login.

    You can follow this blog post for full instructions: Using Personal Access Tokens to access Visual Studio Online.

    git-credentials-personal-access-token-manual.png
    Figure: Good Example - You can also manually enter Personal Access Token into password section if the credential manager doesn't work​
  11. Do you know what to do after migrating from TFVC to Git?

    After you use the right tool to migrate from TFVC to Git, there's a few more things you need to do to clean things up for a new user joining the project. By default, if there is a TFVC repository, that will become the default in the UI.

    Unfortunately, you can't kill the TFVC repository and make Git the default one, so there's a few steps you need to follow.



    2017-04-05_10-02-58.png

    ​Figure: Bad Example - Can't delete the now deprecated​er TFVC repository​​

    Delete files from TFVC

    Go into the repository, delete any existing files. Add a new document saying "_Migrated_to_Git.md". This will stop people from getting the wrong code.

    2017-04-05_10-24-52.png

    ​​​​Figure: Clean up TFVC so developers can't accidentally get the wrong source code

    Note: All the source code is still there, it's just flagged as being deleted.

    Lock down TFVC

    In the TFVC repository, click Security

    2017-04-05_10-43-51.png

    Figure: Con​​figure the security of the TFVC repository

    ​​Then deny check-ins to Contributors, Project Administrators and Project Collection Administrators. This should stop anyone from committing new code to the repository.

    Update the Dashboard

    ​Next step is to update the dashboard to let new developers know.

    2017-04-05_10-30-43.png​​

    ​​Figure: Good example - Let new users know that the source control is now on Git

    ​​

    ​Suggestions for the VSTS team

    1. ​​Give us the ability to hide a repository
    2. Give us the ability to set a repository as the default for all users
    3. Give us the ability to delete a TFVC repository

    Having any of these suggestions will avoid the confusion on this screen
    2017-04-05_10-06-12.png
    ​​Figure: Bad Exmaple - This is confusing for a new dev