practical git for developers
TRANSCRIPT
Practical git for developers(aka 'Git for beginners')
Wim GoddenCu.be Solutions@wimgtr
Who am I ?
Wim Godden (@wimgtr)
Where I'm from
Where I'm from
Where I'm from
Where I'm from
Where I'm from
Where I'm from
My town
My town
Belgium the traffic
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
Who are you ?
Developers ?
CVS ?
SVN ?
TFS ?
Git ?
What is git ?
Git is a file system with a Version Control System on top of it
Git's file structure
Commit
treeauthor_infoparent
Tree
README.MDLICENSEindex.php
Blob
datadatadata...
Blob
datadatadata...
Blob
datadatadata...
6d3af...
8a3ea...
901a3...
4a39c...
e3231...
8a3ea...
901a3...
4a39c...
e3231...
What is git ?
Git is a file system with a Version Control System on top of it
What is git ?
Git is a distributed file system with a Version Control System on top of it
(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
(Almost) everything is immutable
Immutable = doesn't change
Once placed in git, it stays there
Even if you delete files
Differences with SVN
SVN keeps diffs between versions
Git keeps full snapshots
Commit #1
treeauthor_infoparent
Tree
README.MD
Commit #2
treeauthor_infoparent
Tree
README.MDindex.php
Commit #3
treeauthor_infoparent
Tree
README.MDLICENSEindex.php
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, ...)
Basic git config
$ git config --global user.name "Wim Godden"$ git config --global user.email "[email protected]"
$ git config --global core.editor vim
$ git config --global core.autocrlf input$ git config --global core.autocrlf true
$ git config --global color.ui true
(on Linux)
(on Windows)
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
Create a repo
$ git init
$ tree .git.git/ branches config description HEAD info exclude objects info pack refs heads tags
.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
git clone
Creates a repository from an existing repository
git clone https://someplace.com/repo.git
Original repository is called 'origin' This is called a 'remote'
git clone [email protected]:wimg/confoodemo.git
Git flow
Working directoryStaging areaRepositoryRemote(s)AddCommitPush
git status
Simple command
Explains in clear language what's going on
Use it, use it, use it, ...
Git flow
Working directoryStaging areaRepositoryRemote(s)AddCommitPush
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
git reset
Unstage changes
Revert to older commit version
Beware : dangerous command !
Remove files from repository
2 ways :Delete, then rmDelete the file from working directory
git rm
Commit
Just rmgit rm
Commit
Accidentially deleted ? git checkout --
Git flow
Working directoryStaging areaRepositoryRemote(s)AddCommitPush
git commit
Creates a new version
Based on the staging area
Commits on local repository only
Commit #1
treeauthor_infoparent
Tree
README.MD
Commit #2
treeauthor_infoparent : #1
Tree
README.MDindex.php
Commit #3
treeauthor_infoparent : #2
Tree
README.MDLICENSEindex.php
HEAD
git log
Shows each commit with SHA1 hash hash can be used with other commands
-p gives a diff view
git diff
No params : default diff output
git diff --staged : diff with what's been staged
Clone, add, commit and push
C1Remote origin
Local repository
C2
master
C1C2
origin/master
12C1C2
origin/master
C3
masterHEAD
masterHEAD
12C1C2
master
C1C2
origin/master
C3
masterHEAD
33C1C2
master
C3
Working with remotes
git push Send locally commited changes to the remote repository
git fetch/pull Retrieve changes from the remote repository
Fetch, merge and pull
Remote origin
Local repository
C1C2
origin/master
12C1C2
origin/masterC3
masterHEAD
masterHEAD
1C1C2
C3
masterHEAD
23C1C2
master
C3
C2
origin/master
3
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
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
Switch to branch git checkout
Create + switch to new branch git checkout -b
Show branch activity : git show-branch
Delete a branch : git branch -d
* = current brach+ = commit is in the branch- = commit is in the branch as a merge
Merging changes in a project
C1C2
origin/master
C1C2
origin/masterC3
master
master
C4
test
LICENSE : added paragraph 1
LICENSE : added paragraph 2
C1C2
origin/masterC3
master
C4
test
LICENSE : added paragraph 1 + 2
Edited LICENSE
C5
Conflicting change
git merge conflict
git status shows conflicted files
Resolve conflict
git commit -a tells git conflict was resolved
git checkout -b testedit LICENSE filegit checkout masteredit LICENSE filegit merge test will show conflictgit status will show unmerged pathedit LICENSE filegit commit -a -m'conflict resolved'
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
Creating a fork
Creating a fork
Will create a complete copy (not a clone) of the projectIncluding master, all branches, all tags, all commits, etc.
i.e. confoo/some-repo /some-repo
You can do anything in it...
But please don't if you intend to contribute back...
Which you should ofcourse ;-)
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 wellDon't : bugfix
Do : bugfix_issue_26_fatal_error
Next : add/change code and commit
Don't forget unit tests, integration tests,
Make your commit message descriptiveDon'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
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 requestDon't : this will fix my issues
Do : Added an OAuth authentication layer
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 ;-)
Git tools
git-cola (Linux, Win, Mac)
GitHub Desktop (Win, Mac)
GitKraken (Linux, Win, Mac) - beta
Liquid prompt
Useful list : https://git.wiki.kernel.org/index.php/Interfaces,_frontends,_and_tools
Questions ?
Questions ?
Thanks !
@wimgtr
[email protected]