+ All Categories

Git 101

Date post: 18-May-2015
Category:
Upload: the-active-network
View: 7,735 times
Download: 1 times
Share this document with a friend
Description:
Alex Turner, Senior Software Engineer at The Active Network, walks us through the fundamentals of Git.
Popular Tags:
24
Git - An Introduction Alex R. M. Turner August 24th, 2011
Transcript
Page 1: Git 101

Git - An IntroductionAlex R. M. Turner

August 24th, 2011

Page 2: Git 101

Why DVCS, and why Git?

Page 3: Git 101

It makes development faster

Why DVCS, and why Git?

… plus it was written by Linus Torvalds, so it must be awesome!

Page 4: Git 101

What makes DVCS so great?

Page 5: Git 101

• Less time fighting your tool,

More time writing code!

• Less time figuring out what happened,

More time solving the problem.

DVCS makes life better!

Page 6: Git 101

JoeKlag Marie

SVN Server

SVN is a single source system. Everybody speaks to the central repository, and every check-in and check-out is put and made from there.To check code in, you must have a network connection, and a route to the server.

A check-in cannot occur if the branch has diverged, a merge is required, though it's called an update, and SVN doesn't keep any knowledge that it was actually a merge!

SVN, The Straight Jacket

• Check-outs are slow• Commits are infrequent• Branching causes a checkout to pull all the files over again

Infrequent commits lead to gargantuan merges, which suck in any system, and SVN's merge tools aren't great.Expensive branch check-out makes developers reluctant to branch

Page 7: Git 101

Klag Marie

Git Remote

For Git, having a single server that everybody synchronizes with, is just one of a few potential configurations. This is the model that is used most frequently in a corporate environment. This gives a single place that can be managed including things like back-ups and high-availability.

Each repository in a DVCS (Distributed Version Control System) has a complete revision history for any branch that is present, including remotely tracked branches (mostly).

This model allows for a few things that make life for developers significantly better:

• Frequent commits• Horizontal change-set sharing• Commits without internet connectivity• Branches retain all comments and context

especially around merges

DVS - The general proposition

Page 8: Git 101

• Git designed and initially built by Linus Torvalds• Designed to manage the codebase for the Linux kernel

Very very fast, all network transfers are heavily compressed, large merges happen with sub-second response time on UNIXish OSes generating fewer conflicts than other systems

Why is it called git?

Git - Emerging as the leading DVCS

Extremely powerful functionality:• Cheap branching and merging• Guarenteed data integrity• Selective commits• Sophisticating merging algorithms• Stashing• Multiple merge strategies that can merge multiple branches• Fast diffs• Patchset generation• Compact Syntax• Cherry Picking• Rebasing• Branch Filtering• SVN Porting tools!

• Linus jokes that he names programs after himself.• Git is a British English term that roughly means a person who is somewhat

arrogant, obnoxious and slightly sadistic.• Linus hates wasting time having to merge many many changes every day, so

he made git as fast as possible, and he knows the kernel, so it is very fast.

Page 9: Git 101

How do I use Git?

Page 10: Git 101

Local System

Get the code from the remote repository

Working Copy

git clone http://github.com/foo

What happened?

Remote Server

Local Remote

On a clone, git assumes you also want local files, so does a checkout.

Remote git packs

Remote's HEAD gets copied to local, which is a branch labelled "master".

CloneCheckout

HEAD is a descriptor that references the last element of a branch in a repository that is the current branch.

Page 11: Git 101

git clone http://github.com/foo

What just happened?

Local System

Remote Server

Working Copy Local Remote

Add files: Remote git packsPush local to remote:

Commit Push

Commit:git add \file1.txt src/main

git commit -m \"Added some files"

git push \origin master

Sending changes back to the repository

Add Commit Push

Page 12: Git 101

Local System

Remote Server

RemoteFetch

Push

The Index is git's log of what's going on. It contains a list of files and their state relative to HEAD.

Filling in a Few Gaps

Working Copy Local

Index

AddCommit

Checkout

Commit

Page 13: Git 101

master 8a63a7

test 76f43a

463e5c

6c75877

26a562c

8a63a7

26df25c

76f43a

master test

463e5c

6c75877

26a562c

8a63a7

26df25c

76f43a

A revision keeps track of its predecessor(s).This is how a branch is defined, a successive set of revisions that have a common ancestor. The lineage is given a label, and that is a reference to the branch. It isn't a name per se, it's a reference or pointer. You can change the label, or move it, but the underlying branch lineage doesn't change.

What is a Branch?

Page 14: Git 101

Fast Forward

Local System Remote Server

Local RemotePush

463e5c

6c75877

26a562c

8a63a7

26a562c

463e5c

6c75877

8a63a7

When there are only a single person's changes being sent upstream, it's pretty easy.This operation is described as a fast-forward

Pushing Changes - A Fast Forward

Common Ancestor

Page 15: Git 101

Local System Remote Server

Local Remote

Push

When you try to push, and the remote has been updated since you last fetched, it will reject your push

6c75877

26a562c

463e5c

26a562c

The two branches have a common ancestor, but a divergent HEAD. Pushing to a remote branch must be a "fast-forward". This means that the current revision on the remote, must be an ancestor of your current HEAD. When this occurs, what transpires is described as a fast-forward.

Divergent branches

Page 16: Git 101

Retrieving and Merging

Local System Remote Server

Local RemoteFetch

6c75877

26a562c

463e5c

26a562c

Show the difference:git diff --stat origin/master

Show the difference detail:git diff origin/master

Perform the Merge:git merge origin/master

Retrieve the changes:git fetch

Page 17: Git 101

Merge

8a63a7

26a562c

26a562c

6c75877

463e5c

master

origin/master

When a merge occurs, git uses one of the merge strategies to resolve differences:

• resolve• recursive

• ours• theirs• patience• renormalize• no-renormalize• subtree[=<path>]

• octopus• ours• subtree

conflicts are resolved with our versionconflicts are resolved with their versiontakes a bit more time, good for merging highly divergent branchespre-flight normalization for things like line-endingsdisable the previousmerge knowing that one branch is in a subdirectory of the other

merge more than two branchesmerge taking our history exclusivelyless granular subdirectory merge

3-way merge that is good at detecting crisscross merges, doesn't do renames

Merging - What happens

Page 18: Git 101

masterorigin/master

8a63a7

26a562c

6c75877463e5c

The revision 8a63a7 has two predecessors

• 6c75877• 463e5c

Merge Outcome - What does it look like?

Page 19: Git 101

6c75877

463e5c

master

origin/master

Merge Index Working Copy

When there are intersecting changes that can't be resolved by the merge toolManual intervention is required!

Conflict Conflagration!

Conflict!

Page 20: Git 101

Working Copy

Index

Fix poem.txt

Still conflicted?

Fix index.html

Still conflicted?

Working Copy

Index

Index

Index

master Remote

Nope

Add

Commit

Push

The Index keeps track of the status of things

Query the index to find out if there's more left to merge

Add files to indicate they are merged

Commit files once they've all been merge and push

git status

git add poem.txt

git commit -m "Merged, dropped Klag's changes"

Page 21: Git 101

I totally fubared the Merge!

You can get back to pre-merge state with a reset:

get reset --hard HEAD

Index

26a562c

26a562c

6c75877 - HEAD

463e5c

master

origin/master

reset

conflicted local changes

working files

working files

This also works for reverting changes

Page 22: Git 101

master

8a63a726a562c 463e5c

amend

Ammending a Commit

• I wrote a message, but it was crap• I forgot to add some files

Amending a commit after pushing will result in a conflict!

You created a new revision, with a new hash, and the remote has your old commit as HEAD which isn't an ancestor for your local master so a push is no longer a fast-foward

All is not lost though, you can fetch and merge like normal

Page 23: Git 101

Good Git Usage

Use .gitignore• It contains a list of patterns for files to ignore

Always add explicitly, never use git commit -a, or git add.

Always check your status with git status before you commit• If you commit hastily and push too quick, you'll end up

with files that shouldn't be there!

Page 24: Git 101

The End!

Questions?


Recommended