Git 101
concepts
GIT 101
Git 101:
Concepts
repository
// Contains all of the project files
// Stores each file's revision history
// Can have multiple collaborators
// Can be either public or private
// There will be multiple copies
// There is usually one central “master” copy
Git 101:
Concepts
Submodule
// A Git repository that is nested within another repository
// It's codebase is managed independently of its
container's code
// Does not automatically upgrade
Git 101:
Concepts
branch
// A parallel version of a repository
// Does not affect the primary master branch
// Publish your changes by merging the branch
back into the master branch
Git 101:
Concepts
remote
// The version of the
repository that is hosted on
a remote server (GitHub)
// Local repository is
connected to the remote
so that changes can
be synced
Git 101:
Concepts
Working Directory
// Holds the current checkout of the files
you are working on
// Files in will be removed or replaced by Git
as you switch branches
Git 101:
Concepts
Index
// A staging area
// Holds snapshots of changes are before they get
committed to the repository
Git 101:
Concepts
Commit
// A snapshot of the tracked contents of your
working directory
// Records what changes were made when and by who
// Tracked using a unique 40 character SHA1 hash
Git 101:
Concepts
Commit
Git 101:
Concepts
TAGs
// Typically used to mark a particular commit using
a memorable name
Git 101:
Concepts
.gitignore
// Specify which files and directories Git should ignore
// Committed into repository in order to share ignore
rules with other users that clone the repository
Git 101:
Concepts
.gitignore
# Files
.DS_Store
error_log
# File types
*.log
# Directories
node_modules/
wp-content/cache/
# Ignore everything in wp-content except plugins and themes
wp-content/*
!wp-content/plugins/
!wp-content/themes/
Installation
GIT 101
Git 101:
Installation
Installation
$ git --version
Git 101:
Installation
Configuration
// Global configuration
[user]
name = Bill Swath
email = user@example.com
[color]
ui = true
[core]
excludesfile = /Users/swath/.gitignore_global
filemode = false
editor = subl -n -w
~/.gitconfig
Git 101:
Installation
Configuration
// Global configuration
$ git config --global user.name "[name]"
$ git config --global user.email "[email address]"
# Colorful output
$ git config --global color.ui auto
# Don't track file permission changes
$ git config --global core.filemode false
Git 101:
Installation
Configuration
// Project configuration
[core]
...
[remote "origin"]
url = git@github.com:VitalDevTeam/Skeleton.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[submodule "wp"]
url = https://github.com/WordPress/WordPress.git
project/.git/config
Authentication
GIT 101
Git 101:
Authentication
SSH Key
// Identify trusted computers and servers without
using passwords
// Required to push changes to remote
// Required to clone private repositories
Basic commands
GIT 101
Git 101:
BASIC COMMANDS
init
$ git init
Initialized empty Git repository in /Users/vitaldesign/Dropbox/GitHub/test/.git/
$ ls -alh
total 0
drwxr-xr-x@ 3 vitaldesign 501 102B Jul 9 12:19 ./
drwxr-xr-x@ 25 vitaldesign 501 850B Jul 9 12:19 ../
drwxr-xr-x@ 10 vitaldesign 501 340B Jul 9 12:19 .git/
Create a new repository locally
Git 101:
BASIC COMMANDS
clone
$ git clone git@github.com:VitalDevTeam/Nucleus.git nucleus
Cloning into 'nucleus'...
remote: Counting objects: 1548, done.
remote: Total 1548 (delta 0), reused 0 (delta 0), pack-reused 1548
Receiving objects: 100% (1548/1548), 1.17 MiB | 1.79 MiB/s, done.
Resolving deltas: 100% (915/915), done.
Checking connectivity... done.
Clone existing repository locally
Git 101:
BASIC COMMANDS
clone
SSH URL
Requires SSH key
HTTPS URL
Asks for password on every push
Git 101:
BASIC COMMANDS
clone --recursive
$ git clone --recursive git@github.com:VitalDevTeam/Skeleton.git skeleton2
Cloning into 'skeleton2'...
remote: Counting objects: 458, done.
remote: Total 458 (delta 0), reused 0 (delta 0), pack-reused 458
Receiving objects: 100% (458/458), 877.96 KiB | 0 bytes/s, done.
Resolving deltas: 100% (190/190), done.
Checking connectivity... done.
Submodule 'wp' (https://github.com/WordPress/WordPress.git) registered for path 'wp'
Cloning into 'wp'...
remote: Counting objects: 208388, done.
remote: Compressing objects: 100% (179/179), done.
remote: Total 208388 (delta 106), reused 0 (delta 0), pack-reused 208209
Receiving objects: 100% (208388/208388), 130.03 MiB | 2.49 MiB/s, done.
Resolving deltas: 100% (164374/164374), done.
Checking connectivity... done.
Submodule path 'wp': checked out '0b63a9e4b66eff7826bd7319ef767d24d9b55775'
Clone existing repository and all submodules locally
Git 101:
Basic Commands
add
# Add individual files
$ git add <filename> <filename> <filename> ...
# Add all files that have been changed
$ git add -u
# Add all files that have been changed AND new files
$ git add -A
Add your local changes to the staging area (index)
Git 101:
Basic Commands
add -p
Add local changes to the staging area in hunks instead of whole files
Git 101:
BASIC Commands
status
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: bomp.php
no changes added to commit (use "git add" and/or "git commit -a")
Shows the differences between your working directory, the index, and previous commits
Git 101:
Basic Commands
commit
$ git commit -m 'Added awesome widget'
[master 68aa034] Added awesome widget
1 files changed, 2 insertions(+), 1 deletions(-)
Takes the staged snapshot in the index and commits it to the project history
Commit a lot as you go!
Git 101:
Basic Commands
commit -m
Commit message best practices:
// First line should be 50 characters or less
// Second line should be blank
// Omit the -m flag if you need to write more
than 50 characters
// Describe what the commit does to the repo and
not what YOU did
Git 101:
Basic Commands
commit
Adds FAQs
Adds new custom post type plugin for FAQs
as well as 2 content block layouts for the FAQs.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: foo
#
Good example of a longer commit message
Git 101:
Basic Commands
commit
Change your default editor in Git so you can write commit messages easily:
On remote servers where you can't use apps:
$ git config --global core.editor "nano"
Git 101:
Basic Commands
push
$ git push origin master
Counting objects: 25, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (25/25), 2.43 KiB, done.
Total 25 (delta 4), reused 0 (delta 0)
To git@github.com:foo/bar.git
* [new branch] master -> master
Send your committed changes to a remote repository
Git 101:
Basic Commands
fetch
// Get the latest changes from the
remote repository without merging them
// Changes can be compared to your local copy
$ git fetch origin
remote: Counting objects: 4006, done.
remote: Compressing objects: 100% (1322/1322), done.
remote: Total 2783 (delta 1526), reused 2587 (delta 1387)
Receiving objects: 100% (2783/2783), 1.23 MiB | 10 KiB/s, done.
Resolving deltas: 100% (1526/1526), completed with 387 local objects.
From github.com:foo/bar
8e29b09..c7c5a10 master -> origin/master
Git 101:
Basic Commands
pull
Fetch and merge changes from the remote repository to your local copy
Git 101:
Basic Commands
log
View the history of a repository
# View verbose commit history
$ git log
# View simple, one line commit history
$ git log --pretty=oneline
# View commit history with graphical branch chart
$ git log --name-status
BRANCHES
GIT 101
Git 101:
Branches
branch Model
Git 101:
BRANCHES
CREATING a BRANCH
$ git branch <branch>
Create a new branch
Git 101:
BRANCHES
Switch Branch
$ git checkout <branch>
Switch to a branch
Git 101:
BRANCHES
PUSH Branch
$ git push origin <branch>
Push branch to remote so others have access to it
Git 101:
BRANCHES
Merge Branch
$ git merge <branch>
Merge branch into current
Git 101:
BRANCHES
Merge Conflicts
// 2 people make edits on the same line
// One person deletes a file that another was
working on
// 2 people add a file with the same name
REFERENCES
Quick Reference Guide
http://rogerdudler.github.io/git-guide/
More Comprehensive Tutorial
https://www.git-tower.com/learn/git/ebook
Interactive 15-Minute Basic Tutorial
Interactive Branching Tutorial
http://pcottle.github.io/learnGitBranching
Git 101
By Adam Walter
Git 101
The basics of using Git
- 1,270