Git - The stupid content tracker

Robert Boone

Houston.pm

What we will cover

Where did git come from

  • It was written by Linus Torvalds in April 2005
  • Created mostly in response to the linux kernel losing the use of BitKeeper
  • Linus look for another scm and found Monotone
  • The problem was Monotone was too slow
  • So Linus wrote git based on the way he used BitKeeper and the core ideas of Montone
  • Git History

Features of git

  • Distributed development. Like most other modern version control systems, Git gives each developer a local copy of the entire development history, and changes are copied from one such repository to another. These changes are imported as additional development branches, and can be merged in the same way as a locally developed branch. Repositories can be easily accessed via the efficient Git protocol (optionally wrapped in ssh for authentication and security) or simply using HTTP - you can publish your repository anywhere without any special webserver configuration required.
  • Strong support for non-linear development. Git supports rapid and convenient branching and merging, and includes powerful tools for visualizing and navigating a non-linear development history.
  • Efficient handling of large projects. Git is very fast and scales well even when working with large projects and long histories. It is commonly an order of magnitude faster than most other version control systems, and several orders of magnitude faster on some operations. It also uses an extremely efficient packed format for long-term revision storage that currently tops any other open source version control system
  • Cryptographic authentication of history. The Git history is stored in such a way that the name of a particular revision (a "commit" in Git terms) depends upon the complete development history leading up to that commit. Once it is published, it is not possible to change the old versions without it being noticed. Also, tags can be cryptographically signed.
  • Toolkit design. Following the Unix tradition, Git is a collection of many small tools written in C, and a number of scripts that provide convenient wrappers. Git provides tools for both easy human usage and easy scripting to perform new clever operations.
  • Source

Basic Git commands

Creating a git repository:

mkdir repository
cd repository
git init
ls -la
drwxr-xr-x   6 robert  staff   204 Oct 11 12:02 .
drwxr-xr-x  98 robert  staff  3332 Oct 11 11:06 ..
drwxr-xr-x  16 robert  staff   544 Oct 11 11:52 .git

ls .git
branches config  description  HEAD  hooks  index  info  logs  objects  packed-refs refs

Basic Git commands

Introduce yourself to git:

git config --global user.name "name"
git config --global user.email "name@email.com"
  • --global writes information to ~/.gitconfig
  • Without --global information is written to .git/config

Basic Git commands

Adding files to a git repository:

echo "Hello, World" > greeting.txt
git add greeting.txt
git commit -m "Initial checkin"

Created initial commit 4fe4bab: Initial checkin
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 greeting.txt
 
git show
commit 4fe4bab619edfa70421ae686f1a3e81e3d390f4e
Author: Robert Boone 
Date:   Sat Oct 11 12:05:47 2008 -0500

    Initial checkin

diff --git a/greeting.txt b/greeting.txt
new file mode 100644
index 0000000..3fa0d4b
--- /dev/null
+++ b/greeting.txt
@@ -0,0 +1 @@
+Hello, World

Basic Git commands

Making changes

$EDITOR greeting.txt # remove comma
git status
# On branch master
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#
#       modified:   greeting.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

git diff
diff --git a/greeting.txt b/greeting.txt
index 3fa0d4b..557db03 100644
--- a/greeting.txt
+++ b/greeting.txt
@@ -1 +1 @@
-Hello, World
+Hello World

git add greeting.txt
git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       modified:   greeting.txt
#
All changes must be added to the git index before they can be committed

Basic Git commands

Making commits

git commit -m 'Log message'
git commit -a -m 'Log message'
git commit -s -m 'Log message'
  • Without -m you will be loaded into $EDITOR for the log message. Git will not commit without a log message
  • -a automatically adds any tracked files that have changed
  • -s "Signs" the commit by adding "Signed-off-by: name " to the log message

Basic Git commands

Logs

git log
git log -p
git log --pretty={email|full|fuller|medium|oneline|raw|short|format}
  • Shows basic commit logs
  • -p shows the commit log and the diff for that commit
  • --pretty determines the format of the log message

Basic Git commands

Tags

git tag 1.0.0
git tag 1.0.0 <commit>
git tag -a 1.0.0
git tag -s 1.0.0
git tag -u 1.0.0
git tag -d 1.0.0
  • Tag the current commit
  • Tag the given commit
  • -a Annotated tag (preferred method
  • -s Tag and sign commit
  • -u Tag and gpg sign commi
  • -d Delete the commit

Branching and Merging

Branch commands

git branch
git branch -r
git branch -a
git branch new_branch
git branch -d branch
git branch -D branch
git branch new_branch other_branch
git branch --track branch remote/branch
  • Show local branches
  • Show remote branches
  • Show local and remote branches
  • Create new branch base of the current branch
  • Delete local branch
  • Force delete if there are commits in branch that are not a strict subset of your current HEAD
  • Create new_branch based on other_branch
  • Create a branch to track a remote repo

Branching and Merging

Checkout commands

git checkout branch
git checkout -b new_branch
git checkout -b new_branch branch
git checkout --track branch remote/branch
  • Checkout the branch in the current working director
  • Create new_branch based on the current branch and switch to i
  • Create new_branch based on the given 'branch' and switch to i
  • Create a branch to track a remote repo and preform checkou

Branching and Merging

Merging commands

git merge branch
git rebase master
  • Merge branch into current branch
  • Bring current branch up to date with another branch without merging

Branching and Merging

Rebase

What's the difference between merge and rebase

                  o-o-o <- branch
                 /
            o-o-o-o-o <- master

Merge
                   Merge point
                        |
                  o-o-o-o <- branch
                 /     /
            o-o-o-o-o <- master

Rebase

                      o-o-o <- branch
                     /
            o-o-o-o-o <- master

Rebase is used when you have a topic branch you want to keep up to date then merge back to the original branch on your local repository. Merge can also be used for this locally but merge is the recommended to get new changes in to a long lived branch living on the remote repository.

Remote Repositories

Cloning

git clone git://remote.site/repo/dir working_dir
git clone remote.site:/repo/dir working_dir
git clone http://remote.site/repo/dir working_dir
git clone /tmp/repo/dir working_dir
git clone --bare /tmp/repo/dir /var/www/dir.git
  • Clone via git protocol
  • Clone via ssh
  • Clone via http
  • Clone via filesystem
  • Make a bare repository that contains no working directory

Remote Repositories

Pushing and pulling

git pull
git push origin master
git push origin master:mybranch
git push origin --all
git push remote.site/repo.git local_branch:remote_branch
git push origin :branch
git push origin --tags
git fetch origin master
git fetch origin remote_branch:local_branch
git fetch remote.site/repo.git master:master
  • Fetch and merge into a tracking branch
  • Push master branch from origin to local master
  • Push master branch from origin to new remote mybranch
  • Push all branches to remote
  • Push master from the remote repo into master source:destination
  • Delete remote branch
  • Push any tags to origin
  • Fetch master branch from origin to local master
  • Fetch remote_branch from origin to local local_branch
  • Fetch master from the remote repo into master

Making a remote repository

Creating a remote repository

cd repo
git clone --bare . ../repo.git
cd ..
scp -r repo.git remote.site:/git
rm -rf repo.git
cd repo
git remote add public remote.site:/git/repo.git
git fetch public
git chechout -b topic

git commit -a
git push public topic:topic
  • A bare repository is just the .git without the working directory
  • "git remote add" creates a alias to the full repository name

Working with subversion

git svn clone  -T trunk -t tags -b branches svn_url
git svn clone -s  svn_url
git svn clone /branch svn_url
git svn fetch
git svn rebase
git svn dcomit
  • Create new git repo from subversion repo
  • Create new git repo from subversion repo with standard layout
  • Bring over just one branch from svn
  • Grab any new commit from subversion repo
  • Bring master branch upto date after fetch
  • Commit back to subversion