Visual Studio and VSTS Git: Extend the git command line to speed up your workflow–Part 1

The most basic Git commands are built into the Visual Studio Team Explorer.   The Team Explorer will cover your basic needs, but once you get beyond that, you will need to drop down to the command line.    The commands are more explicit there, and you have more options, but you will soon tire of having to repeat the same basic commands again and again.  Enter the Git aliases – and you can easily make your most used commands so much more short and effective. 

What is a Git alias

A Git alias is a short name for a more complex command which you can add to your git configuration.  You can do this either by command, or by editing the git config file directly.  A git alias can build on another other git alias, so you can start out simple and extend them as you like.  Note that you’re sort of creating your own command language here, so decide how much you want, and consider if you will be able to remember the aliases when you’re going to use them.

To add it by command, look up the syntax and usage here, but for what we’re doing now, adding a bunch of these, it is easier to edit the config file itself.

Deciding upon a syntax

I prefer very short commands, preferably single letter commands.  That limits the range a bit, so I let commands build up from single or double letters to multiple letters as they get more complex.  It is pretty common to start out with double letter commands.  In some cases that can even be necessary since the terms they’re covering might start with the same letter, like checkout and commit.

There also are some conventions that are becoming more common, and it certainly doesn’t harm to try to follow these. I you just google “git aliases” you will find a bunch of posts on this.

You don’t need to base your commands on the existing command syntax,  feel free to make the syntax match your thinking.

 

The basic set of aliases

The following list of aliases is a good starting point:

Alias git command Comment
a aliases Lets you get a list of defined aliases
b  <name> checkout –b <name> Create a branch, plus we jump to it
br <name> checkout –b origin/<name> Create a local branch of a remote branch
s status Status stating current branch and files changed and staged
m checkout master Quick way to go to master branch, since that is a very common operation
p push Push to remote
co <name> checkout  <name> Go to named branch
ci <comment> add *  + commit -m Add and commit in one op. Equal to VS commit operation.  Add comment in “some comment”
pub See below Publish local branch to remote
rm rebase master Rebase your branch on master, a very common operation

A common usage scenario using  ‘git pub’

When you work within a team, branches is the saving grace to avoid trouble.  So each developer creates his/her own work or task branch, and merges/rebases that to the common team branch – which may be a distinct branch or simply the master.

The typical workflow will then be:

  1. Create a new branch and go to that branch
  2. Code whatever it is
  3. Stage the changes by doing a git add *
  4. Commit the changes
  5. Push the changes to the remote, but also remember to create the remote branch and set up tracking  (git push -u origin <yourBranchName>)
  6. Go to master, and start over

Doing that with git commands is a lot of typing, but with the aliases it becomes so much easier as shown below:

image

Modifying the git configuration file

The git configuration file can be found in your default user folder,  named something like c:\users\yourname\.gitconfig

Go to it using :  cd %userprofile%  (or just %userprofile% in Explorer)

The file is named     .gitconfig    (note it starts with a dot!)

If the file has a section named Alias you can add lines to that, if not, add the Alias section at the bottom, so it looks like:

[alias]
    a = !git config --get-regexp alias
    b = !git checkout -b
    br = !git checkout -b origin/
    s = !git status
    m = !git checkout master
    p = !git push
    co = !git checkout
    ci = !git add * && git commit -m
    rm = !git rebase master
    rom = !git rebase origin master
    branchname = !git rev-parse --abbrev-ref HEAD
    pub = !git push -u origin $(git branchname)

 

Part 2 :  Adding in commands to access VSTS remote.    Coming soon

About terje

See http://about.me/terjes