CIS Linux – Micah Webner

Getting Started with Git (Fall 2021 Update)

Posted Thursday, September 30, 2021

Important note: Be sure to review these Recommended Editor and Git settings when you've finished the steps on this page.

You should be using Git

If you've ever made a backup copy of a file you were working on, and then a second and a third, and then–months later–could not figure out why you made these copies, in which order, the you need version control. Git has some fairly advanced concepts to master, but every good developer owes it to themself to learn the basics.

Most of the tutorials in this section focus around using ssh and git, because they go hand in hand. Git is the best way to manage your project files, and git uses ssh to connect to a shared repository server.

All you really need to get started is to create a repository following some simple steps, then update that repository every time you change something, making sure to say what you changed and why you changed it in your commit message. Any directory that contains files can be a git repository, but it's especially useful for plain text source code.

You might also want to read my Getting Started with Git blog post. It explains how to create a repository, provides some nice aliases for the command-line tool, and contains tips about writing good commit messages.

Demonstration

This video demonstrates how to install Git source control management on Windows then configure ssh keys for secure communication with remote servers like HFC's GitLab server or public providers like GitHub, GitLab, or BitBucket.

Scroll down past the video and setup info for important git project settings!

View this video on YouTube

For editing projects, see also Basic Web Projects with Git and Visual Studio Code.

Generating keys

This is covered in detail in the video, but here are some screenshots that show the details and commands for generating ssh keys.

To generate a new key:

 ssh-keygen -o -a 100 -t ed25519 -C 'yourname_YYMMDD'

Note: The -C option is the key comment. I've found it useful to include your username and the date so you can keep track of how long you've been using the same key. This comment gets added to your public key, which helps make keys easier to manage.

To view the contents of your public key for copying:

 cat .ssh/id_ed25519.pub

Windows and Linux

If you are on Windows or Linux, create (or append) your .bashrc file to contain the following commands:

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_ed25519

Exit your git bash session and reopen. You should be prompted for the passphrase to your private key.

MacOS

The following steps will set up MacOS to use your private key automatically.

Store passphrase in the Keychain

In the latest version of MacOS (12.0 Monterey), just do this once:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Or in versions of MacOS older than 12.0 Monterey, use:

ssh-add -K ~/.ssh/id_ed25519

Enter your key passphrase, and you won't be asked for it again.

Configure SSH-agent to always use MacOS Keychain

Create a file .ssh/config (under your home directory) to contain the following settings:

Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_ed25519

Once you've done this, MacOS should take care of starting the agent, and will allow you to store the key's passphrase in Keychain so you won't have to type it in the future.

ssh-keygen screenshot

Testing your setup

One time only, open an ssh connection to HFC GitLab server. Your login attempt will fail, but it will let you add the servers' key to your known_hosts file, which you'll need for later git operations.

ssh git@gitlab.hfcc.edu

ssh test screenshot

Add public key to HFC GitLab server

Now that you've verified your key setup, copy the contents of your public key file, and open up the HFC GitLab website. Pull down the menu in the top right corner and select Preferences.

GitLab preferences menu

In the menu on the left, select the SSH Keys tab. Paste your public key in the text area and add your key.

GitLab SSH keys form

After adjusting the settings shown below, you should be ready to use GitLab with ssh keys.

Important settings

In addition to the installation and setup in the video above, here are some important and useful settings you should configure.

Git needs your name and email address to format commit messages properly. If you do not make these settings, git will throw errors while trying to commit.

You will also need correct line-ending settings or we will run into problems working across various platforms, including Docker and the production web server.

git config --global user.name "Your Name"
git config --global user.email you@domain.com
git config --global init.defaultBranch main
git config --global core.autocrlf false
git config --global core.safecrlf false
git config --global core.ignorecase false
git config --global core.whitespace "fix,blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2"

In addition, you may wish to add my full set of recommended git and editor settings.

Commit authorship

In most cases, the user.name and user.email settings above will set correct authorship of your commits. However, when working with our project in Docker, you will need to specify authorship on the command line for proper credit.

The first time you commit to a repository, you'll need to specify your full name and email in your commit message using the --author switch.

git commit --author='Your Name <yourname@hawkmail.hfcc.edu>' -m 'Commit message goes here.'

After you've done this, you can abbreviate the author tag with part of your name, such as --author=yourname instead.