47
Git walkthrough @modsaid

Git walkthrough

Embed Size (px)

Citation preview

Page 1: Git walkthrough

Git walkthrough@modsaid

Page 2: Git walkthrough

Outline

● Source Control● Git Basics● Configuration● Viewing history● Undoing● Aliases● Tagging● Branching● Remotes● Bare repos● Hosting: Github, bitbucket, ...● Git 2.0+

Page 3: Git walkthrough

Version Control System

Page 4: Git walkthrough

Centralized Version Control System

Page 5: Git walkthrough

Distributed Version Control System

Page 6: Git walkthrough

Git Basics

● Local Repo ( .git )● Working directory

Page 7: Git walkthrough

Git Basics - The 3 States

Page 8: Git walkthrough

Git Basics

● git init● git status● git add ● git commit● git reset● git clone

● git mv● git rm● git blame● git push● git pull

Page 9: Git walkthrough

Git Basics - committing# Initializing a new repo

$ git init .

# Staging

$ git add .

$ git add -A

# committing

$ git commit -m “initial commit”

Page 10: Git walkthrough

Git Basics - committing$ git status

# diff (working vs staged)

$ git diff

# diff (staged vs committed)

$ git diff -s

# Stage and commit in one shot

$ git commit -a -m “direct staging and commit”

Page 11: Git walkthrough

Git Configuration

● System ● User ● repo

/etc/gitconfig

~/.gitconfig or ~/.config/git/config .git/config

Page 12: Git walkthrough

Git Configuration

git config --system

git config --global

git config --local

Page 13: Git walkthrough

Git Configuration$ git config --global user.name "John Doe"

$ git config --global user.email [email protected]

$ git config --global core.editor emacs

$ git config --list

$ git config user.name

Page 14: Git walkthrough

Viewing History$ git log$ git log -p -2$ git log --stat$ git log --grep adding$ git log --since=2.weeks$ git log --Sfunction_name$ git log --pretty=oneline$ git log --pretty=format:"%h - %an, %ar : %s"

Page 15: Git walkthrough
Page 16: Git walkthrough

Viewing History$ git log --pretty=format:"%ad %an : %s " --date=short

$ git log --pretty=oneline --since=”2012-12-20” --until=”2013-01-01” -5

$ git log --pretty=oneline | wc -l #no of commits

$ git log --pretty=format:%cd --date=short | uniq | wc -l #no of working days

Page 17: Git walkthrough

Viewing History$ git shortlog

$ git shortlog -s | wc -l #no of devs

$ git shortlog -se # list of devs

#no of coding days

$ git log --pretty=format:”%ad” --date=short | uniq | wc -l

Page 18: Git walkthrough

Viewing History - mapping names$ git shortlog -se | grep -i john

6 John Doe <[email protected]> 10 John-Doe <[email protected]> 4 John.Doe <[email protected]>

$ vim .mailmapname1 name2 name3 [email protected] [email protected]

$ git shortlog -se > .mailmap

Page 19: Git walkthrough

Undoinggit commit --amend

# Changing commit message$ git commit --amend -m “better commit message”

# Adding forgotten file$ git commit -m 'initial commit'$ git add forgotten_file$ git commit --amend

Page 20: Git walkthrough

Undoing#Unstaging$ git add .$ git status$ git reset HEAD benchmarks.rb

#dropping local commits <DANGER>git reset --hard HEAD~3

Page 21: Git walkthrough

Git Aliases$ git config --global alias.co checkout

$ git config --global alias.ci commit

$ git config --global alias.st status

$ git config alias.showdevs 'shortlog -s'

$ git config --global alias.unstage 'reset HEAD --'

Page 22: Git walkthrough

Tagging

● Lightweight: pointer to specific commit$ git tag before-optimization

● Annotated: full objects, checksummed, contains tagger info, suitable for releases$ git tag -a v1.4 -m 'my version 1.4'

Page 23: Git walkthrough

Tagging#Tagging later$ git tag -a v1.2 9fceb02

$ git show v1.2

$ git push origin v1.5

$ git push origin --tags

Page 24: Git walkthrough

Branching$ git branch #List current branches

$ git branch newone # create a new branch newone$ git checkout newone # switch to newone branch

# create branch and switch in one step$ git checkout -b newtwo

Page 25: Git walkthrough

Branching# Committing on master

Page 26: Git walkthrough

Branching$ git branch testing

Page 27: Git walkthrough

Branching

Page 28: Git walkthrough

Branching$ git checkout testing

Page 29: Git walkthrough

Branching$ git commit -a -m “some change”

Page 30: Git walkthrough

Branching$ git checkout master

Page 31: Git walkthrough

Branching$ git commit -a -m “master commit”

Page 32: Git walkthrough

Branching - Merging$ git merge testing

# in case of conflicts (failed auto merge)$ git mergetool

Page 33: Git walkthrough

Branching - Merging

fast-forward

git merge feature

no fast-forward

git merge --no-ff feature

Page 34: Git walkthrough

Remotes# Listing remotes

$ git remote

$ git remote -v

# Adding a remote

$ git remote add [shortname] [url]

$ git remote show origin

Page 35: Git walkthrough

Remotes# Fetching updates$ git fetch [remote-name]

$ git remote rename pb paul

$ git remote rm paul

Page 36: Git walkthrough

Bare Repos

● git repo with no working directory● The git server side$ git init --bare .

# Cloning (backup)

$ git clone --bare [email protected]:modsaid/git-demo2.git

# Restoring

$ git push --mirror [email protected]:modsaid/git-demo2.git

Page 37: Git walkthrough

Hosting: github, bitbucket

● Github: the most popular, free public repos ● Bitbucket: free private with limited

collaborators● A lot others

Page 38: Git walkthrough

Github

● Exploring repos● Pull Requests● Contribution to repos:

○ Fork○ Fix/Add○ Send pull request

https://help.github.com/articles/fork-a-repo/

Page 39: Git walkthrough

Latest git

The latest git release today is 2.3.3

Page 40: Git walkthrough

Installing the latest git (2.3)

$ sudo add-apt-repository ppa:git-core/ppa

$ sudo apt-get update

$ sudo apt-get install git

Page 41: Git walkthrough

What’s new in Git 2.0

“From the point of view of end-users who are totally new to Git, this release will give them the defaults that are vastly improved compared to the older versions, but at the same time, for existing users, this release is designed to be as low-impact as possible as long as they have been following recent releases along.”

● Better Defaults

Page 42: Git walkthrough

what’s new? git push

pre git 2.0

● Default: matching

starting git 2.0

● Default: simple

git config push.default

● ( nothing | current | upstream | simple | matching )

Page 43: Git walkthrough

what’s new? git add -upre git 2.0

● stage files in working directory

starting git 2.0

● stage files in the Repo Directory

Page 44: Git walkthrough

what’s new? git diff -q

pre git 2.0

● valid option

starting git 2.0

● Removed

Do not display deleted files during git diff

Page 46: Git walkthrough

Resources

● Pro Git, Second EditionBy: Scott Chacon; Ben StraubPublisher: ApressPub. Date: November 19, 2014

● git help COMMAND (man pages)● Git Recipes by BasayelSaid

Page 47: Git walkthrough

Thank You