+ All Categories
Home > Technology > Introduction to Git

Introduction to Git

Date post: 15-Jan-2015
Category:
Upload: atishgoswami
View: 367 times
Download: 0 times
Share this document with a friend
Description:
A brief introduction to git commands and architecture with descriptive diagrams and explanations.
Popular Tags:
159
Up and Running with GIT Atish Goswami PHP Developer at SANIsoft
Transcript
Page 1: Introduction to Git

Up and Running with GIT

Atish Goswami

PHP Developer at SANIsoft

Page 2: Introduction to Git

What is Git

• Keeps track of your changes Especially text changes Version 1, Version 2, Version 3

• Version Control System (VCS)

• Source Code Management (SCM)

Page 3: Introduction to Git

Examples of version control(non-source code)

• File naming (Budget_v4.xls, Logo_v2.gif)

• Microsoft Word's Track Change

• Adobe Photoshop's History

• Wikis

• Undo: control+z(windows), command+z(Mac)

Page 4: Introduction to Git

History

• Source Code Control System (SCCS) 1972, closed source, free with Unix

• Revision Control System (RCS) 1982, open source

• Concurrent Versions System (CVS) 1986-1990, open source

• Apache Subversion (SVN) 2000, open source

Page 5: Introduction to Git

Turn of Events

• BitKeeper SCM 2000, closed source, proprietary Distributed version control “community version” was free Used for source code of the linux kernel for

2002-2005 Controversial to use proprietary SCM for an

open source project April 2005: the “community version” not free

anymore

Page 6: Introduction to Git

Git was born

• April 2005

• Created by Linus Torvalds

• Replacement for BitKeeper to manage Linux kernel source code

Page 7: Introduction to Git

What were the features

• Distributed version control

• Open source and free software

• Compatible with Unix-like systems (Linux, Mac Os X, and Solaris) and Windows

• Faster than other SCMs (100x is some cases)

• Better safeguards against data corruption

Page 8: Introduction to Git

Git is a hit

• Explosion in popularity

• No official statistics

• Github launched in 2008 to host Git repositories 2009: over 50,000 repos, over 100,000 users 2011: over 2 million repos, over 1 million users

Page 9: Introduction to Git

Centralized Version Control

• Popular version controls of the past like RCS, CVS, SVN and SCSS

• All of them use central code repository model One central place where you store the master copy of

the your code Check out a copy of the code from the master repo Work with the code, make changes and submit back to

the master/central repository Other users can also work from that repo, make and

submit there changes Users stay up to date with central code repository by

pulling down and update any changes

Page 10: Introduction to Git

Distributed Version Control

• Different users (or teams of users) maintain their own repos, instead if working form a central repository

• Changes are stored as “changes sets” or “patches” Tracks changes, not versions Different from CVS and SVN, which track versions Change sets can be exchanged between repositories

• “merge in change sets” or “apply patches”

Page 11: Introduction to Git

Distributed Version Control

• No single master repository; just many working copies Each with their own combination of change

sets• Imagine changes to document as sets A, B, C, D, E, F

Repo 1: A, B, C, D, E, F Repo 2: A, B, C, D Repo 3: A, B, C, E Repo 4: A. B, E, F

Page 12: Introduction to Git

Advantages of DVS

• No need to communicate with a central server

Faster No network access required No single failure point

• Encourages participation and “forking” of projects Developers can work independently Submit changes sets for inclusions or rejection

Page 13: Introduction to Git

Who should use Git ?

• Anyone wanting to track edits

Reviews a history log of changes made view differences between versions Retrieve old versions

• Anyone needing to share changes with collaborators

• Anyone not afraid of command-line tools

Page 14: Introduction to Git

Programmers and Developers

HTML, CSS, JavaScript

PHP, Ruby, Ruby on Rails, Perl, Python, ASP Java, C, C++, C#, Objection-C ActionScript, CoffeeScript, Haskell, Scala,

Shell scripts• Not as Useful for tracking non-text files

Images, movies, music, fonts Word processing files, spreadsheets, PDFs

Page 15: Introduction to Git

Git Installation

Page 16: Introduction to Git

Where to get Git

Mac

• http://git-scm/download/mac Windows

• http://git-scm/download/windows Linux

• http://git-scm/download/linux

Page 17: Introduction to Git

Configuration

System

• /etc/gitconfig• Program Files\Git\etc\gitconfig

User• ~/.gitconfig• $HOME\.gitconfig

Project• my_project/.git/config

Page 18: Introduction to Git

Commands to Configuration

System

• git config --system User

• git config --global Project

• git config

Page 19: Introduction to Git

Initializing a repository

git init

Page 20: Introduction to Git

Process to follow

Make changes

Add the changes • git add [filename/directory]

Commit changes to the repository with message• git commit -m [commit message]

Page 21: Introduction to Git

Writing Commit Messages

Commit message best practices

• Short single-line summary (less than 50 characters)

• Optionally followed by a blank line and more complete description

• Keep each lines to less than 72 characters• Write commit messages in preset tense, not

past tense “fix bug” or “fixes bug,” not “fixed bug”

Page 22: Introduction to Git

Writing Commit Messages

Commit message best practices

• Bullet points are usually asterisks or hyphens

• Can add “ticket tracking number” form bugs or support requests

• Can develop shorthand for your organization “[css, js]” “bugfix:” “#38456-”

Page 23: Introduction to Git

Writing Commit Messages

Commit message best practices

• Be clear and descriptiveBad: “fix typo”Good: “Add missing > in project section

of HTML”Bad: “Update login code”Good: “Changed user authentication to

use Blowfish”Bad: “Updates member report, we should

discuss if this is right next week”

Page 24: Introduction to Git

One Good Example

T23094 – Fixes bug in admin logout

When an admin logged out of the admin area, they could not log in to the member area because their session[:userid] was still set to the admin ID. This patch fixes the bug by setting session[:userid] to null when user logs of the any area.

Page 25: Introduction to Git

Viewing Commit Logs

git log

commit 330ec85cb900530edbfa0ac73519f29b4150e436Author: Atish Goswami <[email protected]>

Date: Wed Apr 23 02:15:47 2014 +0530

Fix login page html for responsive view

Page 26: Introduction to Git

Advanced Commit Viewing

git log -n [limit]

git log --since=[yyyy-mm-dd]

git log --until=[yyyy-mm-dd]

git log --author=”[name]”

git log --grep=”[pattern]”

Page 27: Introduction to Git

Git Concepts and architecture

Page 28: Introduction to Git

Two Tree architecture

Page 29: Introduction to Git

Three Tree architecture

Page 30: Introduction to Git

Three Tree architecture

Page 31: Introduction to Git

Git Workflow

Page 32: Introduction to Git
Page 33: Introduction to Git
Page 34: Introduction to Git
Page 35: Introduction to Git
Page 36: Introduction to Git
Page 37: Introduction to Git
Page 38: Introduction to Git
Page 39: Introduction to Git
Page 40: Introduction to Git
Page 41: Introduction to Git
Page 42: Introduction to Git
Page 43: Introduction to Git
Page 44: Introduction to Git
Page 45: Introduction to Git
Page 46: Introduction to Git
Page 47: Introduction to Git
Page 48: Introduction to Git

Commit Hash Values (SHA-1)

Git generates a checksum for each change set

• checksum algorithms convert data into a simple number

• same data always equals same checksum Data integrity is fundamental

• changing data would change checksum Git uses SHA-1 algorithm to create checksums

• 40-character hexdecimal string (0-9, a-f)• Example:

2da5bdb0d516f69c69f35bde21312ab89cbc3357

Page 49: Introduction to Git

Referring to Commits

Page 50: Introduction to Git

The HEAD Pointer

pointer to “tip” of current branch in repository

last state of repository, what was last checked out

points to parent of the next commit• where writing commits takes place

Page 51: Introduction to Git
Page 52: Introduction to Git
Page 53: Introduction to Git
Page 54: Introduction to Git
Page 55: Introduction to Git
Page 56: Introduction to Git
Page 57: Introduction to Git
Page 58: Introduction to Git
Page 59: Introduction to Git
Page 60: Introduction to Git
Page 61: Introduction to Git
Page 62: Introduction to Git
Page 63: Introduction to Git

Making changes

git add [filename/directory]

git status git diff git diff --staged git rm [filename/directory] git mv [filename-old]<space>[filename-new] git commit git commit -am

Page 64: Introduction to Git

Undoing changes

git checkout – [filename]

git reset HEAD [filename] git commit –amend -m “[Message]” git checkout [commit hash] – [filename.txt] git revert [commit hash] git clean -n → Trial run git clean -f → actual action

Page 65: Introduction to Git

git reset

--soft

• Does not change staging index or working directory

• git reset –soft [commit hash] --mixed (default)

• changes staging index to match repository• does not change working directory• git reset –mixed [commit hash]

--hard• Changes staging index and working

directory to match repository• git reset –hard [commit hash]

Page 66: Introduction to Git

Ignoring Files

Using .gitignore file in the root of the project directory

Project/.gitignore Very basic regular expression

• * ? [aeiou] [0-9] Negate expression with !

• *.php• !index.php

Ignore all files in a directory with trailing slash• asset/video/

Comment lines begin with #, blank lines are skipped

Page 67: Introduction to Git

What to ignore

complied source code

packages and compressed files logs and databases operating system generated files user-uploaded assets (images, PDFs, videos) Helpful Links

• https://help.github.com/articles/ignoring-files• https://github.com/github/gitignore

Page 68: Introduction to Git

Globally Ignore Files

Ignore files in all repositories

Settings not tracked in repository user-specific instead of repository-specific

git config –global core.excludesfile ~/.gitignore_global

Page 69: Introduction to Git

Ignoring Tracked Files

Want it in your working directory

But don't want git track it any more

git rm –cached [filename]

Page 70: Introduction to Git

Tracking Empty Directories

• Git doesnot track directories

• Git track files only and keep track of any directory required to track that file

• Add empty or .gitkeep to empty directory

Page 71: Introduction to Git

Navigating to a Commit

• Tree-ish

Full SHA-1 hash Short SHA-1 hash

• At least 4 characters• Umambiguous (8-10 characters)

HEAD pointer Branch reference, tag reference ancestry

Page 72: Introduction to Git

Ancestory

• Parent commit

HEAD^, acf87504^, master^ HEAD~1, HEAD~

• Grandparent commit

HEAD^^, acf87504^^, master^^ HEAD~2

• HEAD^^^, acf87504^^^, master^^^

• HEAD~3

Page 73: Introduction to Git

Tree-ish

• git help ls-tree

• git ls-tree HEAD

• git ls-tree master

• git ls-tree master [directory]

• git ls-tree master^ [directory]

• git ls-tree [object hash]

Page 74: Introduction to Git

Getting More From Git Log

• git log --online

• git log –online -[no of commits to show]

• git log –since=”[yyyy-mm-dd]”

• git log –until=”[yyyy-mm-dd]”

• git log –before=”[yyyy-mm-dd]”

• git log –since=”2 weeks ago” --until=”3 days ago”

• git log –since=”2.weeks” --until=”3.days”

Page 75: Introduction to Git

Getting More From Git Log

• git log –author=”Atish”

• git log –grep=”temp”

• git log [sha hash from]..[sha hash to] –oneline

• git log [sha hash from]..[leave empty to point HEAD] [filename]

• git log -p -> Git Patch option

• git log -p c4b913e.. index.html

• git log –stat –summary

• git log –format=[oneline|short|medium|full|fuller|email|raw]

• git log –graph

• git log –online –graph –all --decorate

Page 76: Introduction to Git

Getting More From Git Log

• git show [sha value]

• git show –format=online HEAD

• git show –format=online HEAD^

• git show –format=online HEAD~2

• git show –format=online HEAD~3

Page 77: Introduction to Git

Comparing Commits

• git diff [sha value]

• git diff [sha value]..[sha value till]

• git diff [sha value] filename

• git diff [sha value]..[sha value till] filename

• git diff –stat –summary 1506576..HEAD

• git diff –ignore-space-change or -b

• git diff –ignore-all-change or -w

Page 78: Introduction to Git

Git Branching

• Branches are cheap

Try new ideas Isolate features or sections of work

• One working directory

• Fast context switching

Page 79: Introduction to Git
Page 80: Introduction to Git
Page 81: Introduction to Git
Page 82: Introduction to Git
Page 83: Introduction to Git
Page 84: Introduction to Git
Page 85: Introduction to Git
Page 86: Introduction to Git
Page 87: Introduction to Git
Page 88: Introduction to Git
Page 89: Introduction to Git
Page 90: Introduction to Git
Page 91: Introduction to Git
Page 92: Introduction to Git

Git Branching Commands

• Show branches

git branch• Create branch

git branch [branch name to create]• Switch to a branch

git checkout [branch name]• Create and Switch in a single step

git checkout -b [new branch name]• Branch diff

git diff [branchname]..[other branch name] git diff –color-words new_feature..shorten_title

• Does branch has all changes or not

git branch –merged• Rename a branch

git branch -m [old name] [new name]• Branch Delete

git branch -d [branch to delete]

Page 93: Introduction to Git

Merging Branches

• Merge a branch

git merge [branchname to merge]• Fast Forward Merge

• No Fast Forward

git merge –no-ff [branch name]• Only Fast Forward

git merge –ff-only [branch name]

Page 94: Introduction to Git
Page 95: Introduction to Git
Page 96: Introduction to Git
Page 97: Introduction to Git
Page 98: Introduction to Git
Page 99: Introduction to Git
Page 100: Introduction to Git
Page 101: Introduction to Git

Resolving Conflicts

• Abort merge

git merge --abort• Resolve the conflicts manually

Open files and see for changes to keep git add [files that had merge conflict] git commit → show a auto commit message

• Use a merge tool

Page 102: Introduction to Git

Reduce Merge Conflicts

• Keep lines short

• Keep commits small and focused

• Beware stray edits to whitespace

Spaces, tabs, line returns• Merge often

• Track changes to master

Page 103: Introduction to Git
Page 104: Introduction to Git
Page 105: Introduction to Git
Page 106: Introduction to Git
Page 107: Introduction to Git
Page 108: Introduction to Git
Page 109: Introduction to Git
Page 110: Introduction to Git
Page 111: Introduction to Git
Page 112: Introduction to Git
Page 113: Introduction to Git
Page 114: Introduction to Git
Page 115: Introduction to Git
Page 116: Introduction to Git
Page 117: Introduction to Git
Page 118: Introduction to Git
Page 119: Introduction to Git
Page 120: Introduction to Git
Page 121: Introduction to Git
Page 122: Introduction to Git

Stashing Changes

• It is not like commiting

• Helps keep your experimental files save

• Does add anything to the log history

• Its like a temporary storage space

Page 123: Introduction to Git

Stashing Changes

• Adding/Saving a stash change

– git stash save “[message]”• Listing Stashes

– git stash list – stash@{n}: On [branchname]: [message]

• View Stashes Changes

git stash show stash@{n} → diff stat

git stash show -p stash@{n} → patch stat

Page 124: Introduction to Git

Stashing Changes

• Applying a stashed change

→ git stash apply [stash@{2}]

→ git stash pop [stash@{2}]

• Deleting Stashed Items

→ git stash drop [stash@{2}]

→ git stash clear

Page 125: Introduction to Git

Remotes

Page 126: Introduction to Git
Page 127: Introduction to Git
Page 128: Introduction to Git
Page 129: Introduction to Git
Page 130: Introduction to Git
Page 131: Introduction to Git
Page 132: Introduction to Git
Page 133: Introduction to Git
Page 134: Introduction to Git
Page 135: Introduction to Git
Page 136: Introduction to Git
Page 137: Introduction to Git
Page 138: Introduction to Git
Page 139: Introduction to Git
Page 140: Introduction to Git
Page 141: Introduction to Git
Page 142: Introduction to Git
Page 143: Introduction to Git
Page 144: Introduction to Git
Page 145: Introduction to Git
Page 146: Introduction to Git
Page 147: Introduction to Git
Page 148: Introduction to Git
Page 149: Introduction to Git
Page 150: Introduction to Git
Page 151: Introduction to Git
Page 152: Introduction to Git
Page 153: Introduction to Git
Page 154: Introduction to Git

Remote Repos

• Viewing remotes

– git remote– git remote -v

• Adding remote to local repo

– git remote [alias] [url]• Removing remote from repo

– git remote rm origin

Page 155: Introduction to Git

Remote Repos

• Viewing remotes branches

– git remote -r– git remote -a

• Copying/Cloning Remote Repos

– git clone [url] [folder to create]• Removing remote from repo

– git remote rm origin

Page 156: Introduction to Git

Remote Repos

• Pushing Changes

– git push [remote] [branchname]• Syncing Remote with Local

– git fetch [remote]

Page 157: Introduction to Git

Good Practices

• Fetch before you work

• Fetch before you push

• Fetch often

Page 158: Introduction to Git

Merging Fetched Changes

• Viewing/Comparing Remote with Local

– git diff [remote]/[branchname]..[branchname]

• Merging Changes Into Local

– git merge [remote]/[branchname]• git pull = git fetch + git merge

Page 159: Introduction to Git

Working with remote branches

• Checkout a remote branch

– git branch [remote branch name]• Delete a remote branch

– git push [remote] :[remote branch name]– git push origin –delete non_tracking


Recommended