Git Workshop

Simon Schug

26. April 2019

I. Introduction

Why are you here?

xkcd-1597

What is git?

git-logo

Version Control System for tracking changes in files.

  • Unlimited undo for your files using snapshots 📸️
  • Distributed repositories - online and offline 🌍️
  • See who made what changes when and why 👀️

Why do you need it?

phd_comics-1531

II. Setup

Linux 🐧️

You are nearly done 🍵️.

  1. Run the following command in a terminal:
sudo apt install git-all # Ubuntu / DEB-based
sudo dnf install git-all # Fedora / RPM-based
  1. Read a short history of git

Windows 🏢️

You have 2 options:

  1. Windows Subsystem for Linux (only Windows 10)

    • This will install a Linux distribution within Windows
    • You can use the Linux instructions to install git
  2. Use the standalone *.exe installer

    • Download latest source release for Windows
    • Step through the installer (keep defaults)

Mac OS 🍎️

You have 2 options:

  1. Use brew (preferred 👍️)

    • Install brew as outlined in section Install Homebrew
    • Enter brew install git into the terminal
    • Check if it works by entering git
  2. Use the *.dmg installer

    • Download latest source release for MacOS
    • Open the dmg and right-click open the *.pkg to install
    • Open a terminal & check if it works by entering git

Quick Bash Primer

Some useful commands to get started with Bash 🤖️

  • pwd to print the working directory
  • cd <folder> to change the working directory
  • ls to list files/folders
  • mkdir to make a new directory
  • nano <filename> as a simple text editor
  • man <command> reference manual

III. Version Control

Configuring git

Since this is your first time with git, tell it who you are 🙋️

git config --global user.name "Your Name"
git config --global user.email "youremail@email.com"

which text editor ✍️ you prefer

git config --global core.editor "nano"

and check ☑️ if everything is set up properly

git config --list

Creating a local repository

Creating a local git repository is easy 👯️

  1. Create a new directory for the repository
  2. Change into the newly created directory with cd
  3. Run git init to initialize the repository
  4. Check the status of the repository with git status

Where does git store its information?
Which files & folders will it track?

Tracking changes I

tracking-changes Commits with small changes are easier to read & review 💡️.

Tracking changes II

Let’s go through the modify-add-commit cycle 🌀️

  1. Create/Change files & folders within your new repository
  2. Add files/folders you want to track: git add filename
  3. Record changes as a commit:
    git commit -m "Meaningful commit message"
  4. Repeat 🔁️

Use git status to monitor the current status of your repository. What tricks does it tell you?

Ignoring things

Files you don’t want to track can be ignored 😑️

  • Create a .gitignore text file
  • Add files, folders/ or whole patterns *.dat
  • Exclude specific files from being ignored with !filename

For common use-cases prefabricated .gitignore files most likely already exist, e.g. check .gitignore.io.

Exploring history

How can you see the tracked changes 👁️?

  • git log shows the commit history of the repository
  • git diff shows changes within files
  • Use commit IDs to compare between specific commits

The most recent commit is referred to by the identifier HEAD. What does git diff HEAD~2 do?

Alternate realities I

tracking-changes Branch operations are inexpensive in git 💸️.

Alternate realities II

Exploring an idea without changing the main project 💫️

  • Use git branch <new-branch> to create a new branch
  • Switch to it with git checkout <new-branch>
  • Switch back to master and merge your changes with
    git merge <new-branch>

What happens when both the master and the branch change the same part of a file?

Conflicting realities

Merge conflicts can arise when the same file is changed in multiple branches ⚔️

  • Create a merge conflict between two branches
  • Resolve the conflict manually and look at the resulting graph with git log --graph
  • See Stackoverflow for information on mergetools

Avoiding conflicts

Resolving conflicts takes time, try to avoid them ✋️

  1. Modularize your files into smaller ones
  2. Make smaller and more atomic commits
  3. Use separate branches to segregate work

Time traveling

Oops I made some bad choices, how can I go back in time? 🕓️

  • Revert a single file (this deletes non-commited changes❗️) with git checkout <commit-id> <filename>
  • Time travel with all files using
    git checkout -b <new-branch> <commit-id>
  • Careful, don’t use git checkout <commit-id>. It will make you loose your HEAD

What is the detached HEAD state? How can you avoid it?

Basic git commands

git init        // Initialize local git repository
git status      // Check status of working tree
git add         // Add file(s) to staging area
git commit      // Commit changes to repository
git log         // Show commit logs
git diff        // Show changes between commits
git branch      // Create, list and delete branches
git checkout    // Switch branches or restore files

Get a more elaborate git cheat sheet from GitHub Help.

IV. Collaboration

Hosting services

To collaborate with others we need a remote 🌐️

  • GitHub is the de facto standard with a big community
  • GitLab is an open source alternative often preferred for self-hosted instances

Both offer many additional features like permission management, issue tracking, pull requests…

Setting up GitHub

We use GitHub for the demonstration 🐈️

Create a remote repository

Create a new, empty repository on GitHub 👶️

  • Connect your local repository to the new remote with
    git remote add origin <url>
  • Push your local changes to the remote with git push -u origin master
  • Create and edit the README file, it uses Markdown syntax

Repositories should contain a license to handle copyright. .choosealicense can help you.

Push & Pull

To synchronize changes in the local and the remote repository, you can use push & pull 🏋️

  • Change the README in the web interface and use
    git pull origin master to copy the changes into your local repository
  • Commit a local file change and push it to the remote using git push origin master

While git commit only updates your local repository, git push updates the remote with the local changes.

Pull requests

With pull requests, you can propose changes to a repository and ask someone to review, pull and merge your contribution 💌️

  • Create a new branch via the web interface and try to discover it locally with git fetch && git branch -r
  • Check out the new branch locally by creating a new local branch with the same name:
    git checkout -b <branch> <remote>/<branch>
  • Push local changes to the remote and perform a pull request via the web interface

Basic Collaborative Workflow

You are now ready to collaborate with others using git 🏁️

  1. Retrieve an existing, hosted repository with git clone
  2. Update your local repository with git pull
  3. Make local changes and stage them with git add
  4. Commit your changes with git commit
  5. Upload the changes to the remote with git push

+++Moooore

There is…

… so much more to discover 🔭️

  • Many text editors and IDEs have a deep git integration
  • GitHub/GitLab integrate many more tools to simplify collaboration: Issue tracker, wiki’s, website hosting
  • Numerous GUI’s try to streamline your git experience

You are prepared to discover those things on your own 🚀️