Introduction to Version Control With Git

Elsa Gonsiorowski

July 11, 2022

Created: 2022-07-11 Mon 11:46

1. Introduction

Schedule for the Week

Version Control with GIT Mon
Data Structures Tue
Intro to Containers Wed
Threads and Asynchronicity Thu
Software Automation Testing Fri

Caveats

  • This is a 1 hour session, there is so much more to learn!
  • You can type along, but I will be moving fast
  • Slides are available at gonsie.com/talks under "2022/ Introduction to Git"

Elsa Gonsiorowski

  • 2010 B.S. Physics and Computer Science from Rensselaer Polytechnic Institute (Troy, NY)
  • 2010,2011 Failed to get into any other graduate schools, so I stayed at RPI doing a PhD in CS
  • 2016 Ph.D. on Enabling Extreme-Scale Circuit Modeling Using Massively Parallel Discrete Event Simulation

At LLNL

  • 2016 Joined LLNL as HPC I/O Specialist in Livermore Computing
  • I knew enough about HPC file systems… because I had crashed one several times during grad school.
  • I work on Scalable Checkpoint Restart (SCR) library
  • I work in the LC Hotline

Raise Your Hand

  • If you can find the "Raise Hand" button on Webex
  • If you know someone else attending this lecture
  • If you've used "Track Changes" (MS Word or Google Docs)
  • If you know you have git installed on your system

2. What is Version Control

SC: Automated Version Control

VC Through Naming

phd101212s.png

VC in Parallel

versions-merge.drawio.svg

Key Features

  • Version control is unlimited undo (but not at a granular level)
  • Version Control allows many people to work in parallel

3. Configuring Git

SC: Setting Up Git

Configuring from the Command Line

git config --global user.name "Ada Lovelace"
git config --global user.email "ada@lovelace.io"
git config --global core.editor "emacs -nw"
git config --global init.defaultBranch main

Help with Config

git config --list
git config --help
cat ~/.gitconfig

Current Config ~/.gitconfig

[core]
    editor = emacs -nw
[init]
    defaultBranch = main
[user]
    name = Ada Lovelace
    email = ada@lovelace.io

4. Git Repositories

SC: Creating a Repository and SC: Tracking Changes

Assumptions

  • You are familiar with working on the command line
  • You know the commands:

    ls list files, with the flags -al
    cd change directory
    mkdir make directory
    echo repeat text
    > output redirection

Create a Repository

cd ~/Desktop
ls -al
mkdir Planets
cd Planets
git init
Initialized empty Git repository in /Users/gonsie/Desktop/Planets/.git/
ls -al
total        0
drwxr-xr-x   3  gonsiorowski1 59746  96 Jul 11 10:43 .
drwx------@ 16  gonsiorowski1 59746 512 Jul 11 10:43 ..
drwxr-xr-x   9  gonsiorowski1 59746 288 Jul 11 10:43 .git

Hint Use ls all the time

Query Git

git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Create a File

echo "Cold and dry, but everything is my favorite color" > mars.txt
ls -al
total        0
drwxr-xr-x   3  gonsiorowski1 59746     96 Jul 11 10:43 .
drwx------@ 16  gonsiorowski1 59746    512 Jul 11 10:43 ..
drwxr-xr-x   9  gonsiorowski1 59746    288 Jul 11 10:43 .git
-rw-r--r--   1  gonsiorowski1 59746     50 Jul 11 11:08 mars.txt
git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	mars.txt

nothing added to commit but untracked files present (use "git add" to track)

Hint Read git status

Track a File with Git

git add mars.txt
git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   mars.txt
ls -al
total        0
drwxr-xr-x   3  gonsiorowski1 59746  96 Jul 11 10:43 .
drwx------@ 16  gonsiorowski1 59746 512 Jul 11 10:43 ..
drwxr-xr-x   9  gonsiorowski1 59746 288 Jul 11 10:43 .git
-rw-r--r--   1  gonsiorowski1 59746  50 Jul 11 11:08 mars.txt

Git Staging Area

git-staging-area.svg

Commit Your Changes

git commit -m "Start of mars notes files"
[main (root-commit) a60f2b8] Start of mars notes file
 1 file changed, 1 insertion(+)
 create mode 100644 mars.txt
git status
On branch main
nothing to commit, working directory clean
git log
commit a60f2b8d99fe8e803695e7cfd37fc38b846125ad (HEAD -> main)
Author: Ada Lovelace <ada@lovelace.io>
Date:   Mon Jul 11 11:26:25 2022 -0400

    Start of mars notes file
ls -al
total        0
drwxr-xr-x   3  gonsiorowski1 59746  96 Jul 11 10:43 .
drwx------@ 16  gonsiorowski1 59746 512 Jul 11 10:43 ..
drwxr-xr-x   9  gonsiorowski1 59746 384 Jul 11 11:26 .git
-rw-r--r--   1  gonsiorowski1 59746  50 Jul 11 11:08 mars.txt

Change the File

echo "The two moons may be a problem for Wolfman" >> mars.txt
git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   mars.txt

no changes added to commit (use "git add" and/or "git commit -a")
git diff
diff --git a/mars.txt b/mars.txt
index df0654a..315bf3a 100644
--- a/mars.txt
+++ b/mars.txt
@@ -1 +1,2 @@
 Cold and dry, but everything is my favorite color
+The two moons may be a problem for Wolfman

Commit the Change

git commit -m "Wolfman on mars?"
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   mars.txt

no changes added to commit (use "git add" and/or "git commit -a")
git add mars.txt
git commit -m "Wolfman on mars?"
[main f43dbe4] Wolfman on mars?
 1 file changed, 1 insertion(+)
git log
commit f43dbe4f3e5f5d908500e587e117a998f750b3fe (HEAD -> main)
Author: Elsa Gonsiorowski (Pluto) <gonsie@me.com>
Date:   Mon Jul 11 11:37:54 2022 -0400

    Wolfman on mars?

commit a60f2b8d99fe8e803695e7cfd37fc38b846125ad
Author: Elsa Gonsiorowski (Pluto) <gonsie@me.com>
Date:   Mon Jul 11 11:26:25 2022 -0400

    Start of mars notes file
git show
commit f43dbe4f3e5f5d908500e587e117a998f750b3fe (HEAD -> main)
Author: Elsa Gonsiorowski (Pluto) <gonsie@me.com>
Date:   Mon Jul 11 11:37:54 2022 -0400

    Wolfman on mars?

diff --git a/mars.txt b/mars.txt
index df0654a..315bf3a 100644
--- a/mars.txt
+++ b/mars.txt
@@ -1 +1,2 @@
 Cold and dry, but everything is my favorite color
+The two moons may be a problem for Wolfman

5. Git

whew

Git Commands

git config --global Configure settings
git init Create .git directory
git status Query git about what's up
git add 'move' a file into git's staging area
git commit -m Create a commit with a message, add a group of changes to the repository
git log Show recent commits
git diff Show what is currently different
git show Detail last commit

Git Caveats

  • Git is powerful
  • Git has a terrible user design
  • Git is hard to fully learn by yourself
  • Git (like any other tool) takes time and practice to get good

6. Continue Your Journey

This is just the beginning

  • Distributed VC: Working with a remote person or server
  • Branching
  • Collaboration through "pull" or "merge" requests
  • Platforms: GitHub and GitLab

Get Social

  • Sign up for GitHub
    • Follow your friends, star your favorite (LLNL) projects
    • If you follow me this week I will follow you back
  • With permission: use GitHub to showcase projects
    • Get approval before posting LLNL code
    • Get permission before posting homework solutions

Git Lessons

Advanced Git Resources

7. Credits

The images and lesson structure were taken from the Software Carpentry: Git Novice course.

The presentation was created with Emacs, Org Mode, and RevealJS.

View the source.