Upload
mahmoud-said
View
93
Download
1
Embed Size (px)
Citation preview
Git walkthrough@modsaid
Outline
● Source Control● Git Basics● Configuration● Viewing history● Undoing● Aliases● Tagging● Branching● Remotes● Bare repos● Hosting: Github, bitbucket, ...● Git 2.0+
Version Control System
Centralized Version Control System
Distributed Version Control System
Git Basics
● Local Repo ( .git )● Working directory
Git Basics - The 3 States
Git Basics
● git init● git status● git add ● git commit● git reset● git clone
● git mv● git rm● git blame● git push● git pull
Git Basics - committing# Initializing a new repo
$ git init .
# Staging
$ git add .
$ git add -A
# committing
$ git commit -m “initial commit”
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”
Git Configuration
● System ● User ● repo
/etc/gitconfig
~/.gitconfig or ~/.config/git/config .git/config
Git Configuration
git config --system
git config --global
git config --local
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
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"
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
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
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
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
Undoing#Unstaging$ git add .$ git status$ git reset HEAD benchmarks.rb
#dropping local commits <DANGER>git reset --hard HEAD~3
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 --'
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'
Tagging#Tagging later$ git tag -a v1.2 9fceb02
$ git show v1.2
$ git push origin v1.5
$ git push origin --tags
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
Branching# Committing on master
Branching$ git branch testing
Branching
Branching$ git checkout testing
Branching$ git commit -a -m “some change”
Branching$ git checkout master
Branching$ git commit -a -m “master commit”
Branching - Merging$ git merge testing
# in case of conflicts (failed auto merge)$ git mergetool
Branching - Merging
fast-forward
git merge feature
no fast-forward
git merge --no-ff feature
Remotes# Listing remotes
$ git remote
$ git remote -v
# Adding a remote
$ git remote add [shortname] [url]
$ git remote show origin
Remotes# Fetching updates$ git fetch [remote-name]
$ git remote rename pb paul
$ git remote rm paul
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
Hosting: github, bitbucket
● Github: the most popular, free public repos ● Bitbucket: free private with limited
collaborators● A lot others
Github
● Exploring repos● Pull Requests● Contribution to repos:
○ Fork○ Fix/Add○ Send pull request
https://help.github.com/articles/fork-a-repo/
Latest git
The latest git release today is 2.3.3
Installing the latest git (2.3)
$ sudo add-apt-repository ppa:git-core/ppa
$ sudo apt-get update
$ sudo apt-get install git
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
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 )
what’s new? git add -upre git 2.0
● stage files in working directory
starting git 2.0
● stage files in the Repo Directory
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
what’s new?● https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.0.0.txt● https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.1.0.txt● https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.2.0.txt● https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.3.0.txt
Resources
● Pro Git, Second EditionBy: Scott Chacon; Ben StraubPublisher: ApressPub. Date: November 19, 2014
● git help COMMAND (man pages)● Git Recipes by BasayelSaid
Thank You