Skip to content

Instantly share code, notes, and snippets.

@karlstolley
Last active November 18, 2021 19:13
Show Gist options
  • Save karlstolley/613ab099f634b2638e474f6500537a62 to your computer and use it in GitHub Desktop.
Save karlstolley/613ab099f634b2638e474f6500537a62 to your computer and use it in GitHub Desktop.

Version Control: Command-line Git Installation

Be sure to install Git version 2.30.0 or higher.

MacOS/OS X Installation

A lot of developer technologies require XCode’s command-line tools, but they are useful in their own right. To install them, you’ll need to pull up the Terminal app on your Mac. The easiest way to do this is through Spotlight Search: click the magnifying glass in the upper right-hand corner of your Mac, and start typing Terminal; click the result that appears labeled as an Application.

You’ll have a small white window that pops up, with a line that most likely has the name of your computer followed by a colon, tilde, and dollar-sign, e.g., janes-mac:~ $. There’ll also be a cursor, which may or may not blink.

Type xcode-select --install (note the space before the two hyphens) and hit the Return/Enter key. (You might be prompted for your admintrator password.) You should see a some output fly by, unless you've installed XCode previously. In that case, you'll get an error informing you that your command-line tools are already installed. Once the script has finished running and you see the dollar-sign prompt again, type git version and press return. You should see git version 2.30.1 (Apple Git-130), or a similar number, output in the Terminal window.

To easily install the very latest version of Git, follow the instructions below for installing Homebrew and then run:

$ brew install git

Don't forget to configure Git once you've got the latest version installed.

Windows Installation

Download the Windows installer for Git and double-click the downloaded file’s icon to install it; it’ll probably be in your Downloads folder.

Walk through the Git Setup Wizard, making the following changes from the defaults:

  1. On the Select Components screen:
  • Select "On the Desktop" under Additional Icons
  • De-select "Windows Explorer Integration"
  1. On the Adjusting your PATH environment screen:
  • Choose "Use Git from Git Bash only*"
  1. On the Configuring the line ending conversions screen:
  • Choose "Checkout as-is, commit as-is"

Git Setup will then complete the installation; it takes a few minutes. Click Finish when it's done.

Look for the Git or git-bash icon on your desktop. Double-click it, and type git version into the terminal window that opens. You should see output like git version 2.32.0 or some similar number.

Linux Installation

If you're running Linux, Git may already be on your system. Run git version in your terminal shell to see if there's any output. If you get a "Command not found" error, Google around for the preferred method to install or compile Git on your Linux distribution, preferably through your distro's package manager so you automatically receive updates along with the rest of your system.

Basic Git Configuration

You'll need to open a terminal window (MacOS/Linux) or Git Bash (Windows), and type the following commands. Replace YOUR NAME and YOUR@EMAIL with the actual name and email you use.

$ git config --global user.name "YOUR NAME"
$ git config --global user.email "YOUR@EMAIL"

When you've run those commands, run git config --global --list and you should see your name and email. If you do not, re-run the commands above to fix any mistakes you've made.

Setting Up the Default Branch (main)

As of Git 2.28.0, it is essential to configure Git to use a custom default branch name. The default branch had been master, which Git, GitHub, and the wider industry are moving away from because of its historically racist connotations. main is now the preferred name for the default branch, and we will use that in this course. To set main as your default branch for all new repositories that you create, simply run:

$ git config --global init.defaultBranch "main"

If for whatever reason you cannot install at least Git 2.28.0, your best bet is to create an alias for yourself, such as git new, which you will need to use in place of git init:

$ git config --global alias.new '!git init && git symbolic-ref HEAD refs/heads/main'

If you have old repositories that you'd like to convert from master to main, check out this excellent post's instructions.

Git Workshop Topics

git-config

git-status

https://git-scm.com/docs/git-status

Relevant config stuff:

git-stash

https://git-scm.com/docs/git-stash

git-diff

--word-diff="color": https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---word-diffltmodegt

Aliases

Alias your own typos, e.g., git dif for me

  • alias.dif=diff
  • alias.dw=diff --color-words
  • alias.diffmoved=diff --color-moved=plain

git-branch

https://git-scm.com/docs/git-branch

  • --merged, --no-merged; prefixed with --sort
  • Listing branches by authordate (when the last commit was made) git branch --sort="-authordate" ; see also the branch.sort config value

git-switch, git-restore, git-checkout

git-clean

https://git-scm.com/docs/git-clean

git-log

https://git-scm.com/docs/git-log

git-show

https://git-scm.com/docs/git-show

Especially short formats (--oneline) and custom formats. Couple with aliases.


Rough Order of Things

  • What You’ve Done -> the state of your working directory
  • What You’re Doing -> crafting commits
  • What You Did -> inspecting history
  • What You Want People to Think You Did -> rewriting history

Quick Review

  • Configuring name and email
  • The basic Git workflow: create/modify; save; add; commit
  • The professional way to write a commit message

Burning Questions

  • Anything you’ve ever wanted to know about Git

Configuring Git to Your Liking

  • Configuration values: global, per-repo
  • A global excludes file
  • Aliases

Inspection

  • Past: git log, git show, git diff
  • Present: git status, git diff (--staged)
  • Future: git stash

Experimentation

  • git-branch

Course-Correction

  • git-reset, git-restore, git-revert

Rewriting and Simplifying History

  • git-rebase

Bonus Advanced Topic: git hooks

https://git-scm.com/docs/githooks

Example pre-commit Hook for Automated Testing

#! /usr/bin/env sh

STASH_NAME="pre-commit-$(date +%s)"

git stash push -q --keep-index --include-untracked --message $STASH_NAME

FAILED_TESTS=0
trap 'FAILED_TESTS=$((FAILED_TESTS+1))' ERR
# List all test commands to be run, e.g.,
# npm run test

STASHES=$(git stash list -1 | grep -o $STASH_NAME)
if [[ $STASHES == $STASH_NAME ]]; then
  git reset --hard -q && git stash apply --index -q && git stash drop -q
fi

if ((FAILED_TESTS == 0)); then
  exit 0 # Proceed with the commit
else
  echo "Unable to make commit; fix code and stage fixes with \`git add\`"
  exit 1 # Halt the commit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment