Git101

Preview:

DESCRIPTION

Git101 presntation for iRail Summer of code

Citation preview

Git 101@jenssegers

In a world without Git

Distributed version control

• Speed

• Simple

• Non-linear development

• Fully distributed

• Large projects

Snapshots, no ∆

Distributed

Pieter

Hannes

Jens

Miet

Distributed and managed

Pieter

Hannes

Jens

Miet

Github

Pieter

Hannes

Jens

What is Github?

• Repository hosting service

• Visualization of changes

• Issue tracker

Installing Git

• WindowsGit Bash with MinGW

• OSX and LinuxTerminal

git-scm.com

Git configuration

$ git config --global user.name “Your Name”

$ git config --global user.email “your@email.com”

A new github repository

git@github.com:user/Hello.git

Creating a local repository

$ mkdir Hello

$ cd Hello

$ git init

Initialized empty Git repository in Hello/.git/

$ git remote add origin git@github.com:user/Hello.git

Git Basics

git add

Track files for the next snapshot

$ git add README

$ git add subfolder/

Add all files (including deleted files)

$ git add -A

git rm

Opposite of add, untracks file

$ git rm README

Recursive

$ git rm -r subfolder/

git commit

Create a snapshot of all the added files

$ git commit -m “Adding readme”

[master] created d9e1758: “Adding readme”1 files changed, 1 insertions(+), 0 deletions(-)

Always add an appropriate commit message!

git commit (2)

Automatically detect changed and deleted files

$ git commit -am “Adding readme”

Update a previous commit

$ git commit -am “GUI refactoring”

$ git add new_file

$ git commit --amend

git push

Push commit snapshots to a remote repository

$ git push [remote] [branch]

$ git push origin master

Writing objects: 100% (3/3), 225 bytes, done.Total 3 (delta 0), reused 0 (delta 0)

git pull

Pull updates from a remote repository

$ git pull [remote] [branch]

$ git pull origin master

remote: Total 3 (delta 0), reused 0 (delta 0)Updating d9e1758..c3e12cc

git clone

Work on an existing repository

$ git clone [origin]

$ git clone https://github.com/user/repository.git

Cloning into ...Unpacking objects: 100%, done.

Conflicts

Conflicts

A file was modified since the last pull

$ git push origin master

! [rejected] master -> master (non-fast-forward)

Inspecting the conflict

Update your repository

$ git pull

View conflict info

$ git status

Unmerged paths:both modified: README

Resolving the conflict

Open the conflicted file in your favorite editor

$ nano README

<<<<<<< HEADYes, this is Dog?=======Who is Dog?>>>>>>> a0b0bf3

]]

local repository

remote repository

Push merged file

$ git add README

$ git commit -m “Merging”

$ git push origin master

Branching

Branches

Typical branches

• MasterThe latest stable version of the project

• StagingThe branch where the a beta version is tested before it is merged into the master branch

• Feature XA branch where a specific feature is being developed

Creating a branch

From the base branch

$ git branch feature-x

Switching to the new branch

$ git checkout feature-x

Merging a branch

Switch to the base branch you want to merge in

$ git checkout master

Merge with the branch

$ git merge staging

Updating a branch

Update a feature branch with master branch bug fixes

$ git checkout feature-x

$ git merge master

Removing a branch

$ git branch -D feature-useless

Tips and tricks

.gitignore

A list of files that should be ignored

# OS generated files.DS_Store.TrashesThumbs.db

# ConfigConfig.php

When all hope is lost

Revert all changes to the last pulled version

$ git reset --hard HEAD

Credential cache

Add cache to credentials to prevent typing username and password over and over again

$ git config --global credential.helper cache

Default cache is 15 minutes, to change cache use

$ git config credential.helper 'cache --timeout=3600'

SSH keys

WARNING! Only when you do not have an existing key pair

$ ssh-keygen -t rsa -C "your@email.com"

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

Enter passphrase (empty for no passphrase): [Type a passphrase]

SSH keys (2)

Add your public SSH key to your github account (account settings)

$ cat ~/.ssh/id_rsa.pub

Test your settings

$ ssh -T git@github.com

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Good luck

thanksTim De Pauw, Pieter Colpaert, Hannes Van De Vreken