Git Like a Pro

WnCC
Summer 2013

Hi. I’m Sushant.

WnCC

Today...

Git Like a Pro

Version control

  • track history of files
  • revert to a (previous) version
  • git, svn...

Distributed Version control

  • no central server
  • a clone becomes a repository
  • clone has full history
  • clones can exchange changes with a remotely hosted repo

So...

Git

Git is a free and open source distributed version control system...
  • Linus
  • C
  • > svn

Github

  • Website
  • Host your remote repos
  • Social network for nerds
  • Super awesome!

Git vocabulary

  • branch - parallel version of repository
  • clone - creating a copy of repository
  • commit - a change, or revision
  • repository - "a project folder"
  • remote - version hosted on a server
  • pull/push - get/send updates
  • fetch - get changes without merging them
  • working tree - current version of local repo

Commit

2 steps to register your changes

  • Working tree -> Staging index
    git add files.txt
  • Staging index -> Git repository
  • git commit -m "a helpful commit message"

SHA-1

  • Each commit is addressed by it's hashcode - SHA-1
  • Each commit is an object - SHA-1 is a field
  • 40 bytes long

Installing git

http://git-scm.com/downloads

  • Binaries for Win, Mac, Linux
  • Build from source
"If you want to build [git] on Windows: Don't."

Configure git

Important since Github uses email to identify user (unless using ssh)

# configure the user which will be used by git
# Of course you should use your name
git config --global user.name "Example Surname"

# Same for the email address
git config --global user.email "your.email@gmail.com" 
          

Create a repository

git init awesome-project

Or

cd ~/awesome-project
git init

Or, clone.

git clone link/to/remote

.gitignore

Specifies intentionally untracked files to ignore

cd awesome-project
vim .gitignore
# a comment
.to-exclude
!include-this.txt

Complete pattern guide here: http://git-scm.com/docs/gitignore

Committed files are not automatically removed. Use this to remove files from git repo:

git rm -r --cached awesome-file

Usual workflow

# assuming you have a repo with changes made to it

# this shows files that have been changed, and the status of stage
git status

# add all files to the stage
git add .

# commit the changes to local repo
git commit -m "a message"

log

# show git log
git log

# this is what the output looks like
commit bcf7bd065199c1e79c8dcc57a9518bd53dec6da5
Author: Sushant Hiray < hiraysushant@gmail.com>
Date:   Tue Apr 8 17:41:14 2013 +0530

    started adding files for tomorrow

Useful flags:

  • --oneline - give single line log
  • -N - give last N commits

Remotes

A remote is a URL to a remote server where code lives. Default remote is called origin

# add a new remote
git remote add < remote> < link>

# show all remotes
git remote -v

# fetches the code and merges it into your working directory
git pull < remote> < branch>
# fetches the code but does not merge it into your working directory
git fetch < remote> < branch>

# push my committed changes to remote and remember the upstread branch
git push -u origin master

Github

Clone from Github

# SSH
git clone git@github.com:wncc/wncc.github.io.git

# HTTPS
git clone https://github.com/wncc/wncc.github.io.git

Forking

  • Complete history of original repo
  • Encourages individual exploration
  • Forking != Cloning

Pull Requests

  • Merging with traceability
  • Peer-review via GitHub & Git

Ninja tricks

Correct the last commit

# because coding while drunk is bad for you
git commit -m "my boss is a dick"
# thank god for git
git commit --amend -m "my boss is a fat dick"

Check history of specific file

git log -- filename

Undo commit

# Revert to a previous commit by SHA-1 hash
git reset --hard < hash>

# or simply undo the last commit
git reset --hard HEAD^

Tagging

# tags are used to mark specific points in history

# list all tags
git tag

# add a new tag
git tag -a "v1.0"

# or add a tag for any commit
git tag -a "v1.0" < hash>

# push tags to remote
git push --tag 

Branches

# create a new branch
git branch branch-name

# list all branches in the current repository
git branch

# switch to an existing branch
git checkout branch-name

# create a branch and switch to it
git checkout -b branch-name

# delete a branch
git branch -d branch-name

# merge two branches (source into target) (make sure to be on target)
git merge source

Merge conflicts

# git is smart, but not really. when same line is changed more 
# than once at the same time, conflicts can arise

# this is what lines with conflicts look like

<<<<<<< HEAD
This is what my local repo has.
=======
This is what someone else did.
>>>>>>> 0c7ac2ab7ab93366fb662e8292706c084001fb31 

# manually select what to keep, commit and push!

Aliases

vim ~/.bashrc

# git
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push $1 $1'

# saves a gazillion keystrokes

# for those who live on the edge
alias yolo = 'git commit -am "TAG" && git push -f origin master'

Now what?

  • Signup for Github
  • Start working with git

Resources

That is all.

@SushantHiray

github.com/sushant-hiray


Questions?