Introduction to Git

Getting started with version control workflows

Unity Cluster
Research Computing

Overview

Why git?

Why use version control?

  • General

    • Code evolves over time

    • Identify when bugs were introduced

    • Easily work with others

  • Research

    • Essential for reproducibility

    • Save time for you and collaborators

Centralized vs Distributed

  • RCS — Revision Control System

  • CVS — Concurrent Versions System

  • SVN — Subversion

  • Git

  • Mercurial

  • Darcs

Why git?

  • Need to choose something

  • Git has become de facto version control software

  • Most popular version control software

Initialize your git repository

Configure git

Tell git your name and email.

Global:

$ git config --global user.name "John Doe"
$ git config --global user.email "john.doe@example.com"

Local (per-project):

$ git config user.name "John Doe"
$ git config user.email "john.doe@example.com"

Configure git (continued)

Set your default text editor:

$ git config --global core.editor "<your editor here>"

Verify git configuration

List the current settings:

$ git config --list

Git status

Initialize your repository

Create a new git repository

$ mkdir example-repository
$ cd example-repository
$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/solj/git/example-repository/.git/

Add a new file

$ touch foo
$ git status
On branch master

No commits yet

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

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

Add a new file (continued)

$ git add foo
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   foo

Add our first commit

$ git commit -m "Initial commit" -sv
[master (root-commit) 7723f4f] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo

Commit messages

  • Why is a commit message important?

  • What is a good commit message?

  • Learn more about commit messages[1]

Commit messages (continued)

Atomic Commits

Short and Unambiguous

Active Voice

Example (Good)
Add defer_batch to handle large job influx
Example (Bad)
Added feature ...

Commit messages (continued)

Subject line (try to keep under 50 characters)

Multi-line description of commit,
feel free to be detailed.

[Ticket: X]

Other example commit structures:

Visualize the current status

initial status

Adding a second new file

$ touch bar
untracked file status

Modify an existing file

$ echo "foobar" >> foo
modify existing file status

Add files to staging area

$ git add foo
add file to staging area1

Add files to staging area (continued)

$ git add bar
add file to staging area2

Remove files from staging area

$ git restore --staged bar
remove file from staging area

Git stash

Learn about the stash

What is the git stash?

The git stash command provides a way to save your changes (temporarily) and get a clean working directory.

Why use the stash?

The stash is most useful when multitasking.

  • Not ready (yet) to commit your changes

  • Need to switch branches to work on a bugfix but don’t want to lose your work

Use the stash

$ git stash  # or git stash push
Saved working directory and index state WIP on master: 7723f4f Initial commit
git stash

Show the most recent stashed changes

$ git stash list
stash@{0}: WIP on master: 7723f4f Initial commit
$ git stash show
 foo | 1 +
 1 file changed, 1 insertion(+)
$ git stash show -p
diff --git a/foo b/foo
index e69de29..323fae0 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+foobar

Apply the most recent stashed changes

$ git stash pop
On branch master
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:   foo

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

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (3da3bc9570fe3ec91bf73837fd8e03cc4476479f)

Apply the most recent stashed changes (continued)

git stash pop

Commit changes

$ git add bar foo
git add bar and foo

Commit changes (continued)

$ git commit -m "Add foo and bar" -asv
git commit bar and foo

Git file operations

Move/rename existing file

$ git mv bar baz
git mv bar baz

Move/rename existing file (continued)

$ git commit -m "Move bar to baz" -sv
git commit baz

Amend existing commit

$ git commit --amend -m "Rename bar to baz" -sv
git amend baz

Remove existing file

$ git rm baz
git rm baz

Remove existing file (continued)

$ git commit -m "Remove baz" -sv
git commit rm baz

Revert existing commit

$ git revert HEAD
git revert head

Follow Up & Contact

Keep in touch! To contact us: