SlideShare a Scribd company logo
Getting Some Git
VSC Basics, Hacks and Best Practices
Snapshots, Not Differences
● Other systems store the difference made to the file in
a commit
@Pro Git
Snapshots, Not Differences
● Git stores a snapshot of the file in the commit
● If the file wasn’t changed, it’s pointed to the latest
version
● Introduced many change to how Git performs
@Pro Git
Integrity, Locality
● Git has integrity
⚪ Every file is check-summed
⚪ Checksum is used as a reference
⚪ Whenever something changes, Git knows about it
⚪ Files are stored in DB by checksum not names
● Everything is local
⚪ Local resources are only needed for Git to operate
⚪ Other VCS’s depend on servers heavily
◾ SVN commits online, for instace
⚪ You can work offline. You only need servers to sync up
Git Sections - Three sections
@Pro Git
Git Transitions - Move files
@Pro Git
Git Transitions - Add files
● When the file is first created, it’s untracked
$ git add file # file is now staged
● A tracked file, that has been modified, is in the
working directory
$ git add tracked_file # tracked_file is now staged
● Commit the staged files
$ git commit -m ‘Here goes your message’
● Commit all working directory
$ git commit -am ‘msg’ # Untracked files are not affected
Git Transitions - Undo
● Unstage a staged file back to the working directory or
being untracked
$ git reset HEAD file
● Discard changes in a working-directory file
$ git checkout -- file
Git Transitions - Deletion
● Delete a file
$ rm file # Deletion of file is not staged
$ git rm file # Deletion is now staged.
● Or, stage the deletion directly
$ git rm file
● You can do that
$ rm file
$ git commit -a # Deletion is committed
.gitignore
● Git uses this to ignore files from tracking
● Should be the first step after creating a new repo
● Actually there’s a repo for .gitignore’s
- github.com/github/gitignore/
● Two master rules of thumb
- Ignore anything that can be generated (data files, compiled
files, log files … )
- Libraries generated by dependency managers
- Ignore any user-specific data (preferences, IDE UI data … )
- Party trick: use example patterns.
.gitignore - How
● Create a .gitignore file in your repo’s root folder
● Add names or patterns
#Dont’ mind me. I’m a comment
*.class
WhyIsThisFileHere.txt
bin/
● Add .gitignore to Git
● You can create another .gitignore in a subfolder
⚪ Will only be applied for that folder
.gitignore - Be aware
● Be Aware: .gitignore is only applied on untracked files
● If you need to ignore a tracked file, remove it first
$ git rm --cached <file> #Removes the file from Git, but keeps it
● Adding an ignored file to Git is not allowed
⚪ However, adding -f overrides that. Be a good fellow and don’t
use it
Remotes
● Clone a repo from a remote
$ git clone git@github.com:rails/rails.git Rails # Will be cloned in Rails/
● You can add more remotes
$ git add backup git@github.com:rails/rails.git
● List them
$ git remote -v
origin git@github.com:SeelozInc/dashboard-ios.git (fetch)
origin git@github.com:SeelozInc/dashboard-ios.git (push)
old-remote ssh://git@gitlab.badrit.com/mohannad.hassan/seeloz-dashboard.git (fetch)
old-remote ssh://git@gitlab.badrit.com/mohannad.hassan/seeloz-dashboard.git (push)
● Remove
$ git remote rm other_rigin
● Edit
$ git remote rename origin fathy_remote
$ git remote set-url origin git@github.com:rails/rails.git
Remotes - Listen to them
● Fetch data from a remote
⚪ Branches are fetched into remote/branch (origin/master)
⚪ Merge it yourself
$ git fetch origin # If you don’t type a remote name, it’s origin by default
● Or pull to merge changes automatically
$ git pull origin
$ git pull origin master # Just for a branch
● Fetch from all at once
$ git update
Remotes - Talk to them
● Push your commits up
$ git push origin master
● Push and track
$ git push -u origin new_feature_branch # Will be merged when you pull
● Push all
$ git push origin # Push all tracking branches
$ git push # No remote → origin is the default
Remotes - Show
$ git remote show origin
* remote origin
URL: https://github.com/my-org/complex-project
Fetch URL: https://github.com/my-org/complex-project
Push URL: https://github.com/my-org/complex-project
HEAD branch: master
Remote branches:
master tracked
dev-branch tracked
issue-43 new (next fetch will store in remotes/origin)
refs/remotes/origin/issue-11 stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev-branch merges with remote dev-branch
master merges with remote master
Local refs configured for 'git push':
dev-branch pushes to dev-branch (up to date)
markdown-strip pushes to markdown-strip (up to date)
master pushes to master (up to date)--
Alias
● Shortcuts!
● Define them
$ git config --global alias.st status #”git st” instead of “git status”
● You can add more (args, files … )
$ git config --global alias.unstage “reset HEAD” # git unstage file.c
● Open other applications
$ git config --global alias.visual "!gitk"
Alias
● Add to ~/.gitconfig directly
[alias]
co = checkout
st = status
● Have aliases with arguments
logc = "!f() { 
git log $1 -$2; 
}; f"
$ git logc other_branch 5
git log
● Shows commits history starting from HEAD
● Start from a branch or a tag
$ git log my_branch
$ git log v3.2.2
● Start from a commit
$ git log ab199cf0
● Add “diff” of last two commits
$ git log -p -2
● Put on some makeup
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
git log - more
● Make your own format
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
● There are other pretty params and format options
- Check them. [#10 in references]
● Filter
$ git log -4 #Last 4 commits
$ git log --since 1/1/2015 #Commits after that date. You can als use “after”
$ git log --committer john.smith@badrit.com
$ git log --grep controller
git log - Files
● Show the history of a file
$ git log --pretty=oneline
970d421295efbc6371b5a09384142504232ac73b Renamed c.c to d.d
465b3274ec1f4dbb3179bd3486180f5fb4a9ec76 Deleted b.b
d0f6f31b0569d96af93d031200e97dd545c9bec2 Edited a.a
81bfbdd2f6c8d31c2c242f5d524f9b293e2efc6e c.c
d5d21265591815dce7f320fbb70c52dd99edaadc b.b
ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a
$ git log --pretty=oneline a.a
d0f6f31b0569d96af93d031200e97dd545c9bec2 Edited a.a
ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a
git log - Deleted and Moved Files
● Works for deleted files
$ git log --follow --pretty=oneline -- b.b
465b3274ec1f4dbb3179bd3486180f5fb4a9ec76 Deleted b.b
d5d21265591815dce7f320fbb70c52dd99edaadc b.b
ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a #First commit
$ git log --follow --pretty=oneline -- d.d
970d421295efbc6371b5a09384142504232ac73b Renamed c.c to d.d
81bfbdd2f6c8d31c2c242f5d524f9b293e2efc6e c.c
ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a #First commit
$ git log --follow --pretty=oneline -- c.c
970d421295efbc6371b5a09384142504232ac73b Renamed c.c to d.d
81bfbdd2f6c8d31c2c242f5d524f9b293e2efc6e c.c
ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a
git log --graph
● You can show it as a graph
$ git log --graph
● Forget about the last point. Log it in a gitk-ish way
⚪ Command is tall.
⚪ Get it from http://stackoverflow.com/a/9074343/959221.
git log --graph
@StackOverFlow
Tag
● Mark important steps and milestones in the
development
● Lightweight tag: Commit’s checksum stored in a file
$ git tag v1.4
$ git tag v1.3 4d3f911 # Tag a certain commit
● List
$ git tag
$ git tag -l “v1.8*” # Filter to tags that has “v1.8”
● Annotated; Add a message
$ git tag v9.8 -m ‘Random tag’
Diff
● Default: Diff between working directory and staging
$ git diff
● Diff between working directory and a commit
$ git diff <commit>
● Diff between staging and last commit
$ git diff --cached# Staging and last commit
$ git diff --cached commit # Staging and given commit
● Diff between two commits
$ git diff HEAD^^ HEAD # start_commit end_commit
# “HEAD^^” = “Head~2”
● Diff one file
$ git diff start_commit end_commit -- file
$ git diff we45hsf -- file # Working directory and given commit
$ git diff file # Working directory and HEAD
● Diff across branches
$ git diff master..branch
● Diff tools
$ git difftool # Launches a tool to view changes
● Candidates: meld, vimdiff, diffmerge … References #12
and #14 for more
⚪ You may need to install the chosen package
$ git config --global diff.tool meld
$ git config --global merge.tool meld #
Diff - More
Diff - meld
A Commit Anatomy
● A commit contains:
⚪ A pointer to the files snapshot
⚪ Author’s name and email
⚪ The message
⚪ A pointer to its parent(s)
● Commit’s parents:
⚪ Zero parents; if it’s the initial commit
⚪ Multiple parents; if it is a merge commit
⚪ One; otherwise
Branches
● A branch is just a pointer to a commit
● Master branch is also not different
⚪ git init creates it by default
● Whenever you commit, the branch pointer moves along
with the new commit
● HEAD is a special pointer. It points to the branch that
you currently on
Branches - Anatomy
@Pro Git
Branches - Anatomy
@Pro Git
After one
commit
Branches - Detached HEAD
● Checkout a single commit
⚪ You can even create a branch from there
$ git checkout ab199cf0
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout. If you
want to create a new branch to retain commits you create, you may do so (now
or later) by using -b with the checkout command again.
Branches - Low Cost
● A branch is a single pointer; 40-characters file
● Creating branches and playing with them is not costly
● Sharp contrast with other VCS’s
⚪ Depending on the size of the project, creating a new branch
may take minutes to copy the contents to another directory.
● Since it’s just a pointer, finding the recent common
parent is easy, which makes merging easy.
⚪ You had to find that parent yourself in other systems
● Create a branch and move to it
$ git checkout -b new_branch
● Just create it
$ git branch new_branch
● Push it. Don’t forget to track it
$ git push -u origin new_branch
● Move around
$ git checkout new_branch # HEAD is not at new_branch
$ git checkout master # HEAD is not at master
● Delete it locally
$ git branch -d new_branch
● Delete it on the server
$ git push origin --delete <branchName>
Branches - How
Branches - Merging
● Adds one branch’s commits to the other
$ git checkout master
$ git merge new_branch
● Fast-forward
⚪ Just move the pointer up ahead
@Pro Git
Branches - Merging
● Git determines the best common ancestor
● Creates a new snapshot by heads of each branch, and
the BCS
@Pro Git
Branches - Merge Conflicts
● Conflicts happen when some part is edited in both branches
● The merge commit is paused until these conflicts are solved
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified:
index.html
● Done editing? Add the file
$ git add index.html
● If you don’t have any other conflicts, commit.
Branches - Work Flow and Models
● How you organize stories affects how you see your Git
repo
● Famous models exist
● Develop your own to suit your needs
● Famous levels
⚪ master: stable branch. Has been or will be released
⚪ next or staging: Code that’s ready to be tested. Not always
stable, but when it is stable, will be merged to master
⚪ develop: finish topic-branches and merge them here
⚪ Add or remove, as it suits your model
● Think of it as work silo
⚪ Work on some code
⚪ When reaches some point, move it
● Why
⚪ Agile
Branches - Long-Running Branches
Branches - Long-Running Branches
@Pro Git
Branches - Topic Branches
● Can be used by any work model, any project of any
size
● Steps:
⚪ Branch out from development for feature
⚪ Work on feature
⚪ Merge back into development when done
● Don’t branch out from a topic branch! Or do. There’s
a debate
● Rule of thumb: If you branch B out from A, merge B
back into A
Branches - Topic Branches
● Why
⚪ Agile
⚪ No half-done code in long-running branches
⚪ Work in parallel. But you have to divide work right.
Pull Requests
● Can be used with two models:
⚪ Fork and pull
⚪ Shared repo
● Go to the topic branch and choose “Compare &
review”
● Choose target repo / target branch
● Can be reviewed before merging
● Why?
⚪ Allow for contributions while maintaining conventions
⚪ Review stories before they’re finalised.
The Art of Committing - Messages
● Expressive and understandable. Yet don’t make a speech
● Don’t commit with -m; It’s very limited
● Format it:
⚪ First line is brief
⚪ Add a link to ticket.
⚪ Check #3 in references
● Answer questions: What was done? Why? What side effects?
● Should be integral to code review
⚪ Know what this commit is about, from the message
The Art of Committing - Small Early Commits
● Don’t include some 10 or 20 hours of work in one
commit
● Modularity
⚪ It’s required everywhere!
● Reviewable
● Traceable
● Uncommitted code won’t “crumble” on you
● Would introduce too many unneeded commits in the
history?
⚪ You can edit commits later -- A little advanced
Amend
● Edit the last commit
$ git commit --amend
● Will open the editor to edit the message
● You can even add or remove files that you forgot to
add
$ git add Index.html
$ git commit --amend # Changes in Index.html is now included in last commit
● Don’t edit public history
⚪ Have a look before you push
⚪ If it’s pushed, don’t edit it
● There are other ways to edit non-HEAD commits
Auto-Completion
● Download the script ([11] in References)
● Add it to home directory
● Include the following in .bashrc file
source ~/git-completion.bash
● Press on a tab to enjoy
● For Windows: Auto completion is auto-configured with
mysysGit
References
1. Pro Git
2. man git
3. https://sethrobertson.github.io/GitBestPractices
4. http://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message
5. http://sethrobertson.github.io/GitPostProduction/gpp.html#pre-post-production
6. http://programmers.stackexchange.com/questions/10793/when-is-a-version-control-c
ommit-too-large
7. http://wildlyinaccurate.com/a-hackers-guide-to-git/
8. http://nvie.com/posts/a-successful-git-branching-model/
9. http://blogs.atlassian.com/2014/10/advanced-git-aliases/
10. http://opensource.apple.com/source/Git/Git-19/src/git-htmldocs/pretty-formats.txt?
txt
11. https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
12. http://www.gitguys.com/topics/git-diff/
13. http://stackoverflow.com/a/9834872
14. http://stackoverflow.com/questions/137102/whats-the-best-visual-merge-tool-for-git

More Related Content

What's hot

My Notes from https://www.codeschool.com/courses/git-real
My Notes from  https://www.codeschool.com/courses/git-realMy Notes from  https://www.codeschool.com/courses/git-real
My Notes from https://www.codeschool.com/courses/git-real
Eneldo Serrata
 
Git Memento of basic commands
Git Memento of basic commandsGit Memento of basic commands
Git Memento of basic commands
Zakaria Bouazza
 
Git Real
Git RealGit Real
Git Real
Gong Haibing
 
Introducción a git y GitHub
Introducción a git y GitHubIntroducción a git y GitHub
Introducción a git y GitHub
Lucas Videla
 
GIT in a nutshell
GIT in a nutshellGIT in a nutshell
GIT in a nutshell
alignan
 
Using Git as your VCS with Bioconductor
Using Git as your VCS with BioconductorUsing Git as your VCS with Bioconductor
Using Git as your VCS with Bioconductortimyates
 
Git advanced
Git advancedGit advanced
Git advanced
Peter Vandenabeele
 
Essential Git and Github commands
Essential Git and Github commandsEssential Git and Github commands
Essential Git and Github commands
Isham Rashik
 
Get Good With Git
Get Good With GitGet Good With Git
Get Good With Git
Hoffman Lab
 
Git the Docs: A fun, hands-on introduction to version control
Git the Docs: A fun, hands-on introduction to version controlGit the Docs: A fun, hands-on introduction to version control
Git the Docs: A fun, hands-on introduction to version control
Becky Todd
 
Git session Dropsolid.com
Git session Dropsolid.comGit session Dropsolid.com
Git session Dropsolid.com
dropsolid
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
themystic_ca
 
Git github
Git githubGit github
Git github
Anurag Deb
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshell
Pranesh Vittal
 
Bitbucket
BitbucketBitbucket
Bitbucket
hariprasad1035
 
Git inter-snapshot public
Git  inter-snapshot publicGit  inter-snapshot public
Git inter-snapshot public
SeongJae Park
 
Use Git like a pro - condensed
Use Git like a pro - condensedUse Git like a pro - condensed
Use Git like a pro - condensed
Jesús Miguel Benito Calzada
 

What's hot (19)

My Notes from https://www.codeschool.com/courses/git-real
My Notes from  https://www.codeschool.com/courses/git-realMy Notes from  https://www.codeschool.com/courses/git-real
My Notes from https://www.codeschool.com/courses/git-real
 
Git Memento of basic commands
Git Memento of basic commandsGit Memento of basic commands
Git Memento of basic commands
 
Git Real
Git RealGit Real
Git Real
 
Introducción a git y GitHub
Introducción a git y GitHubIntroducción a git y GitHub
Introducción a git y GitHub
 
GIT in a nutshell
GIT in a nutshellGIT in a nutshell
GIT in a nutshell
 
Using Git as your VCS with Bioconductor
Using Git as your VCS with BioconductorUsing Git as your VCS with Bioconductor
Using Git as your VCS with Bioconductor
 
Git advanced
Git advancedGit advanced
Git advanced
 
Essential Git and Github commands
Essential Git and Github commandsEssential Git and Github commands
Essential Git and Github commands
 
Get Good With Git
Get Good With GitGet Good With Git
Get Good With Git
 
Git the Docs: A fun, hands-on introduction to version control
Git the Docs: A fun, hands-on introduction to version controlGit the Docs: A fun, hands-on introduction to version control
Git the Docs: A fun, hands-on introduction to version control
 
Git real slides
Git real slidesGit real slides
Git real slides
 
Git session Dropsolid.com
Git session Dropsolid.comGit session Dropsolid.com
Git session Dropsolid.com
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
 
Git github
Git githubGit github
Git github
 
Git in a nutshell
Git in a nutshellGit in a nutshell
Git in a nutshell
 
Gittalk
GittalkGittalk
Gittalk
 
Bitbucket
BitbucketBitbucket
Bitbucket
 
Git inter-snapshot public
Git  inter-snapshot publicGit  inter-snapshot public
Git inter-snapshot public
 
Use Git like a pro - condensed
Use Git like a pro - condensedUse Git like a pro - condensed
Use Git like a pro - condensed
 

Viewers also liked

Roof Top Solar Proposal open to all
Roof Top Solar Proposal open to allRoof Top Solar Proposal open to all
Roof Top Solar Proposal open to allNMP Vikramji
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
BADR
 
The Perks and Perils of the Singleton Design Pattern
The Perks and Perils of the Singleton Design PatternThe Perks and Perils of the Singleton Design Pattern
The Perks and Perils of the Singleton Design Pattern
BADR
 
Распределительная Логистика
Распределительная ЛогистикаРаспределительная Логистика
Распределительная Логистика
Moseichuk
 
Mgt 521 new final exam
Mgt 521 new final examMgt 521 new final exam
Mgt 521 new final exam
sergejsvolkovs10
 

Viewers also liked (9)

032515_Tikitum Lab_Full_NEW
032515_Tikitum Lab_Full_NEW032515_Tikitum Lab_Full_NEW
032515_Tikitum Lab_Full_NEW
 
Tounchpoint VAS Linkin
Tounchpoint VAS LinkinTounchpoint VAS Linkin
Tounchpoint VAS Linkin
 
Roof Top Solar Proposal open to all
Roof Top Solar Proposal open to allRoof Top Solar Proposal open to all
Roof Top Solar Proposal open to all
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
The Perks and Perils of the Singleton Design Pattern
The Perks and Perils of the Singleton Design PatternThe Perks and Perils of the Singleton Design Pattern
The Perks and Perils of the Singleton Design Pattern
 
Распределительная Логистика
Распределительная ЛогистикаРаспределительная Логистика
Распределительная Логистика
 
SDCV1116
SDCV1116SDCV1116
SDCV1116
 
Mgt 521 new final exam
Mgt 521 new final examMgt 521 new final exam
Mgt 521 new final exam
 
AOWMC-05-00134
AOWMC-05-00134AOWMC-05-00134
AOWMC-05-00134
 

Similar to Getting some Git

Git and git workflow best practice
Git and git workflow best practiceGit and git workflow best practice
Git and git workflow best practice
Majid Hosseini
 
GIT_GITHUB_2016_06_17
GIT_GITHUB_2016_06_17GIT_GITHUB_2016_06_17
GIT_GITHUB_2016_06_17siva ram
 
Introduction of Git
Introduction of GitIntroduction of Git
Introduction of GitWayne Chen
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With Git
Nick Quaranto
 
Git: Why And How to
Git: Why And How toGit: Why And How to
Git: Why And How to
lanhuonga3
 
Git for beginners
Git for beginnersGit for beginners
Git for beginners
Arulmurugan Rajaraman
 
Git and github 101
Git and github 101Git and github 101
Git and github 101
Senthilkumar Gopal
 
Git & gitflow
Git & gitflowGit & gitflow
Git & gitflow
Nolifelover Earn
 
Git presentation
Git presentationGit presentation
Git presentation
James Cuzella
 
Advanced Git Tutorial
Advanced Git TutorialAdvanced Git Tutorial
Advanced Git Tutorial
Sage Sharp
 
Git with the flow
Git with the flowGit with the flow
Git with the flow
Dana White
 
Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control SystemVictor Wong
 
The Fundamentals of Git
The Fundamentals of GitThe Fundamentals of Git
The Fundamentals of Git
DivineOmega
 
Git Intermediate Course
Git Intermediate CourseGit Intermediate Course
Git Intermediate Course
Ali Abbasi
 
git-and-bitbucket
git-and-bitbucketgit-and-bitbucket
git-and-bitbucketazwildcat
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHub
DSC GVP
 
Git tech talk
Git tech talkGit tech talk
Git tech talkrazasayed
 
Git
GitGit
Git training v10
Git training v10Git training v10
Git training v10
Skander Hamza
 
Git basics
Git basicsGit basics
Git basics
Amit Sawhney
 

Similar to Getting some Git (20)

Git and git workflow best practice
Git and git workflow best practiceGit and git workflow best practice
Git and git workflow best practice
 
GIT_GITHUB_2016_06_17
GIT_GITHUB_2016_06_17GIT_GITHUB_2016_06_17
GIT_GITHUB_2016_06_17
 
Introduction of Git
Introduction of GitIntroduction of Git
Introduction of Git
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With Git
 
Git: Why And How to
Git: Why And How toGit: Why And How to
Git: Why And How to
 
Git for beginners
Git for beginnersGit for beginners
Git for beginners
 
Git and github 101
Git and github 101Git and github 101
Git and github 101
 
Git & gitflow
Git & gitflowGit & gitflow
Git & gitflow
 
Git presentation
Git presentationGit presentation
Git presentation
 
Advanced Git Tutorial
Advanced Git TutorialAdvanced Git Tutorial
Advanced Git Tutorial
 
Git with the flow
Git with the flowGit with the flow
Git with the flow
 
Git Distributed Version Control System
Git   Distributed Version Control SystemGit   Distributed Version Control System
Git Distributed Version Control System
 
The Fundamentals of Git
The Fundamentals of GitThe Fundamentals of Git
The Fundamentals of Git
 
Git Intermediate Course
Git Intermediate CourseGit Intermediate Course
Git Intermediate Course
 
git-and-bitbucket
git-and-bitbucketgit-and-bitbucket
git-and-bitbucket
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHub
 
Git tech talk
Git tech talkGit tech talk
Git tech talk
 
Git
GitGit
Git
 
Git training v10
Git training v10Git training v10
Git training v10
 
Git basics
Git basicsGit basics
Git basics
 

More from BADR

Sunspot - The Ruby Way into Solr
Sunspot - The Ruby Way into SolrSunspot - The Ruby Way into Solr
Sunspot - The Ruby Way into Solr
BADR
 
Docker up and Running For Web Developers
Docker up and Running For Web DevelopersDocker up and Running For Web Developers
Docker up and Running For Web Developers
BADR
 
Vue.js
Vue.jsVue.js
Vue.js
BADR
 
There and Back Again - A Tale of Programming Languages
There and Back Again - A Tale of Programming LanguagesThere and Back Again - A Tale of Programming Languages
There and Back Again - A Tale of Programming Languages
BADR
 
Take Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven DevelopmentTake Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven Development
BADR
 
NoSQL Databases
NoSQL DatabasesNoSQL Databases
NoSQL Databases
BADR
 
Explicit Semantic Analysis
Explicit Semantic AnalysisExplicit Semantic Analysis
Explicit Semantic Analysis
BADR
 
ReactiveX
ReactiveXReactiveX
ReactiveX
BADR
 
Algorithms - A Sneak Peek
Algorithms - A Sneak PeekAlgorithms - A Sneak Peek
Algorithms - A Sneak Peek
BADR
 
Android from A to Z
Android from A to ZAndroid from A to Z
Android from A to Z
BADR
 
Apache Hadoop - Big Data Engineering
Apache Hadoop - Big Data EngineeringApache Hadoop - Big Data Engineering
Apache Hadoop - Big Data Engineering
BADR
 
MySQL Indexing
MySQL IndexingMySQL Indexing
MySQL Indexing
BADR
 
Duckville - The Strategy Design Pattern
Duckville - The Strategy Design PatternDuckville - The Strategy Design Pattern
Duckville - The Strategy Design Pattern
BADR
 

More from BADR (13)

Sunspot - The Ruby Way into Solr
Sunspot - The Ruby Way into SolrSunspot - The Ruby Way into Solr
Sunspot - The Ruby Way into Solr
 
Docker up and Running For Web Developers
Docker up and Running For Web DevelopersDocker up and Running For Web Developers
Docker up and Running For Web Developers
 
Vue.js
Vue.jsVue.js
Vue.js
 
There and Back Again - A Tale of Programming Languages
There and Back Again - A Tale of Programming LanguagesThere and Back Again - A Tale of Programming Languages
There and Back Again - A Tale of Programming Languages
 
Take Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven DevelopmentTake Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven Development
 
NoSQL Databases
NoSQL DatabasesNoSQL Databases
NoSQL Databases
 
Explicit Semantic Analysis
Explicit Semantic AnalysisExplicit Semantic Analysis
Explicit Semantic Analysis
 
ReactiveX
ReactiveXReactiveX
ReactiveX
 
Algorithms - A Sneak Peek
Algorithms - A Sneak PeekAlgorithms - A Sneak Peek
Algorithms - A Sneak Peek
 
Android from A to Z
Android from A to ZAndroid from A to Z
Android from A to Z
 
Apache Hadoop - Big Data Engineering
Apache Hadoop - Big Data EngineeringApache Hadoop - Big Data Engineering
Apache Hadoop - Big Data Engineering
 
MySQL Indexing
MySQL IndexingMySQL Indexing
MySQL Indexing
 
Duckville - The Strategy Design Pattern
Duckville - The Strategy Design PatternDuckville - The Strategy Design Pattern
Duckville - The Strategy Design Pattern
 

Recently uploaded

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 

Recently uploaded (20)

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 

Getting some Git

  • 1.
  • 2. Getting Some Git VSC Basics, Hacks and Best Practices
  • 3. Snapshots, Not Differences ● Other systems store the difference made to the file in a commit @Pro Git
  • 4. Snapshots, Not Differences ● Git stores a snapshot of the file in the commit ● If the file wasn’t changed, it’s pointed to the latest version ● Introduced many change to how Git performs @Pro Git
  • 5. Integrity, Locality ● Git has integrity ⚪ Every file is check-summed ⚪ Checksum is used as a reference ⚪ Whenever something changes, Git knows about it ⚪ Files are stored in DB by checksum not names ● Everything is local ⚪ Local resources are only needed for Git to operate ⚪ Other VCS’s depend on servers heavily ◾ SVN commits online, for instace ⚪ You can work offline. You only need servers to sync up
  • 6. Git Sections - Three sections @Pro Git
  • 7. Git Transitions - Move files @Pro Git
  • 8. Git Transitions - Add files ● When the file is first created, it’s untracked $ git add file # file is now staged ● A tracked file, that has been modified, is in the working directory $ git add tracked_file # tracked_file is now staged ● Commit the staged files $ git commit -m ‘Here goes your message’ ● Commit all working directory $ git commit -am ‘msg’ # Untracked files are not affected
  • 9. Git Transitions - Undo ● Unstage a staged file back to the working directory or being untracked $ git reset HEAD file ● Discard changes in a working-directory file $ git checkout -- file
  • 10. Git Transitions - Deletion ● Delete a file $ rm file # Deletion of file is not staged $ git rm file # Deletion is now staged. ● Or, stage the deletion directly $ git rm file ● You can do that $ rm file $ git commit -a # Deletion is committed
  • 11. .gitignore ● Git uses this to ignore files from tracking ● Should be the first step after creating a new repo ● Actually there’s a repo for .gitignore’s - github.com/github/gitignore/ ● Two master rules of thumb - Ignore anything that can be generated (data files, compiled files, log files … ) - Libraries generated by dependency managers - Ignore any user-specific data (preferences, IDE UI data … ) - Party trick: use example patterns.
  • 12. .gitignore - How ● Create a .gitignore file in your repo’s root folder ● Add names or patterns #Dont’ mind me. I’m a comment *.class WhyIsThisFileHere.txt bin/ ● Add .gitignore to Git ● You can create another .gitignore in a subfolder ⚪ Will only be applied for that folder
  • 13. .gitignore - Be aware ● Be Aware: .gitignore is only applied on untracked files ● If you need to ignore a tracked file, remove it first $ git rm --cached <file> #Removes the file from Git, but keeps it ● Adding an ignored file to Git is not allowed ⚪ However, adding -f overrides that. Be a good fellow and don’t use it
  • 14. Remotes ● Clone a repo from a remote $ git clone git@github.com:rails/rails.git Rails # Will be cloned in Rails/ ● You can add more remotes $ git add backup git@github.com:rails/rails.git ● List them $ git remote -v origin git@github.com:SeelozInc/dashboard-ios.git (fetch) origin git@github.com:SeelozInc/dashboard-ios.git (push) old-remote ssh://git@gitlab.badrit.com/mohannad.hassan/seeloz-dashboard.git (fetch) old-remote ssh://git@gitlab.badrit.com/mohannad.hassan/seeloz-dashboard.git (push) ● Remove $ git remote rm other_rigin ● Edit $ git remote rename origin fathy_remote $ git remote set-url origin git@github.com:rails/rails.git
  • 15. Remotes - Listen to them ● Fetch data from a remote ⚪ Branches are fetched into remote/branch (origin/master) ⚪ Merge it yourself $ git fetch origin # If you don’t type a remote name, it’s origin by default ● Or pull to merge changes automatically $ git pull origin $ git pull origin master # Just for a branch ● Fetch from all at once $ git update
  • 16. Remotes - Talk to them ● Push your commits up $ git push origin master ● Push and track $ git push -u origin new_feature_branch # Will be merged when you pull ● Push all $ git push origin # Push all tracking branches $ git push # No remote → origin is the default
  • 17. Remotes - Show $ git remote show origin * remote origin URL: https://github.com/my-org/complex-project Fetch URL: https://github.com/my-org/complex-project Push URL: https://github.com/my-org/complex-project HEAD branch: master Remote branches: master tracked dev-branch tracked issue-43 new (next fetch will store in remotes/origin) refs/remotes/origin/issue-11 stale (use 'git remote prune' to remove) Local branches configured for 'git pull': dev-branch merges with remote dev-branch master merges with remote master Local refs configured for 'git push': dev-branch pushes to dev-branch (up to date) markdown-strip pushes to markdown-strip (up to date) master pushes to master (up to date)--
  • 18. Alias ● Shortcuts! ● Define them $ git config --global alias.st status #”git st” instead of “git status” ● You can add more (args, files … ) $ git config --global alias.unstage “reset HEAD” # git unstage file.c ● Open other applications $ git config --global alias.visual "!gitk"
  • 19. Alias ● Add to ~/.gitconfig directly [alias] co = checkout st = status ● Have aliases with arguments logc = "!f() { git log $1 -$2; }; f" $ git logc other_branch 5
  • 20. git log ● Shows commits history starting from HEAD ● Start from a branch or a tag $ git log my_branch $ git log v3.2.2 ● Start from a commit $ git log ab199cf0 ● Add “diff” of last two commits $ git log -p -2 ● Put on some makeup $ git log --pretty=oneline ca82a6dff817ec66f44342007202690a93763949 changed the version number 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
  • 21. git log - more ● Make your own format $ git log --pretty=format:"%h - %an, %ar : %s" ca82a6d - Scott Chacon, 6 years ago : changed the version number ● There are other pretty params and format options - Check them. [#10 in references] ● Filter $ git log -4 #Last 4 commits $ git log --since 1/1/2015 #Commits after that date. You can als use “after” $ git log --committer john.smith@badrit.com $ git log --grep controller
  • 22. git log - Files ● Show the history of a file $ git log --pretty=oneline 970d421295efbc6371b5a09384142504232ac73b Renamed c.c to d.d 465b3274ec1f4dbb3179bd3486180f5fb4a9ec76 Deleted b.b d0f6f31b0569d96af93d031200e97dd545c9bec2 Edited a.a 81bfbdd2f6c8d31c2c242f5d524f9b293e2efc6e c.c d5d21265591815dce7f320fbb70c52dd99edaadc b.b ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a $ git log --pretty=oneline a.a d0f6f31b0569d96af93d031200e97dd545c9bec2 Edited a.a ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a
  • 23. git log - Deleted and Moved Files ● Works for deleted files $ git log --follow --pretty=oneline -- b.b 465b3274ec1f4dbb3179bd3486180f5fb4a9ec76 Deleted b.b d5d21265591815dce7f320fbb70c52dd99edaadc b.b ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a #First commit $ git log --follow --pretty=oneline -- d.d 970d421295efbc6371b5a09384142504232ac73b Renamed c.c to d.d 81bfbdd2f6c8d31c2c242f5d524f9b293e2efc6e c.c ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a #First commit $ git log --follow --pretty=oneline -- c.c 970d421295efbc6371b5a09384142504232ac73b Renamed c.c to d.d 81bfbdd2f6c8d31c2c242f5d524f9b293e2efc6e c.c ff55c20108cddad6da7e3e611a0dfe62cf1fced9 a.a
  • 24. git log --graph ● You can show it as a graph $ git log --graph ● Forget about the last point. Log it in a gitk-ish way ⚪ Command is tall. ⚪ Get it from http://stackoverflow.com/a/9074343/959221.
  • 26. Tag ● Mark important steps and milestones in the development ● Lightweight tag: Commit’s checksum stored in a file $ git tag v1.4 $ git tag v1.3 4d3f911 # Tag a certain commit ● List $ git tag $ git tag -l “v1.8*” # Filter to tags that has “v1.8” ● Annotated; Add a message $ git tag v9.8 -m ‘Random tag’
  • 27. Diff ● Default: Diff between working directory and staging $ git diff ● Diff between working directory and a commit $ git diff <commit> ● Diff between staging and last commit $ git diff --cached# Staging and last commit $ git diff --cached commit # Staging and given commit ● Diff between two commits $ git diff HEAD^^ HEAD # start_commit end_commit # “HEAD^^” = “Head~2”
  • 28. ● Diff one file $ git diff start_commit end_commit -- file $ git diff we45hsf -- file # Working directory and given commit $ git diff file # Working directory and HEAD ● Diff across branches $ git diff master..branch ● Diff tools $ git difftool # Launches a tool to view changes ● Candidates: meld, vimdiff, diffmerge … References #12 and #14 for more ⚪ You may need to install the chosen package $ git config --global diff.tool meld $ git config --global merge.tool meld # Diff - More
  • 30. A Commit Anatomy ● A commit contains: ⚪ A pointer to the files snapshot ⚪ Author’s name and email ⚪ The message ⚪ A pointer to its parent(s) ● Commit’s parents: ⚪ Zero parents; if it’s the initial commit ⚪ Multiple parents; if it is a merge commit ⚪ One; otherwise
  • 31. Branches ● A branch is just a pointer to a commit ● Master branch is also not different ⚪ git init creates it by default ● Whenever you commit, the branch pointer moves along with the new commit ● HEAD is a special pointer. It points to the branch that you currently on
  • 33. Branches - Anatomy @Pro Git After one commit
  • 34. Branches - Detached HEAD ● Checkout a single commit ⚪ You can even create a branch from there $ git checkout ab199cf0 You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again.
  • 35. Branches - Low Cost ● A branch is a single pointer; 40-characters file ● Creating branches and playing with them is not costly ● Sharp contrast with other VCS’s ⚪ Depending on the size of the project, creating a new branch may take minutes to copy the contents to another directory. ● Since it’s just a pointer, finding the recent common parent is easy, which makes merging easy. ⚪ You had to find that parent yourself in other systems
  • 36. ● Create a branch and move to it $ git checkout -b new_branch ● Just create it $ git branch new_branch ● Push it. Don’t forget to track it $ git push -u origin new_branch ● Move around $ git checkout new_branch # HEAD is not at new_branch $ git checkout master # HEAD is not at master ● Delete it locally $ git branch -d new_branch ● Delete it on the server $ git push origin --delete <branchName> Branches - How
  • 37. Branches - Merging ● Adds one branch’s commits to the other $ git checkout master $ git merge new_branch ● Fast-forward ⚪ Just move the pointer up ahead @Pro Git
  • 38. Branches - Merging ● Git determines the best common ancestor ● Creates a new snapshot by heads of each branch, and the BCS @Pro Git
  • 39. Branches - Merge Conflicts ● Conflicts happen when some part is edited in both branches ● The merge commit is paused until these conflicts are solved $ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: index.html ● Done editing? Add the file $ git add index.html ● If you don’t have any other conflicts, commit.
  • 40. Branches - Work Flow and Models ● How you organize stories affects how you see your Git repo ● Famous models exist ● Develop your own to suit your needs
  • 41. ● Famous levels ⚪ master: stable branch. Has been or will be released ⚪ next or staging: Code that’s ready to be tested. Not always stable, but when it is stable, will be merged to master ⚪ develop: finish topic-branches and merge them here ⚪ Add or remove, as it suits your model ● Think of it as work silo ⚪ Work on some code ⚪ When reaches some point, move it ● Why ⚪ Agile Branches - Long-Running Branches
  • 42. Branches - Long-Running Branches @Pro Git
  • 43. Branches - Topic Branches ● Can be used by any work model, any project of any size ● Steps: ⚪ Branch out from development for feature ⚪ Work on feature ⚪ Merge back into development when done ● Don’t branch out from a topic branch! Or do. There’s a debate ● Rule of thumb: If you branch B out from A, merge B back into A
  • 44. Branches - Topic Branches ● Why ⚪ Agile ⚪ No half-done code in long-running branches ⚪ Work in parallel. But you have to divide work right.
  • 45. Pull Requests ● Can be used with two models: ⚪ Fork and pull ⚪ Shared repo ● Go to the topic branch and choose “Compare & review” ● Choose target repo / target branch ● Can be reviewed before merging ● Why? ⚪ Allow for contributions while maintaining conventions ⚪ Review stories before they’re finalised.
  • 46. The Art of Committing - Messages ● Expressive and understandable. Yet don’t make a speech ● Don’t commit with -m; It’s very limited ● Format it: ⚪ First line is brief ⚪ Add a link to ticket. ⚪ Check #3 in references ● Answer questions: What was done? Why? What side effects? ● Should be integral to code review ⚪ Know what this commit is about, from the message
  • 47. The Art of Committing - Small Early Commits ● Don’t include some 10 or 20 hours of work in one commit ● Modularity ⚪ It’s required everywhere! ● Reviewable ● Traceable ● Uncommitted code won’t “crumble” on you ● Would introduce too many unneeded commits in the history? ⚪ You can edit commits later -- A little advanced
  • 48. Amend ● Edit the last commit $ git commit --amend ● Will open the editor to edit the message ● You can even add or remove files that you forgot to add $ git add Index.html $ git commit --amend # Changes in Index.html is now included in last commit ● Don’t edit public history ⚪ Have a look before you push ⚪ If it’s pushed, don’t edit it ● There are other ways to edit non-HEAD commits
  • 49. Auto-Completion ● Download the script ([11] in References) ● Add it to home directory ● Include the following in .bashrc file source ~/git-completion.bash ● Press on a tab to enjoy ● For Windows: Auto completion is auto-configured with mysysGit
  • 50. References 1. Pro Git 2. man git 3. https://sethrobertson.github.io/GitBestPractices 4. http://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message 5. http://sethrobertson.github.io/GitPostProduction/gpp.html#pre-post-production 6. http://programmers.stackexchange.com/questions/10793/when-is-a-version-control-c ommit-too-large 7. http://wildlyinaccurate.com/a-hackers-guide-to-git/ 8. http://nvie.com/posts/a-successful-git-branching-model/ 9. http://blogs.atlassian.com/2014/10/advanced-git-aliases/ 10. http://opensource.apple.com/source/Git/Git-19/src/git-htmldocs/pretty-formats.txt? txt 11. https://github.com/git/git/blob/master/contrib/completion/git-completion.bash 12. http://www.gitguys.com/topics/git-diff/ 13. http://stackoverflow.com/a/9834872 14. http://stackoverflow.com/questions/137102/whats-the-best-visual-merge-tool-for-git