The smallest git tutorial you’ll ever need

Let me preface with this: this is over-simplified for someone just getting started with git, who does not have the time to learn all the intricacies of git, and covers the most common workflow I’ve encountered, that is:
– download code from a remote repository
– make changes
– commit and send the changes back
– get the latest from that remote repository

So, here goes:

First you’ll need to install git is it is not aleady installed.

Checkout a project

git clone http://github.com/psq/spider.git

Commit

Edit files as you see fit, then when time comes to commit your changes back in:

git status

will show you what files you’ve modified. Or added. If you added any files, add them to the files controlled by git (this also works with a new directory and will recursively add everything)

git add README.txt

Then commit all

git commit -a -m "added readme"

-m option adds a messages, -a adds all modified files to the commit

Upload your changes

Then push your changes back to the original repo

git push origin master

Download the latest version

And finally, to get any updates from the master repo (you may need to do this before doing the push above)

git pull origin master

This is overly simplified on purpose, but will give any beginner time to dive in deeper when time permits.

See this previous post for some helpful git shortcuts

global git shortcuts

Here’s my ~/.gitconfig file for convenient shortcuts. This way, you can use “git st” instead of “git status”…

[user]
name = Pascal
[color]
status = auto
diff = auto
branch = auto
[alias]
st = status
ci = commit
co = checkout
[merge]
tool = opendiff

in git | 38 Words

Git to svn (read-only)

As a follow up to last post about setting up git with gitorious

Here are the steps to mirror a git branch into svn.

First, I had to install git-svn

$ sudo port deactivate git-core
$ sudo port install git-core +svn

That did not got so well when trying to run git-svn:

error about Error.pm:
Can't locate Error.pm in @INC (@INC contains...

Something did not get copied correctly. Here’s the solution:

$ cp /opt/local/var/macports/software/git-core/1.5.2.4_1+doc/opt/local/lib/perl5/site_perl/5.8.8/Error.pm /opt/local/lib/perl5/site_perl/5.8.8/

Then create a place holder in svn so a location exist (it’s fine if it is empty):

$ svn mkdir svn+ssh://svn.nanorails.com/home/psq/svn/plugins/plugins/rur -m "creating rur"

Then add this to .git/config:

[svn-remote "nanorails"]
  url = svn+ssh://svn.nanorails.com/home/psq/svn/plugins/plugins/rur
  fetch = :refs/remotes/nanorails

nanorails will become the name of the branch

Then fetch the svn (it won’t fetch anything, but this starts the process).

$ git svn fetch nanorails

$ git branch -a
* master
  nanorails


And the new branch is here :)

Checkout that svn branch into a local branch:

$ git checkout -b local-svn nanorails
Switched to a new branch "local-svn"

$ git branch -a
* local-svn
  master
  nanorails

Now the fun part.

Using rebase will forward the local commits to the updated upstream head

$ git-svn rebase
Current branch local-svn is up to date.

Get all the goodies from the master branch

$ git merge master
Merge made by recursive.
 .gitignore                            |    1 +
 MIT-LICENSE                           |   21 ++
 README                                |   48 +++++
 Rakefile                              |   20 ++
 init.rb                               |    3 +
 lib/undo_action.rb                    |   24 +++
 lib/undo_manager.rb                   |   89 ++++++++
 lib/undo_record.rb                    |  224 ++++++++++++++++++++
 lib/undoable.rb                       |   40 ++++
 lib/undoable_helper.rb                |   23 ++
 migrations/001_create_undo_records.rb |   32 +++
 spec/rur_spec.rb                      |  132 ++++++++++++
 spec/spec_helper.rb                   |   26 +++
 spec/undo_action_spec.rb              |   46 ++++
 spec/undo_manager_spec.rb             |   10 +
 spec/undo_record_spec.rb              |  368 +++++++++++++++++++++++++++++++++
 spec/undoable_spec.rb                 |   46 ++++
 17 files changed, 1153 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 MIT-LICENSE
 create mode 100644 README
 create mode 100644 Rakefile
 create mode 100644 init.rb
 create mode 100644 lib/undo_action.rb
 create mode 100644 lib/undo_manager.rb
 create mode 100644 lib/undo_record.rb
 create mode 100644 lib/undoable.rb
 create mode 100644 lib/undoable_helper.rb
 create mode 100644 migrations/001_create_undo_records.rb
 create mode 100644 spec/rur_spec.rb
 create mode 100644 spec/spec_helper.rb
 create mode 100644 spec/undo_action_spec.rb
 create mode 100644 spec/undo_manager_spec.rb
 create mode 100644 spec/undo_record_spec.rb
 create mode 100644 spec/undoable_spec.rb

And finally, send the changes to svn

$ git-svn dcommit
Committing to svn+ssh://svn.nanorails.com/home/psq/svn/plugins/plugins/rur ...
    A    .gitignore
    A    MIT-LICENSE
    A    README
    A    Rakefile
    A    init.rb
    A    lib/undo_action.rb
    A    lib/undo_manager.rb
    A    lib/undo_record.rb
    A    lib/undoable.rb
    A    lib/undoable_helper.rb
    A    migrations/001_create_undo_records.rb
    A    spec/rur_spec.rb
    A    spec/spec_helper.rb
    A    spec/undo_action_spec.rb
    A    spec/undo_manager_spec.rb
    A    spec/undo_record_spec.rb
    A    spec/undoable_spec.rb
Committed r66
    A    Rakefile
    A    .gitignore
    A    init.rb
    A    lib/undo_manager.rb
    A    lib/undo_action.rb
    A    lib/undo_record.rb
    A    lib/undoable_helper.rb
    A    lib/undoable.rb
    A    MIT-LICENSE
    A    spec/undo_record_spec.rb
    A    spec/rur_spec.rb
    A    spec/undoable_spec.rb
    A    spec/spec_helper.rb
    A    spec/undo_manager_spec.rb
    A    spec/undo_action_spec.rb
    A    migrations/001_create_undo_records.rb
    A    README
r66 = 98602d45907206a281f597f87445397f069cdc1d (nanorails)
No changes between current HEAD and refs/remotes/nanorails
Resetting to the latest refs/remotes/nanorails

And then there was much rejoicing across the land :)

Now, to update svn, the only things to do, assuming master is where the latest is, are:

$ git checkout local-svn
$ git merge master
$ git-svn dcommit

This setup could also bring changes back from svn, which git-svn is capable of doing, but I’ll be quite happy with the read-only part.

Resources:

  1. using-git-with-svn
  2. git-svn
  3. git tutorial
  4. gitorious

Update: in fact, it looks like “git-svn dcommit” from the master branch, without merging to the “local-svn” branch first, also commits to svn.

in git | 692 Words

Getting started with Git

I’ve been using git for a bit now since Rick switched Mephisto to git, but I’ve only been using an existing repository, and not using the full thing just yet.

So as I’m starting a new project, I have been taking a few notes to get started with a new project (I’m also testing gitorious.org in the process, a free git repository provided by Johan Sørensen)

First, I created a .gitignore file to filter out anything unwanted. On a Mac, for example, it is alway a good idea to add:

.DS_Store

Then to create the initial repository and commit locally

$ cd vendor/plugin/rur
$ git init
Initialized empty Git repository in .git/
$ git add .
$ git commit

Now the big push:

$ git push git@gitorious.org:rur/mainline.git
The authenticity of host 'gitorious.org (67.207.146.32)' can't be established.
RSA key fingerprint is 1d:0f:80:3b:95:c5:a8:08:85:f6:fc:0a:b2:54:52:bb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitorious.org,67.207.146.32' (RSA) to the list of known hosts.
No refs in common and none specified; doing nothing.
fatal: The remote end hung up unexpectedly


Whoops! Not quite what I expected. Let’s try to pull first then?

$ git clone git@gitorious.org:rur/mainline.git
Initialized empty Git repository in /Users/psq/tmp/mainline/.git/
fatal: no matching remote head
fetch-pack from 'git@gitorious.org:rur/mainline.git' failed.


Hmm, not quite either.

Looking at the gitorious “faq”http://gitorious.org/about/faq provides the answer :) Add the remote and master definitions to .git/config:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
       url = git@gitorious.org:rur/mainline.git
       fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
       remote = origin
       merge = refs/heads/master

(Change to match your project).

Then, try to push again:

$ git push origin master
updating 'refs/heads/master'
  from 0000000000000000000000000000000000000000
  to   74bbfc838d4984ee81d5560daee3511fe044d36e
Generating pack...
Done counting 22 objects.
Deltifying 22 objects...
 100% (22/22) done
Writing 22 objects...
 100% (22/22) done
Total 22 (delta 0), reused 0 (delta 0)
refs/heads/master: 0000000000000000000000000000000000000000 -> 74bbfc838d4984ee81d5560daee3511fe044d36e

Finally! And checking the gitorious site, the code has been pushed!

(You can get your push and pull urls from the mainline page.

To get started locally with an existing project, just use the pull url

git clone git://gitorious.org/rur/mainline.git

Now, if I can figure out the best way to get an svn mirror from gitorious, I’ll be in business!

Source (and a good tutorial): A tutorial introduction to git (for version 1.5.1 or newer)

A final note: the project referenced here is not quite fully baked, so use at your own risk…

in git | 454 Words