0% found this document useful (0 votes)
1K views

Git Real Slides

The document provides an overview of Git basics including version control systems, repositories, working with Git, staging and commits, undoing commits, remotes and sharing projects. Key topics covered include initializing and configuring a local Git repository, tracking and committing files, viewing the commit history and differences between versions, and pushing and pulling from remote repositories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Git Real Slides

The document provides an overview of Git basics including version control systems, repositories, working with Git, staging and commits, undoing commits, remotes and sharing projects. Key topics covered include initializing and configuring a local Git repository, tracking and committing files, viewing the commit history and differences between versions, and pushing and pulling from remote repositories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 156

GIT BASICS

LEVEL 1

SAME OLD STORY

! s e u s s I n o i t a r o b a l l Co
LEVEL 1 GIT BASICS

VERSION CONTROL SYSTEMS


Merging
Ti e l u s p a C e m

REPOSITORY LOCATIONS
Central Repository
Distributed Repository

LEVEL 1 GIT BASICS

Git is a

DISTRIBUTED VERSION CONTROL SYSTEM (DVCS)

ORIGINS
Linux Kernel project.
Meant to be distributed, fast and more natural. Capable of handling large projects.

LEVEL 1 GIT BASICS

WORKING WITH GIT


Command line interface
Many GUI tools available O!cial Git Site http://git-scm.com/

start here
LEVEL 1 GIT BASICS

GIT HELP
$ git help usage: git [--version] [--exec-path[=<path>]] [--html-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [-c name=value] [--help] <command> [<args>] The most commonly used git commands are: add Add file contents to the index bisect Find by binary search the change that introduced a bug ...

LEVEL 1 GIT BASICS

GIT HELP
$ git help config GIT-CONFIG(1) Git Manual GIT-CONFIG(1)

pass in any git command


NAME git-config - Get and set repository or global options SYNOPSIS git config [<file-option>] [type] [-z|--null] name [value [value_regex]] git config [<file-option>] [type] --add name value ...

LEVEL 1 GIT BASICS

SETTING UP GIT
$ git config --global user.name "Gregg Pollack"

Who gets credit for changes What email you use Pretty command line colors

$ git config --global user.email [email protected] $ git config --global color.ui true

LEVEL 1 GIT BASICS

STARTING A REPO
$ mkdir store $ cd store $ git init Initialized empty Git repository in /Users/gregg/store/.git/

git metadata is stored here

LEVEL 1 GIT BASICS

GIT WORK FLOW


Jane creates README.txt "le

Starts as untracked
Add "le to staging area

Getting ready to take a picture


Commit changes

A snapshot of those on the stage


LEVEL 1 GIT BASICS

GIT WORK FLOW


Jane creates README.txt "le Add "le to staging area Commit changes Jane modi"es README.txt "le & adds LICENSE Add both "les to staging area Commit changes
LEVEL 1 GIT BASICS

Jane creates README.txt "le


$ git status

# On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # Our newly created file # README.txt nothing added to commit but untracked files present (use "git add" to track)

To check whats changed since last commit

LEVEL 1 GIT BASICS

Add "le to staging area


$ git add README.txt $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README.txt #

Our staged file

LEVEL 1 GIT BASICS

Commit changes

Commit message what work was done?

timeline

$ git commit -m "Create a README." [master abe28da] Create a README. 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 README.txt

master
$ git status # On branch master nothing to commit (working directory clean)

No new or modified files since last commit


LEVEL 1 GIT BASICS

Jane modi"es README.txt "le & adds LICENSE


$ git status # On branch master # Changed but not updated: # # modified: README.txt # # Untracked files: # # LICENSE no changes added to commit

master

LEVEL 1 GIT BASICS

Add both "les to staging area


$ git add README.txt LICENSE

master

OR
$ git add --all $ git status # On branch master # Changes to be committed: # # new file: LICENSE # modified: README.txt #
LEVEL 1 GIT BASICS

Adds all new or modified files

Commit changes
$ git commit -m "Add LICENSE and finish README." [master 1b0019c] Add LICENSE and finish README. 2 files changed, 21 insertions(+), 0 deletions(-) create mode 100644 LICENSE

master

LEVEL 1 GIT BASICS

GIT TIMELINE HISTORY


$ git log commit 1b0019c37e3f3724fb2e9035e6bab4d7d87bf455 Author: Gregg Pollack <[email protected]> Date: Thu Jul 5 22:31:27 2012 -0400 Add LICENSE and finish README. commit 5acaf86b04aaf9cbbb8ebb9042a20a46d0b9ce76 Author: Gregg Pollack <[email protected]> Date: Thu Jul 5 22:00:46 2012 -0400 Create a README.

master

LEVEL 1 GIT BASICS

DIFFERENT WAYS TO ADD


$ git add <list of files> $ git add --all $ git add *.txt $ git add docs/*.txt $ git add docs/ $ git add "*.txt"

Add the list of files Add all files Add all txt files in current directory Add all txt files in docs directory Add all files in docs directory Add all txt files in the whole project

LEVEL 1 GIT BASICS

STAGING & REMOTES


LEVEL 2

GIT DIFF
LICENSE
Copyright (c) 2012 Envy Labs LLC ... Permission is hereby granted, free of charge, to any person obtaining a copy

LICENSE

edit

Copyright (c) 2012 Code School LLC ... Permission is hereby granted, free of charge, to any person obtaining a copy

Show unstaged $ git diff diff --git a/LICENSE b/LICENSE index 7e4922d..442669e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012 Envy Labs LLC +Copyright (c) 2012 Code School LLC

differences since last commit

Line removed Line added

VIEWING STAGED DIFFERENCES


$ git add LICENSE $ git diff $ git diff --staged

No differences, since all changes are staged View staged differences

diff --git a/LICENSE b/LICENSE index 7e4922d..442669e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012 Envy Labs LLC +Copyright (c) 2012 Code School LLC

LEVEL 2 STAGING & REMOTES

UNSTAGING FILES
$ # # # # # # $

Unstage Tip git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
modified: LICENSE

master
HEAD

Refers to last commit

git reset HEAD LICENSE

Unstaged changes after reset: M LICENSE

LEVEL 2 STAGING & REMOTES

DISCARD CHANGES
$ git status
# On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: LICENSE #

$ git checkout -- LICENSE $ git status

Blow away all changes since last commit

nothing to commit (working directory clean)


LEVEL 2 STAGING & REMOTES

SKIP STAGING AND COMMIT


Readme.txt
here is my readme and now I can sleep

Readme.txt

master
HEAD

edit

here is my readme the cake is a lie

Add changes from all tracked files


$ git commit -a -m "Modify readme" [master d00fefc] Modify readme 1 files changed, 1 insertions(+), 1 deletions(-)

Doesnt add new (untracked) files


LEVEL 2 STAGING & REMOTES

UNDOING A COMMIT
Whoops, we forgot something on that commit.

Reset into staging


$ git reset --soft HEAD^ $ # # # # # # git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README.txt

master
HEAD

Move to commit before HEAD

Now I can make changes, and re-commit


LEVEL 2 STAGING & REMOTES

ADDING TO A COMMIT
Maybe we forgot to add a !le

Add to the last commit


$ git add todo.txt

master

New commit message

HEAD

$ git commit --amend -m "Modify readme & add todo.txt." [master fe98ef9] Modify readme and add todo.txt. 2 files changed, 2 insertions(+), 1 deletions(-) create mode 100644 todo.txt

Whatever has been staged is added to last commit


LEVEL 2 STAGING & REMOTES

USEFUL COMMANDS
$ git reset --soft HEAD^

Undo last commit, put changes into staging Change the last commit

$ git commit --amend -m "New Message" $ git reset --hard HEAD^

Undo last commit and all changes Undo last 2 commits and all changes

$ git reset --hard HEAD^^

LEVEL 2 STAGING & REMOTES

HOW TO SHARE?
Remote Repository

push
master

pull
pull

git remote command Git doesnt take care of access control


LEVEL 2 STAGING & REMOTES

REMOTE REPOSITORY HOSTING


Hosted Self Managed Gitosis Gitorious

GitHub BitBucket

push
master
LEVEL 2 STAGING & REMOTES

LEVEL 2 - STAGING & REMOTES

LEVEL 2 - STAGING & REMOTES

ADDING A REMOTE
$ git remote add origin https://github.com/Gregg/git-real.git

New remote

our name for this remote show remote repositories

address

origin

$ git remote -v origin https://github.com/Gregg/git-real.git (fetch) origin https://github.com/Gregg/git-real.git (push)

LEVEL 2 STAGING & REMOTES

PU SHING TO R EMOTE
remote repository name
$ git push -u origin master Username for 'https://github.com': Gregg Password for 'https://[email protected]': Counting objects: 11, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (11/11), 1.50 KiB, done. Total 11 (delta 0), reused 0 (delta 0) To https://github.com/Gregg/git-real.git * [new branch] master -> master

local branch to push

push
master

origin

Password caching

https://help.github.com/articles/set-up-git

LEVEL 2 - STAGING & REMOTES

Same information from git log

LEVEL 2 - STAGING & REMOTES

PU LLING FROM REMOTE


To pull changes down from the remote
$ git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1) Its Unpacking objects: 100% (3/3), done. From https://github.com/Gregg/git-real fe98ef9..4e67ded master -> origin/master Updating fe98ef9..4e67ded Fast-forward todo.txt | 1 + 1 file changed, 1 insertion(+)
LEVEL 2 STAGING & REMOTES

pull
origin

good

n e t f o s i h t o to d

HAVING MULTIPLE REMOTES

origin

production

test
LEVEL 2 STAGING & REMOTES

WORKING WITH REMOTES


To add new remotes
$ git remote add <name> <address>

To remove remotes
$ git remote rm <name>

To push to remotes
$ git push -u <name> <branch>

usually master
LEVEL 2 STAGING & REMOTES

HEROKU REMOTE
$ heroku create Creating dev-server-1426... done, stack is cedar http://dev-server-1426.herokuapp.com/ | [email protected]: dev-server-1426.git Git remote heroku added $ git remote -v heroku [email protected]: dev-server-1426.git (fetch) heroku [email protected]: dev-server-1426.git (push) origin https://github.com/Gregg/git-real.git (fetch) origin https://github.com/Gregg/git-real.git (push)

git repo ssh address

To push to heroku
$ git push heroku master
LEVEL 2 STAGING & REMOTES

triggers deploy

USEFUL COMMANDS
$ git reset --soft HEAD^

Dont do these after

you push

$ git commit --amend -m "New Message" $ git reset --hard HEAD^

$ git reset --hard HEAD^^

LEVEL 2 STAGING & REMOTES

CLONING & BRANCHING


LEVEL 3

COLLABORATING
I want a copy Clone the repo!

g g e Gr
push
local repo

Jane
github
?

How do we start collaborating?


LEVEL 3 CLONING & BRANCHING

FINDING THE REPO URL

URL of the remote repository


LEVEL 3 CLONING & BRANCHING

FINDING THE REPO URL

URL of the remote repository


LEVEL 3 CLONING & BRANCHING

CLONING A REPOSITORY
$ git clone https://github.com/codeschool/git-real.git Cloning into 'git-real'...

URL of the remote repository


$ git clone https://github.com/codeschool/git-real.git git-demo Cloning into 'git-demo'...

local folder name

LEVEL 3 CLONING & BRANCHING

GIT CLONE
1 - Downloads the entire repository into a new git-real directory. 2 - Adds the origin remote, pointing it to the clone URL.

master
HEAD

$ git remote -v
origin origin https://github.com/codeschool/git-real.git (fetch) https://github.com/codeschool/git-real.git (push)

3 - Checks out initial branch (likely master).

sets the head


LEVEL 3 CLONING & BRANCHING

BRANCHING OUT
Need to work on a feature that will take some time? Time to branch out. $ git branch cat

Jane
cat HEAD

branch created from master

$ git branch cat * master

HEAD still on master


LEVEL 3 CLONING & BRANCHING

master

SWITCHING TO A BRANCH
Time to jump on that new 'cat' branch. $ git checkout cat Switched to branch 'cat'

Jane
cat HEAD

HEAD is now on cat


master
LEVEL 3 CLONING & BRANCHING

WORKING ON A BRANCH
$ echo "Schrdinger" > cat.txt $ git add cat.txt $ git commit -m "Create quantum cat." [cat ab48a3f] Create quantum cat. 1 file changed, 1 insertion(+) create mode 100644 cat.txt

cat HEAD

committed to the cat branch


master
LEVEL 3 CLONING & BRANCHING

WORKING ON A BRANCH
$ ls README.txt cat.txt

see the cat?

cat HEAD

master
LEVEL 3 CLONING & BRANCHING

BACK TO MASTER
$ git checkout master Switched to branch 'master' $ ls README.txt

cat.txt is gone! and were back on master


HEAD

cat

$ git log commit 1191ceb7252c9d4b1e05c9969a55766a8adfce3b Author: Gregg <[email protected]> Date: Wed Jun 27 23:11:20 2012 -0700 Add README.

nothing in the log either

master

LEVEL 3 CLONING & BRANCHING

BACK TO CAT
$ git checkout cat Switched to branch 'cat' $ ls README.txt cat.txt

cat

phew, still here

HEAD

master
LEVEL 3 CLONING & BRANCHING

TIME TO MERGE
Done with that feature branch? Time to merge it into 'master '. $ git checkout master Switched to branch 'master' $ ls README.txt

master cat

HEAD

no cat, as expected

$ git merge cat Updating 1191ceb..ab48a3f Fast-forward cat.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 cat.txt

whats that?

merge brings one branchs changes into another

FAST-FORWARD TO THE FUTURE


Conditions for a fast-forward merge

master HEAD cat

nothing new

something new

LEVEL 3 CLONING & BRANCHING

BRANCH CLEAN UP
When youre done with a branch, you can safely remove it. $ git branch -d cat Deleted branch cat (was 957dbff).

master HEAD cat

LEVEL 3 CLONING & BRANCHING

NON-FAST-FORWARD
Lets work on a new admin feature. $ git checkout -b admin Switched to a new branch 'admin' ... $ git add admin/dashboard.html $ git commit -m 'Add dashboard' ... $ git add admin/users.html $ git commit -m 'Add user admin'

h c n a r b t u o s k c e h c d n a s e creat
master admin HEAD

Please fix the bugs on master.


LEVEL 3 CLONING & BRANCHING

BUG FI XING ON MASTER


Time to put out the !re. Well get back to that admin branch later. $ git checkout master Switched to branch 'master' $ git branch admin * master $ git pull ... $ git add store.rb $ git commit -m 'Fix store bug' ... $ git add product.rb $ git commit -m 'Fix product' ... $ git push

master admin HEAD HEAD

BACK TO OUR BRANCH


$ git checkout admin Switched to branch 'admin' ... When ready to bring in changes $ git checkout master Switched to branch 'admin' $ git merge admin

master admin HEAD HEAD

LEVEL 3 CLONING & BRANCHING

AND SUDDENLY...
Git uses Vi if no default editor is set to edit commit messages.
1 2 3 4 5 6 7 Merge branch 'admin' # # # # #

DON

T P

Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch. Lines starting with '#' will be ignored, and an empty message aborts the commit.

ANI

:wq

+ hit Enter to write (save) & quit

Vi commands
j h down left k l up right ESC i leave mode insert mode :wq :q! save & quit cancel & quit

RECURSIVE MERGING
Git cant fast-forward since changes were made in both branches.
Merge made by the 'recursive' strategy. 0 files changed create mode 100644 admin/dashboard.html create mode 100644 admin/users.html

master

merge commit

admin

A commit was created to merge the two branches.


$ git log commit 19f735c3556129279bb10a0d1447dc5aba1e1fa9 Merge: 5c9ed90 7980856 Author: Jane <[email protected]> Date: Thu Jul 12 17:51:53 2012 -0400 Merge branch 'admin'

2 commits

2 commits

COLLABORATION BASICS
LEVEL 4

github

I want a co py
?

push

Gregg
Clone the repo

Jane

$ git clone https://github.com/codeschool/git-real.git Start making changes

Jane adds two files

TWO NEW FILES


jane $ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # product.rb # store.rb nothing added to commit but untracked files present jane $ git add --all jane $ git commit -m "Add store and product models." [master 30ce481] Add product and store models. 2 files changed, 2 insertions(+) create mode 100644 product.rb create mode 100644 store.rb

COMMIT FLOW
jane $ git push Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 441 bytes, done. Total 4 (delta 0), reused 0 (delta 0) To https://github.com/codeschool/git-real.git 4e67ded..30ce481 master -> master

Sends her new commit

github

push new commits


master
LEVEL 4 COLLABORATION BASICS

DIFFERENT GIT COMMITS


gregg $ git commit -am "Update the readme." [master c715339] Update the readme. 1 file changed, 1 insertion(+), 1 deletion(-)

gregg

different same

github

? w o n s n e p p a h t a Wh

LEVEL 4 COLLABORATION BASICS

GIT PUSH REJECTED


gregg $ git push

Cannot write over Janes commit

To https://github.com/codeschool/git-real.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/codeschool/git-real.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull
...

$ git push
...

Success!

LEVEL 4 COLLABORATION BASICS

UNDERSTANDING PULL
$ git pull 1. Fetch (or Sync) our local repository with the remote one gregg github $ git fetch

origin

Fetch doesnt actually update any of our local code

UNDERSTANDING PULL
$ git pull 1. Fetch (or Sync) our local repository with the remote one gregg github $ git fetch

master

origin/master

master

2. Merges the origin/master with master $ git merge origin/master

MERGE COMMIT
$ git pull 1. Fetch (or Sync) our local repository with the remote one 2. Merges the origin/master with master $ git merge origin/master

Create a new commit for this merge


1 2 3 4 5 6 7 # # # # #

my editor

Merge branch 'master' of https://github.com/codeschool/git-real Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch. Lines starting with '#' will be ignored, and an empty message aborts the commit.

GIT PUSH REJECTED


$ git pull
remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 4 (delta 0) Unpacking objects: 100% (4/4), done. From https://github.com/codeschool/git-real 4e67ded..30ce481 master -> origin/master Merge made by the 'recursive' strategy. product.rb | 1 + store.rb | 1 + 2 files changed, 2 insertions(+) create mode 100644 product.rb create mode 100644 store.rb

merge commit

MERGE COMMIT
$ git pull 1. Fetch (or Sync) our local repository with the remote one 2. Merges the origin/master with master $ git fetch $ git merge origin/master

master

merge commit

origin/master

PU SHING COMMITS
$ git push Update origin/master be at the same state as our local repo

origin/master master

merge commit

github

OUR LOG
gregg $ git log commit ee47baaedcd54e1957f86bda1aaa1b8a136185da Merge: 87c5243 57501d5 Author: Gregg Pollack <[email protected]>

The problem with pull

Merge branch 'master' of https://github.com/Gregg/git-real commit 87c5243d2266f05cd9fda8b1c9137f11b3fe6f31 Author: Gregg Pollack <[email protected]> Update the readme. commit 57501d595b16e2d1198a9c04c547a5b1380a6618 Author: Gregg Pollack <[email protected]> Add store and product models.

MERGE CONFLICTS
README
here is my readme the cake is a lie

README

Gregg
committed
github

here is my readme

Jane

the cake is telling the truth!

committed & pushed

gregg

different same

MERGE CONFLICT
gregg $ git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1) Unpacking objects: 100% (3/3), done. From https://github.com/Gregg/git-real ee47baa..4e76d35 master -> origin/master Auto-merging README.txt CONFLICT (content): Merge conflict in README.txt Automatic merge failed; fix conflicts and then commit the result.

Git modified this file with the diff


LEVEL 4 COLLABORATION BASICS

MERGE CONFLICT
gregg $ git status # On branch master # Your branch and 'origin/master' have diverged, # and have 1 and 1 different commit each, respectively. # # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: README.txt # no changes added to commit (use "git add" and/or "git commit -a")

Need to edit these files

LEVEL 4 COLLABORATION BASICS

MERGE CONFLICT
README
here is my readme <<<<<<< HEAD the cake is a lie. ======= the cake is telling the truth! >>>>>>> 4e76d3542a7eee02ec516a47600002a90a4e4b48
gregg $ git commit -a Merge commit
LEVEL 4 COLLABORATION BASICS

Edit file and correct


here is my readme

Our local version the Janes version

cake is a lie.

COMMIT EDITOR
gregg $ git commit -a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Merge commit

Editor

Merge branch 'master' of https://github.com/Gregg/git-real Conflicts: ! README.txt # # It looks like you may be committing a merge. # If this is not correct, please remove the file #!.git/MERGE_HEAD # and try again.

# # # # #

Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. On branch master Your branch and 'origin/master' have diverged, and have 1 and 1 different commit each, respectively.

MERGE COMMIT
master
Merge commit

origin/master master
Merge commit

origin/master
$ git push

COLLABORATION BASICS
LEVEL 4

REMOTE BRANCHES & TAGS


LEVEL 5

WHY CREATE A REMOTE BRANCH?


When you need other people to work on your branch.
Any branch that will last more than a day.

Gregg

LEVEL 5 REMOTE BRANCHES AND TAGS

CREATING A REMOTE BRANCH


$ git checkout -b shopping_cart Switched to a new branch 'shopping_cart' $ git push origin shopping_cart Counting objects: 10, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 619 bytes, done. Total 6 (delta 2), reused 0 (delta 0) To https://github.com/codeschool/git-real.git * [new branch] shopping_cart -> shopping_cart

Links local branch to the remote branch (tracking)

LEVEL 5 REMOTE BRANCHES AND TAGS

PU SHING TO THE BRANCH


$ git add cart.rb $ git commit -a -m "Add basic cart ability." [shopping_cart 2a0dbf9] Add basic cart ability 1 file changed, 1 insertion(+) create mode 100644 cart.rb $ git push Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 302 bytes, done. Total 3 (delta 1), reused 0 (delta 0) To https://github.com/codeschool/git-real.git 786d7a1..2a0dbf9 shopping_cart -> shopping_cart

Pushed changes from branch

CREATING A BRANCH Hey Jane, I star ted a branch

t u o t i k c e h c l l I , t Swee

LEVEL 5 REMOTE BRANCHES AND TAGS

PU LLING NEW BRANCHES


$ git pull remote: Counting objects: 13, done. remote: Compressing objects: 100% (6/6), done. remote: Total 9 (delta 3), reused 8 (delta 2) Unpacking objects: 100% (9/9), done. From https://github.com/Gregg/git-real 4e76d35..786d7a1 master -> origin/master * [new branch] shopping_cart -> origin/shopping_cart Updating 4e76d35..786d7a1 Fast-forward README.txt | 2 +1 file changed, 1 insertion(+), 1 deletion(-)

LEVEL 5 REMOTE BRANCHES AND TAGS

remote branch

PU LLING NEW BRANCHES


$ git branch * master $ git branch -r origin/master origin/shopping_cart

list all remote branches

$ git checkout shopping_cart


Branch shopping_cart set up to track remote branch shopping_cart from origin. Switched to a new branch 'shopping_cart'

$ git branch master * shopping_cart

! h s u p d n a e t u b i r t n o c n a c e w Now

LEVEL 5 REMOTE BRANCHES AND TAGS

REMOTE SHOW
$ git remote show origin * remote origin Fetch URL: https://github.com/Gregg/git-real.git Push URL: https://github.com/Gregg/git-real.git HEAD branch: master Remote branches: master tracked shopping_cart tracked Local branches configured for 'git pull': master merges with remote master shopping_cart merges with remote shopping_cart Local refs configured for 'git push': master pushes to master (up to date) shopping_cart pushes to shopping_cart (local out of date)

REMOVING A BRANCH
$ git push origin :shopping_cart To https://github.com/codeschool/git-real.git - [deleted] shopping_cart $ git branch -d shopping_cart

Deletes remote branch lly

error: The branch 'shopping_cart' is not fully merged. If you are sure you want to delete it, run 'git branch -D shopping_cart'.

Must delete local branch manua

$ git branch -D shopping_cart Deleted branch shopping_cart (was ea0a1b9).

LEVEL 5 REMOTE BRANCHES AND TAGS

WHAT ABOUT GREGG?

Jane

Gregg

LEVEL 5 REMOTE BRANCHES AND TAGS

ON DELETED REMOTE BRANCH


$ git commit -m -a "Add ability to pay." [shopping_cart 9851887] Add ability to pay. 1 file changed, 1 insertion(+), 1 deletion(-) $ git push
Everything up-to-date

Gregg
t a local branch now)

No remote to push to (its jus

$ git remote show origin


Remote branches: master tracked refs/remotes/origin/shopping_cart stale (use 'git remote prune' to remove)

$ git remote prune origin

Pruning origin URL: https://github.com/codeschool/git-real.git * [pruned] origin/shopping_cart

To clean up deleted remote br anches

REMOTE BRANCH NAMES


$ git branch * staging master $ git push heroku-staging staging

Heroku deploys only master bra

nc h

Would not work, would push to staging

heroku-staging

local:remote
$ git push heroku-staging staging:master

Will push and deploy staging on heroku


LEVEL 5 REMOTE BRANCHES AND TAGS

TAGGING
A tag is a reference to a commit (used mostly for release versioning)
$ git tag v0.0.1 v0.0.2

list all tags it m m o c t a e d o c t u o check


Text

$ git checkout v0.0.1

To add a new tag


$ git tag -a v0.0.3 -m "version 0.0.3"

To push new tags


$ git push --tags
LEVEL 5 REMOTE BRANCHES AND TAGS

REMOTE BRANCHES & TAGS


LEVEL 5

REBASE BELONG TO US
LEVEL 6

MERGE COMMITS ARE BAD


origin/master master
Merge branch 'cats' Add Cats. Merge branch 'master' of http... Update the Readme.

merge commit

s s e l e s u l e e f s t i m m o c e g r e

Add product and store models.

LEVEL 6 REBASE BELONG TO US

DIFFERENT GIT COMMITS


gregg $ git commit -am "Update the readme." [master c715339] Update the readme. 1 file changed, 1 insertion(+), 1 deletion(-)

gregg

different same

github

t i m m o c s Jane

LEVEL 6 REBASE BELONG TO US

GIT PUSH REJECTED


gregg $ git push

Cannot write over Janes commit

To https://github.com/codeschool/git-real.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/codeschool/git-real.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull
...

$ git push
...

! e p o N

LEVEL 6 REBASE BELONG TO US

FETCH
gregg $ git fetch remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 4 (delta 0) Unpacking objects: 100% (4/4), done. From https://github.com/codeschool/git-real f35f2f1..71a4650 master -> origin/master

gregg

github

Syncs but doesnt merge

master

origin/master

LEVEL 6 REBASE BELONG TO US

REBASE
gregg $ git rebase

1. Move all changes to master which are not in origin/master to a temporary area.

temp

master

origin/master

LEVEL 6 REBASE BELONG TO US

REBASE
gregg $ git rebase

1. Move all changes to master which are not in origin/master to a temporary area. 2. Run all origin/master commits.

temp

master

origin/master

3. Run all commits in the temporary area, one at a time.


LEVEL 6 REBASE BELONG TO US

REBASE
gregg $ git rebase

1. Move all changes to master which are not in origin/master to a temporary area. 2. Run all origin/master commits. 3. Run all commits in the temporary area, one at a time.

master
Update the Readme.

No M

erge

Add product and store models.

Comm it!

LOCAL BRANCH REBASE


$ git checkout admin Switched to branch 'admin' $ git rebase master ...

master admin

master admin

Rebase

I F AL L GOES WELL, MERGE MAS TER


$ git checkout master Switched to branch 'master' $ git merge admin ...

master admin

WHAT ABOUT CONFLICTS


gregg
Add shopping cart.

github

Add lie to readme.

d e t i d E

! e l i f e m sa

Add truth to readme.

Add product and store models.

same

LEVEL 6 REBASE BELONG TO US

FETCH
gregg $ git fetch

master
Add shopping cart.

origin/master
Add truth to readme.

Add lie to readme.

Add product and store models.

LEVEL 6 REBASE BELONG TO US

REBASE
gregg $ git rebase

1. Move all changes to master which are not in origin/master to a temporary area

temp

origin/master
Add truth to readme.

Add product and store models.

master

REBASE
gregg $ git rebase

2. Run all origin/master commits.

temp
Add shopping cart.

master
Add truth to readme.

Add product and store models. Add lie to readme.

3. Run all commits in the temporary area, one at a time.

REBASE CONFLICT
gregg $ git rebase
First, rewinding head to replay your work on top of it... Applying: Add lie to readme. Using index info to reconstruct a base tree... M README.txt <stdin>:13: trailing whitespace. the cake is a lie, and I am your father! warning: 1 line adds whitespace errors. Falling back to patching base and 3-way merge... Auto-merging README.txt CONFLICT (content): Merge conflict in README.txt Failed to merge in the changes. Patch failed at 0001 Add lie to readme.

master CONFLICT

When you have resolved this problem run "git rebase --continue". If you would prefer to skip this patch, instead run "git rebase --skip". To check out the original branch and stop rebasing run "git rebase --abort".

REBASE CONFLICT
gregg $ git status
# Not currently on any branch. # Unmerged paths: # (use "git reset HEAD <file>..." to unstage) # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: README.txt # no changes added to commit (use "git add" and/or "git commit -a")

master

Edit the README.txt


gregg $ git add README.txt gregg $ git rebase --continue
Applying: Add lie to readme. Applying: Add shopping cart

gregg $

REBASED LOG
master
Add shopping cart. Add lie to readme.

Add truth to readme.

Add product and store models.

REBASE BELONG TO US
LEVEL 6

HISTORY & CONFIGURATION


LEVEL 7

VIEWING THE LOG


$ git log commit 915f242e262052b11c511dc07bef237fabb7ed85 SHA Author: Gregg <[email protected]> Date: Thu Jun 28 02:10:57 2012 -0700 Update index. commit message

hash

COLORIZING THE LOG


$ git config --global color.ui true $ git log commit 915f242e262052b11c511dc07bef237fabb7ed85 Author: Gregg <[email protected]> Date: Thu Jun 28 02:10:57 2012 -0700 Update index $ git log --pretty=oneline 08f202691c67abd12eb886b587ac7b26d51005c7 Update index

LOG FORMAT
$ git log --pretty=format:"%h %ad- %s [%an]" placeholder %ad %an %h %s %d replaced with author date author name SHA hash subject ref names

any string you want & placeholder data

run git help log for more options!


LEVEL 7 HISTORY & CONFIGURATION

PATCH
$ git log --oneline -p 3ea7f70 I'm telling you, it's 'Octopi'. diff --git a/index.html b/index.html index 021a54e..640d66d 100644 --- a/index.html +++ b/index.html @@ -8,7 +8,7 @@ <nav> <ul> <li><a href="cat.html">Cats</a></li> <li><a href="octopus.html">Octopuses</a></li> + <li><a href="octopi.html">Octopi</a></li>
LEVEL 7 HISTORY & CONFIGURATION

STATS
$ git log --oneline --stat 3ea7f70 I'm telling you, it's 'Octopi'. index.html | 2 +1 file changed, 1 insertion(+), 1 deletion(-) 96776a4 Add index. index.html | 30 +++++++++++++++--------------1 file changed, 15 insertions(+), 15 deletions(-)

LEVEL 7 HISTORY & CONFIGURATION

GRAPH
$ git log --oneline --graph * 30b1f8f Merge branch 'bird' into master |\ | * 8b8f950 Revise silly hipster name for bird aisle. * | 915f242 Add emphasis. |/ * 69728cd Update index descriptions.

visual representation of the branch merging into master

LEVEL 7 HISTORY & CONFIGURATION

DATE RANGES
$ git log --until=1.minute.ago $ git log --since=1.day.ago $ git log --since=1.hour.ago $ git log --since=1.month.ago --until=2.weeks.ago $ git log --since=2000-01-01 --until=2012-12-21

until since (days) since (hours) since & until (relative) since & until (absolute)

DIFFS
$ git diff diff --git a/index.html b/index.html @@ -8,7 +8,10 @@ <nav> <ul> <li><a href="cat.html">Cats</a></li> <li><a href="octopus.html">Octopuses</a></li> + <li><a href="birds.html">Birds</a></li> + <li><a href="hamsters.html">Hamsters</a></li> </ul> </nav> </body>

removed line

added lines

LEVEL 7 HISTORY & CONFIGURATION

UNCOMMITTED CHANGES
$ git diff HEAD diff --git a/index.html b/index.html index 021a54e..1ceb9d6 100644 diff between last commit @@ -8,7 +8,10 @@ & current state <ul> <li><a href="cat.html">Cats</a></li> ... diff --git a/octopus.html b/octopus.html index 55806be..ce8a2c7 100644 @@ -2,6 +2,6 @@ <html lang="en"> ...

includes both staged and unstaged files

EARLIER COMMITS
$ git diff HEAD^ $ git diff HEAD^^ $ git diff HEAD~5 $ git diff HEAD^..HEAD

parent of latest commit grandparent of latest commit five commits ago second most recent commit vs. most recent

LEVEL 7 HISTORY & CONFIGURATION

EARLIER COMMITS
$ git diff f5a6sdfsfsdfsdfff9..4sdsdfsdfsdfsdffb063f $ git log --oneline
257256c cat 4fb063f Add index f5a6ff9 Add catalog pages

range of SHAs

$ git diff 4fb063f..f5a6ff9 $ git diff master bird

range of abbreviated SHAs diff between two branches time-based diff

$ git diff --since=1.week.ago --until=1.minute.ago

BLAME
$ git blame index.html --date short ... 96776a42 (Gregg 2012-06-29 9) <ul> 96776a42 (Gregg 2012-06-29 10) <li>Cats</li> 3ea7f709 (Jane 2012-06-30 11) <li>Octopi</li> 96776a42 (Gregg 2012-06-29 12) </ul> ...

commit hash author

date

line # content

LEVEL 7 HISTORY & CONFIGURATION

EXCLUDING FILES
$ git status # Untracked files: # (use "git add <file>..." to include in what will be committed) # # experiments/ we dont want to commit this...
.git/info/exclude

experiments/

will exclude this folder from git

$ git status # On branch master nothing to commit (working directory clean)

the experiment directory is now invisible to git

EXCLUDE PATTERNS
tutorial.mp4 *.mp4 experiments/ logs/*.log

exclude this file exclude all .mp4 files exclude directory exclude .log files in logs directory

LEVEL 7 HISTORY & CONFIGURATION

EXCLUDING FROM ALL COPIES


$ # # # # # git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) logs/server.log

dont commit log files, they create conflicts


add this pattern

.gitignore

logs/*.log

$ git status no more logs # On branch master nothing to commit (working directory clean)

REMOVING FILES
$ git rm README.txt $ git status # Changes to be committed: # # deleted: README.txt

DELETED from the local filesystem & untracked


$ git commit -m Remove readme

LEVEL 7 HISTORY & CONFIGURATION

UNTRACKING FILES
$ git rm --cached development.log $ git status # Changes to be committed: # # deleted: development.log

what if youre already tracking log files?

not deleted from the local file system, only from Git

LEVEL 7 HISTORY & CONFIGURATION

UNTRACKING FILES
.gitignore

logs/*.log

will ignore all .log files inside the logs directory

$ git add .gitignore $ git commit -m "Ignore all log files." [master bccdc8c] Ignore all log files. 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 .gitignore delete mode 100644 development.log

LEVEL 7 HISTORY & CONFIGURATION

CONFIG
$ git config --global user.name "Gregg Pollack" $ git config --global user.email "[email protected]"

remember these? theres more


$ git config --global core.editor emacs

use emacs for interactive commands


OS X o use opendiff for merging conflictsnly

$ git config --global merge.tool opendiff

LEVEL 7 HISTORY & CONFIGURATION

LOCAL CONFIG
$ git config user.email "[email protected]" sets email for current repo $ git config --list user.name=Gregg [email protected] color.ui=true core.editor=mate -w [email protected] $ git config user.email [email protected]

same key can be set twice

the global config loaded first, then repo config

ALIASES
$ git config --global alias.mylog \ "log --pretty=format:'%h %s [%an]' --graph"

aliases for log formats

$ git config --global alias.lol \ "log --graph --decorate --pretty=oneline --abbrev-commit --all" $ git mylog * 19f735c |\ | * 7980856 * | 5c9ed90 |/ * ab48a3f

Merge branch 'admin' [Jane] Add user admin [Jane] Add dashboard. [Jane] Create quantum cat. [Jane]

ALIASES
$ git config --global alias.st status

git config alias.<name> <command>


git st git co git br git ci git status git checkout git branch git commit

$ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit

$ git st # On branch master nothing to commit (working directory clean)

You might also like