Tuesday, May 02, 2017

My Git set-up

It took some time to get used to git, but it keeps taking more and more time to migrate others to git.
So here's a quick guide to help out with getting up to speed using git, specifically in corporate environments where you would also need some SSH configuration.

Get some tools

You don't have to stick to one single tool. I work on Windows. What I prefer to use for my work configuration is a combination of:
SourceTree
and
TortoiseGit

Why both?

SourceTree

  • uses an integrated window to display the status of many repositories
  • uses a panel layout that allows the easy display of history, status and modifications.
  (it does look strange at first, with so much information in one screen, but very productive later on)

TortoiseGit

  • has a Windows Explorer icon overlay integration, which is very useful.
  • has a nice merge tool; I'm also using it from SourceTree
Regardless of which tools you use for providing a GUI, you will end up using the command line interface for more advanced features.

SourceTree Diff/Merge

I prefer to have SourceTree configured to use TortoiseMerge. (accessible from SourceTree -> Tools -> Options -> Diff)
SourceTree with TortoiseMerge Diff/Merge
You could use Meld Merge instead. It looks like nice tool.
You could even ignore using a merge tool and edit everything manually, without a dedicated GUI, but that's not as productive.

Configure the basics

If you'll be using git for work, you'll want to do this securely, so one approach would be to use ssh.

Create some ssh keys

Or at least one.
  1. First, go to your user account to make certain you don't already have some configured files that you would overwrite.
    • Open a Windows Explorer window and go to [%userprofile%/.ssh].
    • If you have an id_rsa file already created, don't just run the following steps; instead: adjust them to your needs: either rename the file in Explorer, or use a different file name in the commands.
  2. Open a git bash terminal (from SourceTree: SHIFT + ALT + T) and type
    • ssh-keygen -t rsa -b 2048 -C "gusti.email@address.com"
    • Generating public/private rsa key pair.
  3. Enter file in which to save the key (/c/Users/gusti/.ssh/id_rsa): /c/Users/gusti/.ssh/id_rsa
  4. For simplicity, you may leave the password empty, but it would not hurt to add one.
    • Enter passphrase (empty for no passphrase):
    • Enter same passphrase again:
  5. Your identification has been saved in /c/Users/gusti/.ssh/id_rsa.
  6. Your public key has been saved in /c/Users/gusti/.ssh/id_rsa.pub.

Assign the public key to your account

Upload the public ssh key (/c/Users/gusti/.ssh/id_rsa.pub) to your account on your git server.
For enterprise git approaches, you  may end up using a 3rd party solution, such as Atlassian's Bitbucket. Just go to your account setting, and add a the (public) SSH key to the list of SSH keys.
SSH Keys in Account Settings

Use the private key in your tools

Convert the key

For SourceTree on Windows you'll have to use the Putty key generator to transform the key to the ppk format it expects to use (Tools -> Create or Import SSH Keys).
Conversions -> Import key (/c/Users/gusti/.ssh/id_rsa)
  -> Save private key
The PuTTY Key Generator can convert ssh keys

Use the key

Use the key in Pageant (Putty authentication agent)
Look for the icon in the SysTray. If it's not started, you can use SourceTree -> [Tools] -> [Launch SSH Agent...]
OR:
SourceTree -> [Tools] -> [Options] -> [General] -> [SSH Client Configuration] area; change the used "SSH Key" file and enable "Automatically start SSH agent when SourceTree opens".
Quick SSH key specification in SourceTree

If you entered a password for your file, you will be presented with a pop-up asking for the password when opening SourceTree.
Restart SourceTree to make certain it works. You may have to close the Pageant utility from the SysTray, in order to restart it.

Some more configuration

Line endings on Windows
Line endings differ between Windows and Linux, and sharing code between developers on Windows and Linux is a pain without an automatic handling of the line endings.
Make sure you have the setting core.autocrlf set to true as suggested in this article on github.

Note: you can check what value the key already has, by opening a git bash / terminal, typing git config --list and searching for the core.autocrlf value.

Ready for cloning

You are ready to clone your first repository. You can use the SourceTree integration for Bitbucket.
SourceTree Bitbucket integration

Get some training

There are many resources out there and you will end up on stackoverflow or some similar site searching for answers at some point. What you should get accustomed to are the core principles of git.
I found 2 training courses by Paolo Perrotta on PluralSight to be very useful:

Get some hands-on experience

Code on some repositories where other people are involved, so that you deal with branches, merging, conflicts and configuration issues.

No comments:

Post a Comment