git-for-you-and-me



git-for-you-and-me

0 0


git-for-you-and-me

DrupalCorn 2015 talk about Git.

On Github alimac / git-for-you-and-me

Git for you and me

DrupalCorn 2015

bit.ly/git-drupalcorn

- Slides and speaker notes available online

Alina Mackenzie

University of Illinois at Chicago

alimac

czaroxiejka

char • o • jay • ka

- Developer and system administrator based in Chicago - Doing Drupal for ~4 years, active in the community for ~2 years - Using git for ~2 years

Plan

- Beginner session: introducing concepts, sprinkling with tips - Healthy dose of resources for beginners

Why?

history

undo

experiment

- "Everybody's doing it." - 3 reasons: 1. write project history 1. undo changes (thanks to history) 1. experiment and track changes

How?

single user

collaborative

- single-user: local, or local+remote - collaborative: local+remote - code, websites, books, slides

Distributed or centralized?

Centralized

- Client-server model - Local checkout of recent files - Full history of the project is on a central server - Example: CVS, Subversion

Distributed

- Each developer works directly with their own local repository - Changes are shared between repositories as a separate step - Full history in every repository - Agreement on which is the authoritative repository - Example: Mercurial, Git

Hosting

- GitHub: free public repos, pay for private. GitHub Education: student developer pack - BitBucket: unlimited public and private, free for up to 5 users, academic plan - GitLab: free unlimited public/private repositories, unlimited private collaborators

Self-hosted

- Community edition. Free. - Need your own server. - Digital Ocean has one-click install.

Command line

Update to the latest version (2.5.0)

- Download for Windows, comes with OS X, Linux - Tip: update for latest features

Apps

- SourceTree: OS X, Windows. Free - GitHub for: OS X, Windows. Free. GitHub Desktop - join waitlist - SmartGit: cross-platform. Commercial license, non-commercial license

SourceTree

- Same concepts (add, remove, branch, merge) apply - At-a-glance view of the repository vs. command at a time

Configure git

  • system
  • global
  • local
Configuration has scope: - system-wide (for all users of a system) - global (for all of user's projects) - local (for a specific project)

Name and email address

git config --global user.name "Alina Mackenzie"
git config --global user.email hello@alim.ac

Try these

git config --global color.ui true
git config --global core.excludesfile /path/to/home/directory/.gitignore
git config --global core.whitespace trailing-space
- have git use colors in output - ignore certain files globally: .DS_Store, *.swp - show trailing whitespace

Sample project

  • local repository
  • remote repository

Packing list

  • packing-list
    • README.md

# Packing List

Things to bring.

Initialize repository

$ cd packing-list
$ git init
  Initialized empty Git repository in /Users/alimac/packing-list/.git/

  • packing-list
    • README.md
    • .git
- .git/ is a hidden folder where your repository's snapshots and history are stored

Phases

Untracked

Tracked

Staged

Committed

Pushed

- Untracked: Git sees file, but doesn't know about it - Tracked: Git knows about file and tracks changes - Staged: Changes are ready to be commited - Committed: Changes to the file are saved to project history - Pushed: Changes are pushed to remote repository

Sidekicks

Where am I?

What is going on?

What just happened?

- The "three sidekicks" are 3 useful commands for orienting yourself in a project.

git status

$ git status
  On branch master

  Initial commit

  Untracked files:
    (use "git add <file>..." to include in what will be committed)

    README.md

  nothing added to commit but untracked files present (use "git add" to track)
- tells us which branch are we on - Initial commit means there is no history written yet - shows untracked files - gives hints

git add

$ git add README.md
$ git status
  On branch master

  Initial commit

  Changes to be committed:
    (use "git rm --cached <file>..." to unstage)

    new file:   README.md
- git add has 2 uses: - start tracking a file - stage a file that is ready to be committed

README.md

# Packing List

Things to bring.

laptop
skirt with pockets
toothbrush
- adding a few items to the list

git status

$ git status
  On branch master
  Initial commit

  Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)

    modified:   README.md

  Changes not staged for commit:
    (use "git add <file>.." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   README.md
- Still initial commit - Some changes are staged, some are not - Git keeps track of sets of changes within the same file

git diff

$ git diff
  diff --git a/README.md b/README.md
  index daa8d60..da0a4b2 100644
  --- a/README.md
  +++ b/README.md
  @@ -1,3 +1,7 @@
   # Packing List

   Things to bring.
  +
  +laptop
  +skirt with pockets
  +toothbrush
- git diff shows changes between two versions of a file, changes that have not been staged yet - added lines are prefixed with a + - removed lines are prefixed with a -

git commit

$ git commit -m "Initial commit."
  [master (root-commit) 1c50eb4] Initial commit.
  1 file changed, 3 insertions(+)
  create mode 100644 README.md
- To commit is to write something down as history - git commit -m is a shortcut to do the commit in one line - git commit will open a text editor to write the commit message

git log

$ git log
  commit b9fd7596e44051a99b683a19982c592fdcbca5a5
  Author: Alina Mackenzie <hello@alim.ac>
  Date:   Wed Mar 8 12:10:25 2015 -0500

    Initial commit.
- git log shows commit hash, author, date and message

Commit

A set of related and reversible changes that function together as a unit of work. Alina Mackenzie alimac

- up to each developer to decide - when on a team, agree on conventions and document them - beware of unrelated changes, changes outside of the scope - base commits on tickets

Interactive staging

git add -p
- git will ask which changes to stage - But it will only ask about changes to tracked files - Use git status to find any new, untracked files

README.md

# Packing List

Things to bring.

## Technology
laptop

## Clothes
skirt with pockets

## Toiletries
toothbrush
- Adding headings to separate items into categories

git add -p

diff --git a/README.md b/README.md
index da0a4b2..9e775a7 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,11 @@

 Things to bring.

+## Technology
 laptop
+
+## Clothes
 skirt with pockets
+
+## Toiletries
 toothbrush
Stage this hunk [y,n,q,a,d,/,s,e,?]?
- y: yes, n: no, q: quit - a: stage this hunk and all later hunks - /: search for a hunk matching the given regex - d: do not stage this hunk or any of the later hunks in the file - s: split, e: edit, ?: help

Split

Stage this hunk [y,n,q,a,d,/,s,e,?]? s
Split into 3 hunks.
@@ -2,4 +2,5 @@

 Things to bring.

+## Technology
 laptop
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?
- changes are split into smaller chunks, or hunks - git add -p: Stage only the changes you want. - git reset -p: does the same, but for unstaging.

Editor

git config --global core.editor sublime
Change default editor to Sublime Text, Atom

Message

Add short descriptive subject line.

Longer description in the form of paragraphs
that include reasons for the change, reference
tickets or other commits.
- Imagine saying "This commit will..." before writing the message - Short subject, blank line, longer message - Prefix with a ticket number - Be consistent

Push

git add remote origin git@bitbucket.org:alimac/packing-list.git
- Add a remote repository - "origin" is a convention, use any meaningful name

git push

git push -u origin master

Undo

- Many ways to undo - Depends on which phase you are in

Not committed yet

Unstage a file

$ git reset HEAD README.md
$ git reset -p

Undo all changes to a file

$ git checkout -- README.md

Committed but not pushed?

Update last commit

$ git commit --amend

Undo last commit

$ git reset --soft HEAD~1
$ git reset HEAD~1
- You can always amend the last commit - Undo a commit and keep files staged - Undo a commit and unstage files

More than one commit

Undo last 3 commits

$ git reset --soft HEAD~3
$ git reset HEAD~3

Undo a specific commit

$ git revert 617ff153
- Undo last 3 commits - Undo last 3 commits and unstage files - Revert a specific commit. This will create a new commit.

Pushed?

Don't do it.

- If you pushed to remote repository, use revert to undo. - If you are the only contributor, git push -f <remote> <branch> will force an update

Flowchart

Undoing changes in Git Emma Jane Hogbin Westby emmajane

Branch

- cheap way to create copies of the project - multiple copies in one directory - easy to integrate

git status

- Starting point - "master" is pointer to our latest commit - "HEAD" is a special pointer that points to which branch we are on

git branch test

- Create branch named "test" - "test" is a pointer to the same commit

git checkout test

- Switch to "test" branch - "HEAD" pointer points to "test"

Make a commit to test branch

- Experiment and commit changes

Switch back to master

- HEAD now points to master branch - Branches are typically created for new features, hotfixes - Some projects (Drupal) use a branch per major version

Merge

$ git checkout master
$ git merge test
- git merge is one way we can integrate changes from one branch to another - When we do a merge, a couple of things could happen

Fast-forward merge

- A fast-forward merge is the simplest type of merge - Let's review: atfer git checkout master HEAD now points to master branch

git merge test

- There are no new commits on the master branch - Advance master to where test is pointing

Three-way merge

- new commit on test, new commit on master

Merge commit

- Merge commit ties the knot between two branches

Rebase

$ git checkout test
$ git rebase master

Commits exist on both branches

- Different commits on each branch

Step 1 of rebase

- In step 1, git temporarily "lifts" all of the commits from the branch we are rebasing ("test") - Then advances the branch that is being rebased to the same place as the branch we are rebasing on.

Step 2 of rebase

- In step 2, git applies the commits that were "lifted" - Do a fast-forward merge to join branches - Useful when we need to keep a separate branch (publishing on GitHub pages) - Asterisk on commit 5 because its commit hash will be changed - git rebase rewrites history

Conflicts

- Conflicts occur when same lines are modified in different ways

README.md

Things to bring.
- What if this line were modified in different ways, by two different contributors?

Contributor 1

Things to bring on a trip.

Contributor 2

Things to bring on an expedition.
- Both contributors modify the same line, to add different words. - When the second person tries to merge or rebase, git will tell them there is a conflict.

  • stop activity (merge, rebase)
  • list files with conflicts

  • resolve conflicts
  • stage files to mark as resolved

Conflict markers

<<<<<<< HEAD
Things to bring on a trip.
=======
Things to bring on an expedition.
>>>>>>> expedition-branch
- 7 less-thans, 7 greater thans, separated by 7 equals signs - Replace text between markers with your resolution - Delete markers

Resolve

Things to bring on a trip.

git add README.md

Next steps?

- Do not be afraid to take the next step - Experiment with git commands - There is (usually) more than one way to do a thing - Write down your conventions

Git Immersion

gitimmersion.com

- guided tour that walks through the fundamentals of Git - inspired by the premise that to know a thing is to do it - for step-by-step learners

Try Git

try.github.io

- created by Code School, sponsored by GitHub - in browser tutorial - for instant gratification

Visualizing Git Concepts with D3

onlywei.github.io/explain-git-with-d3

- D3.js is a JavaScript library for data visualization - Animates git concepts like branching, pushing, merging - for visual learners of git concepts

Git for Teams

gitforteams.com

- comprehensive look at using git for teams of people - for people who are looking for strategies to integrate git into existing teams

Pro Git

git-scm.com/book/en/v2

- in-depth look at git, including what's inside git - for people who like to take things apart to learn

git commit -m "Thank you, DrupalCorn."

github.com/alimac/git-for-you-and-me

Git for you and me DrupalCorn 2015