A Gentle Introduction to Git


Agenda

  • Git and Distributed Version Control
  • Odd (Non-Code) Uses of Git
  • Learning How it Works
  • Git Hosting / Repos
  • Online Git Resources
  • Q&A

What is Git?

Git is a free and open source distributed version control system
https://git-scm.com/

Or if you ask the git man page

What is Version Control?

What is Distributed Version Control?

What is Distributed Version Control?

Non-Code Uses for Git

https://github.com/lemonade-hq/policy-2.0
https://github.com/getify/You-Dont-Know-JS#publishing
https://github.com/sindresorhus/ama/issues
https://github.com/steeve/france.code-penal

The Standalone User


Getting Started and Adding Files

Starting with Local Files

Let's create a git repository

# git init

Starting with Local Files

From here we can add files to our repo

# git add file

And we can check the status of our repo

# git status

Tracked vs Untracked Files

Tracked files are those in your repo, while untracked files have not yet been added to the repo.

Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   index.html

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

	images/cat-delete.png
	images/hoax.png
	images/moscow.JPG
	images/pexels-photo-428430.jpeg

no changes added to commit (use "git add" and/or "git commit -a")

Adding to the Repo

We need to add files to the repo so they can be tracked.

# git add index.html
# git add .

Committing to the Repo

Don't be afraid to commit....to the local repo

# git commit index.html
# git commit -a
# git commit -a -m "Message Goes Here"

Checking the Logs

We can take a look at that log

# git log
commit 35e4b1b535c148a703911ca7f07e25f7eaf57356 (HEAD -> master)
Author: Alex 
Date:   Fri Jun 8 13:21:01 2018 -0500

    Adding the images

commit c3beb7ed5ddd6463e98e8988c030ad7832165220 (origin/master)
Author: Alex 
Date:   Wed May 23 16:27:50 2018 -0500

    Updated files

Checking the Logs II

# git log --oneline
35e4b1b (HEAD -> master) Adding the images
c3beb7e (origin/master) Updated files
b62f3b9 Added the images
6dca960 Initial Commit
a2e69a4 Merge pull request #2097 from bnjmnt4n/docs/readme

Check Point Slide

Adding Files to a Git repo
  • git init
  • git add .
  • git commit -a -m "Commit Message"

The Standalone User


The ctrl-z of Git: Reverting Back

Reverting Changes

One of the big benefits of of version control is being able to roll back to a previous version of a file.

With the diff and checkout commands we see what is different and revert back.

The Difference Between Files

# git diff < FILE >
# git diff b62f3b9 6dca960 --oneline < FILE >

Reverting a File

# git checkout file

Check Point Slide

Reverting to an Old Version
  • git diff
  • git checkout
  • git commit -a -m "Commit Message"

Git Repo Hosting

GitHub, GitLab, BitBucket etc

So What is GitHub?

GitHub, GitLab, BitBucket and all the like are web based repository hosting platforms. These platforms offer up a way to share, collaborate and contribute code.

Getting "Social"


Share, Collaborate and Contribute

Git Clone

# git clone git@gsome-url-project.com/gentle-git-intro.git

Checking the Remotes

When we work with a hosted repo we have a remote URL we push to and fetch from.

$ git remote -v
origin	git@some-url/repo.git (fetch)
origin	git@some-url/repo.git (push)

Pushing to the Remote(s)

Pushing is syncing your changes and commit logs with a remotely hosted repository.

# git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 830 bytes | 830.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To gitlab.unsupported.io:mralexjuarez/gentle-git-intro.git
   b8516bd..85c3079  master -> master

Pulling from the Remote(s)

We can use the fetch or the pull command to bring down content from the remote repository

# git fetch
or
# git pull

Fetch vs Pull

A fetch will pull down remote references and commits but not update the local repository.

A pull will both fetch the remote references and merge the new content in the current branch.

Check Point Slide

Once we want to sync our changes with the remote repository.

  • git push - sync our changes back to the remote repository
  • git pull - to bring down those changes from the remote

Shameless Plug

Setting Up GitLab for Fun and Profit @ 1:00PM

So There You Have It

This should be a good start. There is still A LOT of git to explore but I'll leave that up to the reader.

Git Resources

Contact Information

Alex Juarez
GTS Linux Principal Engineer at Rackspace
twitter: @mralexjuarez

This hCard created with the hCard creator.

Q & A

Or we can talk about whiskey

Cat Tax!