The Fundamentals of Git

33
@DivineOmega By Jordan Hall The Fundamentals of Git

Transcript of The Fundamentals of Git

Page 1: The Fundamentals of Git

@DivineOmega

By Jordan Hall

The Fundamentals of Git

Page 2: The Fundamentals of Git

@DivineOmega

Background

What, why, when?

Page 3: The Fundamentals of Git

@DivineOmega

What is Git?

● Version control● Distributed● Free (GNU GPL2)

Page 4: The Fundamentals of Git

@DivineOmega

History of Git

● Developed in 2005– Started on 3 April

– 1.0 release on 21 December 2005

● Created– For the Linux Kernel

– By Linus Torvalds

● Maintained by Junio Hamano

Page 5: The Fundamentals of Git

@DivineOmega

'Git'?

"I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'."

Linus Torvalds

Page 6: The Fundamentals of Git

@DivineOmega

'Git'?

Page 7: The Fundamentals of Git

@DivineOmega

How people use Git

● Many different ways● Can just be used locally

– Just on your PC

● Or between teams– Using a remote repository

● Many web services/interfaces– GitHub, GitLab, BitBucket, etc.

Page 8: The Fundamentals of Git

@DivineOmega

Git Internals

What makes up typical Git usage

Page 9: The Fundamentals of Git

@DivineOmega

The Git Places

● Working directory● 'Stage' or 'Staging Area'● Local Git repository

– '.git' directory

● Optionally, remote Git repository– Known as 'remotes'

Page 10: The Fundamentals of Git

@DivineOmega

Working directory

● Where you work● Where you make your changes● Use 'git checkout' to revert changes to a file in

the working directory (back to the last commit)

● Sometimes called the 'workspace'

Page 11: The Fundamentals of Git

@DivineOmega

Staging area

● Any files about to be committed ('staged')● Use 'git add' to move files to the staging area

('stage' them)● Use 'git reset' to remove files from the staging area

● Stored in index file in your .git directory● Sometimes called the 'index'

Page 12: The Fundamentals of Git

@DivineOmega

Local Git Repository

● All committed files, revision history, etc.– Commits (objects identified by SHA1 hashes – commit IDs)

– Pointers to certain commits (known as references)● Branches● Tags

– 'Head' – where you currently are, a pointer to another reference

● Use 'git commit' to store any staged files in a new commit● Use 'git revert' to undo a commit by its ID (creates a new commit)

● Most of what is stored in your .git directory (.git/objects/ & .git/refs/)

Page 13: The Fundamentals of Git

@DivineOmega

Remote Git Repository

● Any repository not on your computer

● Known as a 'remote'● Consists of a name and a URL● 'git remote add', 'git remote remove', 'git remote'

● 'git push' to send changes to the remote● 'git pull' to fetch & merge changes from the remote

Page 14: The Fundamentals of Git

@DivineOmega

Git Branches

Splitting off to do new things

Page 15: The Fundamentals of Git

@DivineOmega

Quick Explanation of Branches

● Just a named pointer to a specific commit● Use 'git branch branch_name' to create one

pointing at your current commit

● File containing the SHA-1 hash of the commit it points to– .git/refs/heads/master

– .git/refs/heads/branch_name

Page 16: The Fundamentals of Git

@DivineOmega

Quick Explanation of Branches

http://goo.gl/wpesH6

● git branch testing– Create new 'testing' branch at current commit

Page 17: The Fundamentals of Git

@DivineOmega

Quick Explanation of Branches

● Used to separate developments from the 'master' branch– New features

– Phases of development

● Typically merged back into master when feature is complete

Page 18: The Fundamentals of Git

@DivineOmega

Every Day Git Usage

Local and remote examples

Page 19: The Fundamentals of Git

@DivineOmega

Local Repository Example

http://goo.gl/4Pojzb

Page 20: The Fundamentals of Git

@DivineOmega

Local Repository Example

● Assumption: A local Git repository exists and has multiple branches.

● git checkout greetings– checkout 'greetings' branch of local repository

● create/change hello.txt in your favourite editor

● git add hello.txt– stage files (added to the staging area / git index)

● git commit -m “Altered greeting”– commit to local repository (.git directory)

Page 21: The Fundamentals of Git

@DivineOmega

Remote Repository Example

http://goo.gl/iCaOTt

Page 22: The Fundamentals of Git

@DivineOmega

Remote Repository Example

● Assumption: A remote Git repository exists and has been cloned locally.

● git add hello.txt– stage files (to staging area / git index)

● git commit -m “New greets”– commit to local repo. (.git directory)

● git push– Updates the remote repository with local changes– Technically:

● Tells the remote about your local branches/commits● Asks the remote to update its branch reference (commit ID) to match the local reference● It will oblige providing the change is a 'fast forward' (i.e. no merge required, just moving reference)

Page 23: The Fundamentals of Git

@DivineOmega

Merging

Bringing branches together

Page 24: The Fundamentals of Git

@DivineOmega

Create your new branch

● git branch super_new_feature– Create a new branch for the new feature

● git checkout super_new_feature– Switch to the new branch

Page 25: The Fundamentals of Git

@DivineOmega

Add your new fix/feature

● Add the new feature– In our example, we'll say this feature only required a change to 'thing.php'

● git add thing.php– Stage the file(s)

● git commit -m 'Added great new feature'– Commit the file(s)

● Continue to change, add and commit until the feature is complete, tested and ready to be merged into the main (master) branch

Page 26: The Fundamentals of Git

@DivineOmega

Merge the branch

● git checkout master– Switch back to the main branch

● git merge super_new_feature– Merge the changes from super_new_feature into the

current branch (master)

● git branch -d super_new_feature– Delete the branch, to keep your local repository tidy

Page 27: The Fundamentals of Git

@DivineOmega

Notes on Merging and Conflicts

● Branches can be used however you wish.● Master is nothing special, it's just the default.

● Automatic merges sometimes fail.– CONFLICT (content): Merge conflict in thing.php

– Automatic merge failed; fix conflicts and then commit the result.

● When they do:– Go into the files specified and fix the conflicts.

– Stages the files ('git add')

– Commit the fixes ('git commit') with an appropriate message

Page 28: The Fundamentals of Git

@DivineOmega

Git Directory and Commands

A quick overview

Page 29: The Fundamentals of Git

@DivineOmega

.git directory

Page 30: The Fundamentals of Git

@DivineOmega

Quick reference to Git commands

● git status – Shows the current branch, modified working directory files, and staged files● git add – Stages files (gets them ready for committing)● git commit – Moves all staged files into your local repository● git push – Sends all local repository changes to the remote, and updates the remote reference● git pull – Fetch and merge from a remote repository (or local branch)● git revert – Undoes a commit based on its ID (creates a new commit)

● git checkout – Reverts file(s) in the working directory back to the last commit (also be used to switch branch)– Be careful! This command can cause permanent loss of uncommitted changes!

● git reset – Removes files from the staging area (and more, be careful!)– Danger Will Robinson! Use of the '--hard' parameter can cause permanent loss of both uncommitted and

committed changes!

Page 31: The Fundamentals of Git

@DivineOmega

Git Help

Page 32: The Fundamentals of Git

@DivineOmega

End!

Thanks. :)

Page 33: The Fundamentals of Git

@DivineOmega

References and useful links

● Explaining Git Push - http://goo.gl/VK2aiE● About Git References - http://goo.gl/IwyJZZ● The .git Directory - http://goo.gl/8vghFq● Git Branching - http://goo.gl/oAbQDM● Branching and Merging - http://goo.gl/lJkuUD● Undoing Changes - http://goo.gl/VKpRhM● The 'Master' Branch - http://goo.gl/NbECCY