Skip to content
abhikp edited this page Oct 19, 2011 · 84 revisions

How is Git different than SVN?

  1. You're workspace is its own local repository.
    • You can commit changes without having a connection to the remote repository (remote) or even defining one.
    • Your local repository can track and switch between multiple remotes.
  2. Branches are a separate dimension rather than another directory.
    • Branching and merging is encouraged and purposefully made easy.
    • Branches can either be kept local or track a branch in a remote.
  3. Git locally "caches" each remote (e.g. branches, commits, tags).
    • Unless otherwise stated (e.g. git pull), commands that use a remote will always use the "cache".
      • The "cache" must manually be updated (via git fetch).
  4. Commits are stored as SHA-1 hash keys that uniquely identify the entire repository at the time of commit.
    • Git does not store changes.
    • Relatively self-contained commits can easily be pulled out of the history.
    • Merging branches can be done through rebasing.
  5. Changes are staged before they are committed.
    • You can commit specific changes and keep other ones around.
  6. Uncommitted changes can be stored away for later use (via git stash).

Commands

A quick rundown of basic commands in git. They will be explained and used with more detail in the how tos.

  • git init - Initializes the current directory as a git repository.
  • git clone - Creates a local clone of a remote repository.
  • git log - View the commit log. If given no arguments it will be for the local repository and current branch.
  • git commit - Commit staged changes.
  • git merge <branch> - Merge in a branch (similar to SVN)
  • git stash - Stashes away the current state of the working directory.
  • git rebase <branch> - Rebase the current branch with the specified branch.
  • git branch - Provides several methods to deal with branching
  • More to come...

Concepts

Fast-forward merge

In Git if you merge in a branch that is ahead of master, doing git merge <branch> will not create a merge commit, but copy the branch commits. This is on by default and a standard merge can be done with git merge --no-ff <branch>.

Git fast-forward merge

BranchOut

Instructions regarding converting your workspace and our internal workflow can be found here: https://docs.google.com/a/branchout.com/document/d/1_9xZV6c_dhRX4F3PFwB6Lv7ZLN9h2-bQuS8TtJ7dfes/edit?hl=en_US

How Tos

Step by step instructions for accomplishing various tasks with Git. The instructions use the git-test repository. To gain access to it, you will need to create a Github account, upload your computer's ssh keys, and request to be added as a collaborator (email abhik).

Basics

Logs

  • Show the diffs in the commit log
% git log -p
commit 75ed2b99ef7be390d6e53b911a09b0962775eb93
Author: Abhik Pramanik <[email protected]>
Date:   Wed Aug 3 01:23:06 2011 -0700

    Fixing the bug.

diff --git a/README b/README
index eae4da1..49154c4 100644
--- a/README
+++ b/README
@@ -1 +1,2 @@
 Hey this a test for git. Welcome, to git.
+Bug fix.

commit 94ae28061caec2c11e937e8840096f6ba2bf98b4

Commits

  • Automatically stage changes when committing - Note this will not stage new files
% rm README
% echo "ASDLKFJ" >> INSTALL
% git status
# On branch abhiks-branch
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   INSTALL
#   deleted:    README
#
no changes added to commit (use "git add" and/or "git commit -a")
% git commit -am "Deleting and modifying"
[abhiks-branch 9481b0d] Deleting and modifying
 2 files changed, 1 insertions(+), 2 deletions(-)
 delete mode 100644 README
% git status
# On branch abhiks-branch
# Your branch is ahead of 'origin/abhiks-branch' by 1 commit.
#
nothing to commit (working directory clean)
% git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   README
#
% git reset HEAD README
Unstaged changes after reset:
M   README
% git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   README
#
no changes added to commit (use "git add" and/or "git commit -a")

Stashes

% git stash list
stash@{0}: On master: Another stash
stash@{1}: On master: A stash
  • Clearing all stashes
% git stash list
stash@{0}: On master: Another stash
stash@{1}: On master: A stash
% git stash clear
% git stash list
%

Branching

  • Creating a local branch (will copy over all local commits and changes)
  • Listing the local branches - '*' indicates the checked out branch (the branch you're working in).
% git branch
* master
  a-branch
  another-branch
  • Deleting a local branch
% git branch
  a-branch
  another-branch
* master
% git branch -d a-branch
Deleted branch a-branch (was 6b1fca1).
% git branch
  another-branch
* master
  • Listing remote branches
% git branch -r
  origin/HEAD -> origin/master
  origin/master
  origin/test-branch
  • Creating a remote branch to collaborate with others
  • Checking out a remote branch - If you have a branch with the same name as the remote branch you want to track, you'll have to delete it (see above).
% git branch
* master
% git checkout test-branch
Branch test-branch set up to track remote branch test-branch from origin.
Switched to a new branch 'test-branch'
% git status
# On branch test-branch
nothing to commit (working directory clean)
% git branch -r                 
 origin/HEAD -> origin/master
 origin/abhiks-branch
 origin/master
 origin/test-branch
% git push origin :abhiks-branch
To [email protected]:abhikp/git-test.git
 - [deleted]         abhiks-branch
% git branch -r                 
  origin/HEAD -> origin/master
 origin/master
 origin/test-branch

Recovery/Patching

Git, SVN and you

Your local git repository has the ability to track an SVN repo. You have pretty much the best of both worlds except for the ability to shuffle commits around, advanced rebasing, and removing commits.

These examples will utilize the svn repo: https://pl3.projectlocker.com/BranchOut/SvnTestRepo/svn

  • Tracking an SVN repo - git svn clone -s <svn repo> <folder> Assumes the svn repo has the standard directory structure (branches, tags, trunk)
% git svn clone -s https://pl3.projectlocker.com/BranchOut/SvnTestRepo/svn git-test_svn
Initialized empty Git repository in /Users/abhik/workspace/git-test_svn/.git/
    A   INSTALL
    A   README
r2 = 4e2785cf9c12d7b36caf5cfc377ac89e99bbac7f (refs/remotes/trunk)
Checked out HEAD:
  https://pl3.projectlocker.com/BranchOut/SvnTestRepo/svn/trunk r2
% git log
commit 4e2785cf9c12d7b36caf5cfc377ac89e99bbac7f
Author: [email protected] <[email protected]@ba991f9a-d5de-4771-8651-3090c57d9f48>
Date:   Mon Aug 15 22:14:53 2011 +0000

    Creating a standard svn workspace
        
    git-svn-id: https://pl3.projectlocker.com/BranchOut/SvnTestRepo/svn/trunk@2 ba991f9a-d5de-4771-8651-3090c57d9f48

FAQ

Q - When I perform a command with origin/master, origin/master seems to be out of date or missing commits?

A - Run git fetch to sync the "cache" back up with the server.

Helpful Links

Here are some links to help you transition into using Git.

  • Pro Git - Great online book that provides a deeper understanding of Git and step by step instructions like the How Tos section.
  • SVN Crash Course - A crash course that shows how to accomplish basic SVN commands in Git.
  • 20 Common Git Commands

Clone this wiki locally