Intro to Git & GitHub

DH Infrastructure Symposium, 2017

What we'll cover...

  • Introduction to Git
  • Basic Git commands & workflow
  • Introduction to GitHub
  • Basic GitHub workflows
  • Useful Git & GitHub resources

Git...

  • System for distributed, decentralized version control
  • Facilitates collaboration and non-linear development
  • Each "clone" repository is complete - full history, logs, etc.
  • Every "clone" is a backup
  • Can access via command line or GUI

Distributed version control?

  • Compare changes to files over time.
  • Keep a backup of earlier versions of files.
  • Revert to earlier versions of files.
  • Safely experiment.

Version control systems track changes to a file or files over time. This means you can:

Decentralized...

Decentralized...

Understanding Git

Repository

  • The data structure Git uses to record everything 
  • Stored in the same directory as the project itself, in a hidden .git subdirectory
  • Once a repository is initialized, Git will track all changes in the project directory

Also called a 'repo'

Commit

  • The files themselves 
  • The directory structure
  • A commit object (the tree, the parent commit hash, the author and date of the commit, the commit message)
  • A SHA key - a 40 character string that uniquely identifies the commit.

A snapshot of the state of a project at a point in time. It includes:

Remote

Collaborating with others (and managing your own code/files on GitHub) involves managing these remote repositories - 'pushing' and 'pulling' code from your machine to and from these remotes.

A remote repository, or 'remote', is a version of your project stored on the internet, or a network, or anywhere other than your machine, to which your local repo is linked. This remote repository is often named 'origin.'

Three Git States

  • Modified: You've made changes to files or the directory, but haven't staged them yet.
  • Staged: Git takes a snapshot of the modified files and places them in a 'staging area' to be included in the next commit. 
  • Committed: Git takes the snapshots of the files in the staging area and commits them to the repository.

Working spaces...

Remote

Local

Upstream

Repository

Local

Repository

Index

(Staging)

Workspace

Stash

Working with branches is good!  Branches allow you to work away from the main line of development.

Branches

Let's learn some Git commands!

git init

Initialize a new local git repository

$ git init starfish
Initialized empty Git repository in /Users/kirschbombe/Sites/
starfish/.git/
$ 

git add

Add a file to the staging area

$ git add rays.txt

OR

$ git add .

git commit

Add files from the staging area to your local repository

$ git commit -m "add rays.txt"
[master (root-commit) f9d6e54] add rays.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 rays.txt
$

git status

Shows the current status of your repository files (unstaged & staged)

$ git status
On branch master

Initial commit

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

	new file:   rays.txt

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

	test.txt

$ 

rm & mv

$ git rm my-first-file
rm 'my-first-file'

$ git status
On branch my-new-feature
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    my-first-file
$ git mv my-first-file other-name

$ git status
On branch my-new-feature
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	renamed:    my-first-file -> other-name
$ git log
commit 5e31a9080c7f0a9b0390cd7dd2aa8e59a28a7328
Author: Yves Laroche <yves.laroche@logicnow.com>
Date:   Tue Feb 2 20:02:08 2016 +0000

    My first commit

# Short
$ git log --oneline
5e31a90 My first commit

# Limit number of commits
$ git log -10
$ git log --since=1.week
$ git log --after='2016-01-10'

git log

History of commits sent to the repository

$ cat starfish/.gitignore
# Ignore modules, themes, vendors
/core
/modules
/themes
/vendor

# Ignore config files
sites/*/settings*.php
sites/*/services*.yml

# Ignora private files
sites/*/files
sites/*/private

.gitignore

Files that git should ignore

Git workflow

Hands-on with Git (1): A new Git repo

We'll do this in our Cloud 9 workspace

  1. Make sure you are not in a git repo: git status
  2. Initialize a git repo:  git init [repo_name]
    or you can create/navigate to an existing directory and invoke git init from inside
  3. Add files, delete files, edit files
  4. Stage your changes:  git add . or git add [filename]
  5. Commit your changes:  git commit -m "commit message"
  6. Repeat 3-5
  7. View your commit history: git log

Working with branches

git branch

List, create, name, and delete branches.

$ git branch develop

Create a new branch named develop

$ git branch
  develop
* master

List all branches

git checkout

Switch to another branch, like the one we just created using git branch

$ git checkout develop
Switched to branch 'develop'

$ 

git checkout -b

Create and switch to a new branch. 

$ git checkout -b feature
Switched to a new branch 'feature'
$

The same as git branch feature + git checkout feature

git merge

Merge one branch into another

$ git checkout master

To merge the branch "feature" into the branch "master":

$ git merge --no-ff feature

Hands-on with Git (2): Branches

We'll do this in our Cloud 9 workspace

  1. Check to see which branch you are on: git status
  2. List all branches: git branch
  3. Create a new branch: git branch [new_branch]
  4. List your branches again
  5. Switch to your new branch: git checkout
  6. Make some changes in your new branch and commit
  7. Switch back to branch master: git checkout master
  8. Let's take a look at our files and folders - see how the file system reflects your current branch?
  9. Merge changes from your new branch into master: git merge [new_branch]

GitHub...

  • Cloud-based git repository hosting service
  • Free for public repositories
  • Features: documentation, bug tracking, task management and other collaboration tools
  • gh-pages for hosting web content

Create repository

It's easy to create a new repo in GitHub. There are two options:

Create a new project in GitHub

 ~ OR ~

Create a new repo in GitHub as the remote for an existing local project

Fork an existing repo

Forking a repo results in a clone of someone else's repo in your GitHub account. The two remain connected, so you can pull from 'upstream' or send 'pull requests' to the upstream repo. 

Origin

Your 'fork' of the original repository. A clone of the original repository that lives on GitHub.

Upstream

The original repository (if applicable)

The local repository on your computer will usually have (at least) two remotes: an origin and an upstream.

Pull request

Pull requests send a message to the 'upstream' asking to merge changes from your repo into theirs. This give the repo owner control over what changes will be accepted, while allowing others to contribute.

Hands-on with GitHub (1): Fork & Pull

We'll do this in GitHub

  1. Login to GitHub
  2. Create a new repository and share the URL with your partner - don't forget the Readme!
  3. Fork your partner's repo
  4. Let's make a few changes to our new forked repos (add files, edit files, etc.)
  5. Submit a pull request
  6. Now, check the repo that you created for the pull request
  7. You can view the changes and accept or close the pull request

Remote repositories

Remotes

  • Remote repo that is synched with our local repo
  • Necessary for working collaboratively
  • Push/pull the data from the remote repo to the local repo

Local

Remote (origin)

pull

push

Remember the working spaces...

Remote

Local

Upstream

Repository

Local

Repository

Index

(Staging)

Workspace

Stash

git clone

Create a copy of a git repo

$ git clone git@git.bcash.com.br/bcash/api.git

git push

Push commits from local repo to remote repo

$ git push origin master

git fetch

Downloads the objects and refs (branches & tags) from the remote to the local repo

Fetch does not merge the new commits from the remote.

$ git fetch origin

git pull

Imports remote commits to local repo and merges into working directory

$ git pull

git pull is the same as:

$ git fetch origin
$ git merge

git push

Push commits from local repo to the remote

$ git push origin master

git remote

Specifies a remote for your local repo to track.

$ git remote add origin https://git.bcash.com.br/fabio.nowaki/teste.git

With no arguments, it will show a list of existing remotes.

$ git remote
origin
$

Hands-on with Git (3): Cloning a repo

We'll do this in our Cloud 9 workspace

  1. Go back to the Home folder (and make sure you are not in a git repo with git status)
  2. Copy the Git URL of the forked repo (in GitHub)
  3. Clone the repo:   git clone [url_from_github]
  4. Modify, stage, commit
  5. Let's push our changes to 'origin': git push origin master
  6. Let's see what's happened in GitHub...
  7. Make a new change in GitHub, then a new change in local
  8. Push to origin. What happens?
  9. We'll need to pull the changes from origin, then push...

gh-pages

  • Host web pages in GitHub (HTML5, Jekyll, etc.)
  • Development and hosting in one location
  • Can use own domain
  • Just place web files in a branch named 'gh-pages' or in the /docs folder in master

If time, we can publish a simple web page with the web-page generator.

Resources

Questions?

dchildress@library.ucla.edu

@kirschbombe

Intro to Git/GitHub

By Dawn Childress

Intro to Git/GitHub

Git/Github workshop for the 2017 DH Infrastructure Symposium

  • 55