An Introduction to Git

47
An Introduction to Git Hiroyuki Vincent Yamazaki 2016-03-06

Transcript of An Introduction to Git

Page 1: An Introduction to Git

An Introduction to GitHiroyuki Vincent Yamazaki 2016-03-06

Page 2: An Introduction to Git

Target

• Version Control System (VCS) and Git beginners

• Developers using Git commands without knowing what’s actually happening

Page 3: An Introduction to Git

Goal

• Familiar with VCS

• Basic understanding of how Git operates

• Can use basic Git commands and know what they do to the filesystem

Page 4: An Introduction to Git

Content

• Version Control (VC)

• About Git

• Git in practice

Page 5: An Introduction to Git

What’s VC about?

Page 6: An Introduction to Git

- It’s about file history

Page 7: An Introduction to Git

> touch foo.js // Create a new file

… // Edit

> cp foo.js bar.js // Create a backup

… // Do something horrible

> cp bar.js foo.js // Revert to backup

Page 8: An Introduction to Git

File

Create foo.js

Time

Add Hello World

Remove unused functions

Clean up comments

The state of the file at a certain point in time

Current state

Page 9: An Introduction to Git

File

Current state

Time

The state of the file at a certain point in time

Create foo.js

Add Hello World

Remove unused functions

Clean up comments

Page 10: An Introduction to Git

File(s)Tim

e

A snapshot of the states of thefiles at a certain point in time

Current state

Page 11: An Introduction to Git

Git

Page 12: An Introduction to Git

Git

Git ( ) is a VCS, a program that is run from the CLI

> git <command>

Page 13: An Introduction to Git

First Git

Start a new project

> git init

Download an existing project

> git clone <url>

Page 14: An Introduction to Git

Overview

Page 15: An Introduction to Git

add

commit

checkout

Index / Staging

area

Localrepository

Local computer

Working directory

Page 16: An Introduction to Git

add

commit

checkout

Index / Staging

area

Localrepository

Local computer

Working directory

Files on your computer (file system)

Page 17: An Introduction to Git

Working directory

add

commit

checkout

Localrepository

Local computer

Set of files to be included in the next snapshot ( = snapshot )

Index / Staging

area

Page 18: An Introduction to Git

Index / Staging

area

Working directory

add

commit

checkout

Local computer

Database of snapshots

Localrepository

Page 19: An Introduction to Git

add

commit

checkout

Index / Staging

area

Localrepository

Local computer

Working directory

Page 20: An Introduction to Git

Add

Tell Git that a file is to be included in the next snapshot by adding it to the index / staging area

> git add foo.js

foo.js

Index / Staging area

Page 21: An Introduction to Git

CommitTake a new snapshot of the state of the files in the staging area (append it to the previous commit)

Store a snapshot in the local repository

> git commit

Previous commit

New commitfoo.js

Page 22: An Introduction to Git

References

Git only creates new files when a file is modified

Files are not stored in a commit, but rather references to files

foo.jsfoo.js

File foo.js Reference to foo.js

Page 23: An Introduction to Git

foo.js v1

bar.js v1

foo.js v2

bar.js v1

foo.js v1

foo.js v2

bar.js v1

Files (blob)

They point to the same old file

(Only references)

Repository Commithistory

Page 24: An Introduction to Git

Inside a Commit

Commit Reference (SHA-1 checksum) Author Date Message Reference to parent commit(s) Tree

Page 25: An Introduction to Git

Inside a Commit

Commit Reference (SHA-1 checksum) Author Date Message Reference to parent commit(s) Tree

Includes the references to all the files in the snapshot

Page 26: An Introduction to Git

add

commit

checkout

Index / Staging

area

Localrepository

Local computer

Working directory

Page 27: An Introduction to Git

Get the data from the repository into the working directory

Again, not the files but the references to the files from a commit

> git checkout <commit>

Checkout

Workingdirectory

Localrepository

Page 28: An Introduction to Git

Collaboration

Page 29: An Introduction to Git

Working directory

Index / Staging

area

Localrepository

Local computer

add

commit

checkout

Page 30: An Introduction to Git

Working directory

Index / Staging

area

Localrepository

Local computer

add

commit

checkout

Remoterepository

Hosted on a remote server

push

fetch

Page 31: An Introduction to Git

Remote Repository

A copy of the local repository, hosted on a network

Other people can join a project, by cloning its remote repository

Note: GitHub is a service that provides this remote repository hosting

Page 32: An Introduction to Git

Demo1. Create a new local repository

2. Create a file and make a new commit to the local repository

3. Associate the local repository with a remote repository

4. Push it to the remote repository

Page 33: An Introduction to Git

Branch

Page 34: An Introduction to Git

Branch

A branch is a pointer to a commit

master, is the name of the default branch

master

Page 35: An Introduction to Git

master

new feature

Initial commit

Example with 3 branches

Time

bug fix

Page 36: An Introduction to Git

master

new feature

bug fix

Initial commit

Example with 3 branches

Time

HEAD

A special type of branchthat always points to the currently active branch / commit Points to the master branch when a repository is initializedA checkout just changes the HEAD pointee

Page 37: An Introduction to Git

Demo Cont.1. Clone an existing repository

2. Create a new branch

3. Make a commit to the new branch

4. Switch between branches (checkout and change HEAD), see the changes in the local filesystem

5. Push the new branch to the remote repository

Page 38: An Introduction to Git

MergeMerge two commits together

Can be used to merge branches

May cause conflicts between commits that need to be resolved manually

// Merge <commit> into HEAD pointee

> git merge <commit>

Page 39: An Introduction to Git

A

B

master

develop

bug fix

Initial commit

Time

AB

Merge-commitof A and B

B

HEAD

Page 40: An Introduction to Git

Summary

Page 41: An Introduction to Git

Version Control

Series of commits

Page 42: An Introduction to Git

Three components Working directory

Index / Staging area Repository, local and remote

Commit Snapshot of file references (and other metadata)

Branch / Just a pointer to a commit

HEAD

Working directory

Index / Staging

area

Localrepository

Page 43: An Introduction to Git

Thank you for listening 🙇

Page 44: An Introduction to Git

More Git Features

Reset, --soft, --mixed, --hard

Revert

Fast-Forward / No Fast-Forward merge

Rebase

Cherrypick

Page 45: An Introduction to Git

Further Readings

Git Documentation https://git-scm.com/doc

Pro Githttps://git-scm.com/book/en/v2

Git Cheatsheet http://ndpsoftware.com/git-cheatsheet.html#loc=local_repo

Page 46: An Introduction to Git

Cheatsheet 💯> git init

> git status

> git branch [-a] [-v]

> git remote add <name> <url>

> git fetch

> git add <file>

> git commit -m “<message>”

> git push <repository> <branch>

> git pull <repository> <branch>

> git checkout [-b] <branch> -- <file>

> git log [--graph]

> git merge

> git --help

> git revert <commit>

> git reset --hard HEAD * Actually not even close to 100実は満点からはほど遠い

*

Page 47: An Introduction to Git

Install GitLinux > sudo apt-get install git-all

OS X Included in Xcode Command Line Tools

WindowsBinary installer

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git