[ SDF Public Access UNIX System .. Est. 1987 ]

join welcome faq status members projects store tour gopher abuse dialup
nihongo chinese europa usermap webmail forum gallery elearn irc tutorials software telnet ssh
scmgit basics

Introduction

git is a source control management tool similiar to CVS or SVN. This tutorial will give enough information to:

Unfortunately in it's current form it will not cover multiple users using the same git repo.
The best reason why a person would use git instead of cvs or svn is that git does not require a central server. For example, lets say you were taking out a CVS repository to your laptop and would not be able to connect to the server for a month. When you get back, that entire months worth of work is seen as a single diff. Which you would have to merge by hand to the other changes, but also you couldn't differentiate between reasons you changed files. For example lets say you fixed a bug and added a new feature. Which filechanges were for the new feature? Which for the bugfix? CVS has no clue. Enter git. You can locally "commit" changes without talking to the server, so you could have done a commit after the new feature and a commit after the bug fix and to the repository they are two unique changes.

Configuring your account to use git on sdf

First, you must be MetaARPA to use git.
Second, the git pkg installs most binaries you need in /usr/pkg/libexec/git-core. This needs to be in your PATH. The easiest solution is to edit ~/.bash_profile and ~/.bashrc to read:
export PATH=/usr/pkg/libexec/git-core/:${PATH}

Now any SSH session will have the necessary git binaries in the PATH.

Creating a central git repository on SDF

I suggest creating a subdirectory that will hold nothing but your git repositories.. lets say it's ~/git. The server copy of the git repository need not be a useable repository (IE, the files under revision control do not need to exist.. we just need the git database to exist so we can clone from it). Under git this is called a "bare" repository.

Create the server repository

cd ~
mkdir git
mkdir myproject
git --bare init

And that's it! on the server side. This remains empty until you first "push" your project to the server.

Creating your local git repository.

Let's assume you already have a project you want to start watching under git, with the files
~/proj/
~/proj/include
~/proj/test.c
~/proj/include/test.h

First, initialize the git project:

cd ~/proj
git init

Now your repository is initialized! Time to check in your current project. First we add the files to the repository. I like to manually add each file instead of doing a "commit all" because "commit all" tends to collect files you never wanted to add to source control (object files, temp editing files, etc).

git add test.c include/test.h
git commit

If the commit failed, follow the directions onscreen to configure your username and email so git can track you as a user in the repository.
Now time to connect your local copy to the repository on sdf.

git remote add origin ssh://user@sdf.lonestar.org:~/git/proj
git push origin master

Git should ask for your password, and then tell you it uploaded the objects and that everything succeeded.
If not, ask on the sdf forum for advise.

Copying your central repository to a client machine

Last thing: Now that you have a central copy, how do you check it out? use "git clone":

git clone ssh://user@sdf.lonestar.org:~/git/proj

Backing up all your existing git repos to a remote server

sdf doesn't backup your git repository.. while any cloned git tree is basically a backup it'd be nice to have an "official" backup to go along with your now "official" git server on sdf.
Here is a script that will, in sequence: The script is expecting you to have a directory called ~/git, and under that directory have your git projects named as ~/git/proj1.git, ~/git/proj2.git, etc. Otherwise modify it as you see fit.

The script, git-backup.sh

#!/usr/pkg/bin/bash

cd ~/git
mv git-summary git-summary-old
for i in *.git; do
  cd $i
  git log --pretty=oneline >> ../git-summary
  cd ..
done

diff git-summary git-summary-old
if [ "$?" != "0" ]; then
   #tgz up the whole git directory with today's date
   cd ..
   rm git-latest.tgz
   tar -cvzf git-latest.tgz git
   ftp ftp.your-host.com <<WOOPWOOPWOOP
   cd git-backup
   rename git-latest.tgz git-backup.tgz
   put git-latest.tgz
   quit
WOOPWOOPWOOP
fi

Note: For the FTP to work, edit your ~/.netrc file so that ftp.your-host.com has an entry that looks like:

machine ftp.your-host.com
    login your-user-name
    password your-password
    bin
Also ensure to run "chmod 600 ~/.netrc" to hide your credentials to the rest of the world :). Now add this to your (daily or less) cron tasks with mkcron (MetaARPA only, just like git ;) and enjoy your timely backups!

TODO

updating your local copy with changes made to the repo
merging/branching
Best look online for more in-depth tutorials.. I haven't needed these features yet as my projects are all just me, so I don't know how to do it!

$Id: scmgit-intro.html,v 1.3 2008/08/09 15:06:04 th0mas Exp $

©1987-2010 SDF Public Access UNIX System, Inc. 501(c)(7)
(this page was generated using ksh, sed and awk)