Taking Advantage of Git's command line

Using Git directly through VS2015 is one of the most common ways for you to incorporate it into your daily workflow to easily accomplish your most frequent tasks. However, at its roots, Git is a command-line tool, and in some cases, it can be advantageous to access Git in this manner.

Throughout the previous recipe Using Git for source control, you may have noticed that VS2015 was prompting you to install the 3rd-party command-line tools. In this recipe, we are going to do just that so that all of Git's functionality is available to you when Git is used with your projects.

Getting ready

This recipe will pick up directly from the previous one, but feel free to substitute your own Git-based project should you have one available. This recipe will assume you are using Visual Studio Community 2015 Update 2, but the important thing is for you to be using a version of VS2015 with Update 2 or newer applied.

How to do it...

To use Git on the command line, take the following steps:

  1. Start your copy of VS2015. Under Team Explorer, you will see a prompt to install 3rd-party Git command prompt tools, as shown in the following screenshot:
    How to do it...
  2. Click on Install. On the next screen, you will be prompted to read a short license agreement. Again, click on Install. This second click will then take you to the official Git webpage where the download of the latest Git tools will be installed. If you are looking for them outside of VS2015, go to https://git-scm.com/download/win.
  3. After downloading the tools, run the installation program. The default options are okay, and no changes are needed. Restart VS2015.
  4. While you can open Git outside of VS2015, this recipe will explore how to do so from within Visual Studio. Using the project from the previous recipe or a similar Git-based project, click on the Changes tile under Team Explorer. Then click on the Actions menu, and select Open Command Prompt, as shown in the following screenshot:
    How to do it...

    Note

    Alternatively, you can select a local repository to work with from Team Explorer – Connect. Click on the menu Team | Manage Connections or use the hot key Ctrl + 0, C (that is, the number zero, followed by the letter C). Once that is open, right-click on the desired Git repository and select Open Command Prompt, as shown in the following screenshot:

    How to do it...
  5. Once you have opened the Git command prompt, you will find that you are currently located in the base directory of the solution you have selected. From there you have access to all of Git's commands, and we will take a look at some of the common ones.
  6. The command git status will indicate whether or not you have any outstanding files to commit, and indicate the branch you are on. The following listing is an example:
    c:usersjemardocumentsvisual studio 2015ProjectsCh8-Git-ConsoleApplication1>git status
    On branch master
    nothing to commit, working directory clean
    
  7. The command git log will show the commit history for the project you are working on. The following excerpt shows a portion of the history from our previous project:
    commit 5ac2388506970f3283c2a364c620f6537534f4ae
    Author: Jeff Martin <jemartin>
    Date:   Sat Jun 25 13:07:16 2016 -0400
        Adding new class
    commit bf162194b618b5a7c3166655be72d5c5dbe47c8f
    Author: Jeff Martin <jemartin>
    Date:   Sat Jun 25 13:04:05 2016 -0400
        Revert "Just project file"
        This reverts commit 2494dc217b7e752124b5a4bcf3b144720fea7248.
    commit 2494dc217b7e752124b5a4bcf3b144720fea7248
    Author: Jeff Martin <jemartin>
    Date:   Sat Jun 25 13:03:35 2016 -0400
        Just project file
    

How it works…

Installing the Git command-line tool allows VS2015 to work with the official project tools, and provide you with an additional option beyond the GUI based approach provided in VS2015. Since Git is a multiplatform tool, various tutorials and scripts will assume it is present, and this allows you to make use of them without being constrained by VS2015.

Tip

Not all of the Git commands are available through VS2015. There are a few that require you to use the command line, including stash and reset. For a complete list, go to https://www.visualstudio.com/en-us/docs/git/command-prompt.

There's more…

While Microsoft recommends that you start the Git command prompt from within VS2015 as we have shown in this recipe, that isn't the only way. As part of the Git tools installation, you may also use the included Git Bash program. Git Bash is accessible through your Windows Start menu. This uses the familiar Bash shell environment to interface with your file system. The biggest immediate difference to using this via the approach in the recipe is that you will start in your Windows user's base directory rather than within your VS2015 project folder. Those of you familiar with Bash and its associated commands may prefer this method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.255