A practical step-by-step guide to Git, taking you through each phase of a project and explaining the use of Git at each step of the development process. Expect lots of how-to, but also some how-not-to, to avoid going down the wrong path.
12. Who am I ?
Wim Godden (@wimgtr)
Founder of Cu.be Solutions (http://cu.be)
Open Source developer since 1997
Developer of PHPCompatibility, OpenX, PHPConsistent, ...
Speaker at Open Source conferences
13. Who are you ?
Developers ?
CVS ?
SVN ?
TFS ?
Git ?
14. What is git ?
Git is a file system
with a Version Control System on top of it
16. What is git ?
Git is a file system
with a Version Control System on top of it
17. What is git ?
Git is a distributed file system
with a Version Control System on top of it
18. (Almost) everything is local
Clone = copy of entire repository
Work offline :
Perform a diff
View file history
Commit changes (!)
Create, merge or switch branches
etc.
Only push/pull are not local
19. (Almost) everything is immutable
Immutable = doesn't change
Once placed in git, it stays there
Even if you delete files
20. Differences with SVN
SVN keeps diffs between versions
Git keeps full snapshots
Commit #1
tree
author_info
parent
Tree
README.MD
Commit #2
tree
author_info
parent
Tree
README.MD
index.php
Commit #3
tree
author_info
parent
Tree
README.MD
LICENSE
index.php
21. Differences with SVN
SVN is centralized
↔ Git is distributed
An SVN commit → shared with everyone
↔ A Git commit is local
SVN has revision increments
↔ Git has SHA1 hashes to identify objects
(commits, trees, blobs, ...)
23. Basic git - .gitignore
Allows you to specify files to ignore
Can be specific to repository
Hint : set up a global .gitignore for :
thumbnail.dat
desktop.ini
.DS_Store
Your local editor's files
…
24. Create a repo
$ git init
$ tree .git
.git/
├── branches
├── config
├── description
├── HEAD
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
25. .git directory
config – Configuration file
refs/heads/... - branches
refs/tags/... - tags
refs/remotes/... - remotes
objects/... - the actual objects
HEAD – the current working space, points to one of branches
26. git clone
Creates a repository from an existing repository
git clone https://someplace.com/repo.git <somefolder>
Original repository is called 'origin'
→ This is called a 'remote'
30. git add
Add file in current state to the staging area
Can be new file
Can be modification
git add -A stages all
git add . stages new and modified, but no deletions
git add -u stages modified and deleted, but no new files
34. git commit
Creates a new version
Based on the staging area
Commits on local repository only
Commit #1
tree
author_info
parent
Tree
README.MD
Commit #2
tree
author_info
parent : #1
Tree
README.MD
index.php
Commit #3
tree
author_info
parent : #2
Tree
README.MD
LICENSE
index.php
HEAD
35. git log
Shows each commit with SHA1 hash
→ hash can be used with other commands
-p gives a diff view
36. git diff
No params : default diff output
git diff --staged : diff with what's been staged
38. Working with remotes
git push
→ Send locally commited changes to the remote repository
git fetch/pull
→ Retrieve changes from the remote repository
39. Fetch, merge and pull
Remote originLocal repository
C1 C2
origin/master
1 2
C1 C2
origin/master
C3
master HEAD
master HEAD
1
C1 C2 C3
master HEAD
2
3
C1 C2
master
C3C2
origin/master 3
40. git fetch + merge vs git pull
git fetch : fetches information from remote into local repository
but nothing more
git merge : merges changes that were fetched
needs a source parameter
merges TO wherever HEAD is pointing to :
git merge origin/master
git pull : does both at the same time
41. Branches
Branches are separate full-blown versions of your code
Default branch = master
Which branches are there ? → git branch
Create a new branch → git branch <branchname>
Switch to branch → git checkout <branchname>
Create + switch to new branch → git checkout -b <branchname>
Show branch activity : git show-branch
Delete a branch : git branch -d <branchname>
43. Conflicting change
git merge <branchname> → conflict
git status → shows conflicted files
Resolve conflict
git commit -a → tells git conflict was resolved
44. Contributing to a Github project
Github is built on Git, so...
Clone, commit, pull, merge, push are all the same
But :
You need to fork the project first
Changes to the main repository must be requested through a
pull request
46. Creating a fork
Will create a complete copy (not a clone) of the project
Including master, all branches, all tags, all commits, etc.
i.e. confoo/some-repo → <your_github_username>/some-repo
You can do anything in it...
But please don't if you intend to contribute back...
Which you should ofcourse ;-)
47. Next : create a branch for your feature/bugfix
Why ?
Work on multiple features/fixes at same time
Experiment without damaging your master
master should always be a fully functioning, deployable version
Name the branch well
Don't : bugfix
Do : bugfix_issue_26_fatal_error
48. Next : add/change code and commit
Don't forget unit tests, integration tests, …
Make your commit message descriptive
Don't : fixed it
Do : added real-time updates on dashboard
Each commit should contain a single feature or bugfix
→ Allows project maintainers to add small blocks of code
→ Easier than adding 50 features/bugfixes
→ Easier to test
49. Next : create a Pull Request (PR)
When you want to contribute
Partial code = OK
→ But only if you want feedback
Otherwise :
Finish your code
Make sure you have unit tests
Be descriptive in your pull request
Don't : “this will fix my issues”
Do : “Added an OAuth authentication layer”
50. Next : merging the PR
Done by a project maintainer (could be you !)
Merge from the PR branch to master
Again : have a clear merge message
→ On Github : 'Closes #56' will close Github issue and links
Congratulations, you're a Github contributor ;-)
* = current brach
+ = commit is in the branch
- = commit is in the branch as a merge
git checkout -b test
edit LICENSE file
git checkout master
edit LICENSE file
git merge test → will show conflict
git status → will show unmerged path
edit LICENSE file
git commit -a -m&apos;conflict resolved&apos;