Git session Dropsolid.com

Preview:

DESCRIPTION

Git session

Citation preview

Git session Hannes Tack

Overview

1.What is git?2.Basic git3.Workflow4.Tips and tricks

What is git?

What is git?

Open source distributed version control system

Distributed

• Everything is local

– Fast– Every clone is a backup–Work offline

Basic git

Installing Git

• Download from http://git-scm.com/downloads

• Launch the installer• Or Xcode, homebrew, …

Configuring Git

• $ git config --global user.name 'Hannes Tack’• $ git config –global user.email

‘hannes@dropsolid.com’

Clone a repo

Clone a repo

• Clone an existing repo or create a new one:

– $ git clone user@host:repo

.gitignore

.gitignore

• What?

–Makes git ignore files / directories– Repo specific or global– Drupal example of a .gitignore file:

http://drupalcode.org/project/drupal.git/blob/HEAD:/example.gitignore

.gitignore

# Ignore configuration files that may contain sensitive information*/sites/*/settings*.php# Ignore paths that contain user-generated content.*/sites/*files*/sites/*/private# Ignore editor specific filesDrupal.sublime-projectcompletions*.sublime-project*.sublime-workspace# Ignore OS specific files.DS_Store

global .gitignore

• Create any file:- $vi ~/.gitignore_global

• Add paths to be ignored.- Example: https://gist.github.com/4321950

• Use as global gitignore file:

Git config –global core.excludesfile~/.gitignore_global

local .gitignore

• Create a .gitignore file:- $vi ~/your_project/.gitignore

• Place it anywhere in your repo

• Add paths to be ignored, relative to the location of the file.

A Basic Workflow

A basic workflow

• Write code• Stage your changes• Review your changes• Commit your changes• Push your changes

A basic workflow

• Write code• Stage your changes• Review your changes• Commit your changes• Push your changes

A basic workflow

A basic workflow

Git diff

• Git diff shows what you’ve changed• $ git diff <filename>

A basic workflow

A basic workflow

• Write code• Stage your changes• Review your changes• Commit your changes• Push your changes

A basic workflow

A basic workflow

A basic workflow

A basic workflow

A basic workflow

Git add

A basic workflow

A basic workflow

A basic workflow

A basic workflow

Staged files• git diff --staged shows the changes

that are staged• git reset HEAD <filename> removes a

file from “staging”

A basic workflow

• Write code• Stage your changes• Review your changes• Commit your changes• Push your changes

Git commit

A basic workflow

A basic workflow

A basic workflow

Git commit• Changes are committed to local repo• Others won’t see these changes yet• Commit message is required• And should look like ‘Issue #123: describe

what you changed.’

A basic workflow

• Write code• Stage your changes• Review your changes• Commit your changes• Push your changes

Git push

A basic workflow

A basic workflow

A basic workflow

Merging

A basic workflow

• Remote branch contains commit(s) that are not present in your local branch.

• Get those commits first by merging, then push your commits.

A basic workflow

Git pull

A basic workflow

Git push

A basic workflow

Git pull

A basic workflow

Git pull

A basic workflow

Git pull

A basic workflow

Git pull

A basic workflow

Git pull

A basic workflow

Git pull

A basic workflow

Fix code manually

A basic workflow

Fix conflict UI

A basic workflow

Fix conflict UI

A basic workflow

Push merged code

Git log

Git log

- View the latest 10 log messages- $ git log -n 10

Git log

- Show nicer log:- $ git log --oneline -n 20

Git log

- Search log messages content:$ git log --grep=Breadcrumb

Git log

- Search log messages content:$ git log --grep=Breadcrumb

Git log

- Search commit content:$ git log -S "Implements hook_views_data" –oneline

Git log

- Show commit content:$ git show de25c94

Undoing things

Git checkout

Git checkout

Git checkout

• Throw away changes:$ git checkout index.html

• Restores your working tree to the last version committed to git.

• Use git checkout to throw away changes that have not been added and committed.

• Use carefully.

Git checkout

Git reset

Git reset

Git reset

• Keep your code changes, remove file from the index:$ git reset HEAD index.html

• Restores your index to the last version known to git.

Git reset

Git reset --hard

Git reset --hard

• Resets working tree and index to a specific commit.

• $ git reset --hard 5497461• Reset a branch to the origin branch:$

git reset --hard origin/master• Use carefully.

Git revert

Git revert

• Undo a specific commit:$ git revert 5497461

• Creates a new commit that removes the specified commit.

Git revert

Branching

Branching

- List local branches:$ git branch

Branching

- List all branches:$ git branch -a

Branching

- Create a new branch:$ git branch issue-123

- Switch to that branch:$ git checkout issue-123

- Or create and switch to a branch:$ git checkout -b issue-123

- Push branch to origin:$ git push origin issue-123

Branching

- Use a remote branch:$ git checkout -t origin/issue-11699

Branching

• Delete a local branch that is fully merged:$ git branch -d branch_name

• Force delete a local branch:$ git branch -D branch_name

• Delete a remote branch:$ git push origin --delete :branch_name

• Completely remove a branch:$ git push origin --delete :branch_name$ git branch -D branch_name

Tagging

Tagging

• Create tag:$ git tag sporting-beta-1 -a -m 'Sporting beta 1 release.'

• Push tag:$ git push --tags• List tags:$ git tag

Tagging

• Move a tag:$ git tag -f sporting-beta-1• Delete a tag:$ git tag -d sporting-beta-

1$ git push origin :sporting-beta-1

Patching

Patching

• $ git diff > [project_name]-[short_description]-[issue-number]-[comment-number].patch

• Add the patch file to the root of the module you patched.

Tips & tricks

Git stash

Git stash

• Store code changes without committing them.

• Handy when you urgently need to switch branches.

• Stash your code changes:$ git stash• List all available stashes:$ git stash list• Most recent stash is shown on top

Git stash

• Apply a stash:$ git stash apply stash@{0}

Git stash

Git aliases

Git bisect

• Shortcuts• Define aliases in .gitconfig file.• Ex: ~/.gitconfig

Git aliases

Git aliases

Git bisect

Git bisect

• Find out in when something broke• First, find a commit when everything

was working• Find a commit where things are not

working. • Go to the root of the repository• $ git bisect start$ git bisect good

fd0a623$ git bisect bad 256d85

Git bisect

• Refresh the page and see if the bug is there

• $ git bisect good/git bisect bad• Repeat until git tells you the commit

that broke it

Resources

Resources

• Pro git book: http://git-scm.com/book

• Git bisect tutorial: http://webchick.net/node/99

Questions?

Recommended