Download - First steps with Git - Linux Day 2016 - Enna
Francesco Pira | fpira.com
First steps with Git
Francesco Pira
fpira.com
@pirafrank
Linux Day 2016 - Enna (Sicily)
Francesco Pira | fpira.com
Who am I?
• Web developer
• btw currently sys admin
• I like Python
• I’ve founded a startup (no much fuss...)
• I write a blog in my spare time
• ...and some code (github.com/pirafrank)
• I like business
• ...but code is better!
Francesco Pira | fpira.com
What is a VCS?
• VCS stands for Version Control System
• A software to handle different versions of a pool of files
• Great to manage source code
• Something you need to get a job
• Something you need to share your weekend project
• Something you need to deploy and maintain your blog• See Github Pages, GitLab Pages and Jekyll
Francesco Pira | fpira.com
Why VCS?
• A repository as a unique codebase
• Reliability
• History and changes + easy to roll back at any point
• Blaming (who changed what?)
• Side projects (aka forking a repo)
• Parallel development on the same repo (aka team working)
• Code deployment!
Francesco Pira | fpira.com
Popular VCS software
● Git
● Team Foundation Server (MS)
● Apache Subversion (SVN)
● Mercurial (BitBucket)
● Bazaar (Canonical)* In order of popularity
Francesco Pira | fpira.com
• repository = a directory with files (usually code!)• working tree = index of files part of the repo• ignoring = no to keep track of changes• staging = virtual area with changes ready to commit• commit = snapshot of a repository, changes saved in history• head = pointer to the latest commit of the current branch• branch = a copy of files to track different changes• merging = merging the history of a branch with his parent one• conflict = 2 or more people making changes to same file• tag = named pointer to a particular snapshot (e.g. v1.1.0)• remote = server (it can be another PC for distributed VCS)
VCS concepts
Francesco Pira | fpira.com
Who’s Git?
• Created by Linus Torvalds (mid 2005)
• to manage the shared development of Linux kernel
• Today is the most common VCS
• It works using snapshots, pointers and compressed deltas
• All changes and history are in ./.git
• Works best with small files (see git lfs)
• Distribute (remote) architecture
• Used by GitHub: the biggest and de facto standard platform
for sharing open-source code
Francesco Pira | fpira.com
Setup the environment
● Download and install git
○ bit.ly/linux-day-git-install
● Set it up
○ git config --global user.name "My Name"
○ git config --global user.email "[email protected]"
● Remotes require your to have an RSA key: set it up!
○ ssh-keygen -t rsa -b 4096 -C "[email protected]"
○ eval "$(ssh-agent -s)"
○ ssh-add ~/.ssh/id_rsa
Pro tip: Use different RSA keys (personal, work, etc.)
Francesco Pira | fpira.com
Your first commit, yay!
Locally on your computer
• git init
• git add .
• git remote add origin ...
• git commit -m “Hey! This is my first commit”
• git push origin master
Your teammate
• git clone ...
Francesco Pira | fpira.com
Some notes
master is the main and first branchorigin is the primary remote. You push your code to it
Tip: Use --global to make a setting work for all repo of current local user
Interesting: Git global configuration usually lives here ~/.gitconfig or here ~/.config/git/config
Francesco Pira | fpira.com
Live demo
Francesco Pira | fpira.com
• init = initialise the repository (make ./.git subdir)• clone = make a local copy of the repo from a remote one• diff = show current changes (before staging them)• add = stage one of more file / folders• commit = make a ‘snapshot’ / save a state• branch = create a branch off the current one• merge = merge a branch into his parent• fetch = download (new) changes from remote• pull = fetch from remote branch + merge to local one• checkout = switch branch / restore file in working tree• reset = undo / remove from staging area• log = explore changes history• … tons of commands, each one has tons of parameters: RTFM!
Main Git commands
Francesco Pira | fpira.com
Lazy as a pig? Aliases!
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st 'status -s'
git config --global alias.unstage 'reset HEAD --'
...Make yours!
Francesco Pira | fpira.com
Best practices
• Don’t zip repositories! Use remotes!
• master as the main branch
• devel as the main development branch
• all features start off devel
• all features ends
• usually you don’t push features
• all releases must be tagged (in master)
• use semantic versioning for tags
• hotfix start off master, merged to master and devel
Francesco Pira | fpira.comCredits to Vincent Driessen
Francesco Pira | fpira.com
Live demo
Francesco Pira | fpira.com
UI clients
● Windows and macOS
○ Sourcetree
● Linux
○ SmartGit
Francesco Pira | fpira.com
Best platforms
● GitHub
○ Free only for open-source public projects
● BitBucket
○ Free for unlimited private or public repos (any license)
○ Paid for 5+ collaborators
● GitLab
○ Paid enterprise version (with support)
○ Free Community Edition (self-hosted)
○ Free gitlab.com: unlimited repos, unlimited collaborators
Francesco Pira | fpira.com
Your turn!
• Wow! git log --graph --oneline --decorate --all
• Discover git stash and git pop
• Deploy best practices
• Use git-flow
• Deploy a web application using git
• Try git-lfs
• ...Have fun!
Francesco Pira | fpira.com
Sources and extras
• https://git-scm.com/book/en/v2/Getting-Started-Git-Basics
• https://help.github.com/articles/generating-a-new-ssh-key-and-addi
ng-it-to-the-ssh-agent/
• https://www.atlassian.com/git/tutorials
• http://nvie.com/posts/a-successful-git-branching-model/
• http://semver.org (semantic versioning)
Francesco Pira | fpira.com
Thank you!Keep in touch
fpira.com
@pirafrank
github.com/ pirafrank