Git basics

setting commit encoding

Depending on your locale setting, it might be appropriate to set up a commit encoding differing from UTF8. Each repository can have its own setting and you can set it using the following command:

git config i18n.commitencoding ISO-8859-1

config Command Documentation

cloning

A project clone is the basis for making changes to sources. Usually we need to initialize and update submodules after checking out the containing repository, so this can be simplified using the --recursive option.
If your submodule URL locations need to be changed because of differing protocols, you can not use the --recursive option as you need to change the URL strings manually after having initialized the submodules.

Simple clone command with automatic submodule initialization and update

git clone --recursive [URL]

Adjust submodule URLs after initial clone

git clone [URL]
git submodule init

Now edit .git/config and change the imported submodule-urls to your needs. P.e. change the protocol (unfortunately the urls are always ssh no matter which protocol was used to clone the repository) or disable (delete) a unneeded submodule.

Fetch the submodules:

git submodule update

clone Command Documentation

backup repository directory

git allows easy backup of the repository by just saving the contents of the .git subdirectory. As git already compresses its objects, archive compression is not needed and the tar command is just the right thing to do the job.

# change into the root directory of your repository
tar cf ../`basename \`pwd\``.git.`date +'%Y%m%d%H%M%S'`.tar .git

restoring from a repository backup

# cleanup 'old' repitory
rm -rf .git
# extract from backup
tar xf ../blabla.tar
# restore directory contents to match repository state
git reset --hard

creating a shared git repository

A shared repository is needed when multiple users need to have write access (push) to it. Check the git init manual for further description.
We use the group access mode for our repositories as the write allowed users all belong to the required unix group.
When creating a new repository, the following option needs to be specified.

--shared=group

To reconfigure an existing repository and correct the permissions use the following command:
git config core.sharedrepository group

Readjust directory permissions as described here

init command documentation
shared-git-repo-using-ssh-transport

creating a bare repository

mv oldrepo.git .oldrepo.git
git clone --bare /tmp/rpo/location newrepo.git
cd newrepo.git
cp -p hooks/post-update.sample hooks/post-update
git update-server-info
git config core.sharedrepository group
git gc --aggressive --prune=now

Check if branches were copied, if not you have to create them now:

git branch BranchName #hashOfBranch#

Adjust Permissions

sudo find newrepo.git -type f -exec chmod ug+w {} \;
sudo find newrepo.git -type d -exec chmod g+s {} \;
sudo chown -R webservd:develop newrepo.git

If you are using a front end to view repository changes (like redmine, trac, etc.), do not forget to delete and add the changed repository as former commit references are not existing anymore.

Create source package for delivery

Single archive variant

For this to work, you need to get the following tool: http://wiki.github.com/meitar/git-archive-all.sh/
It is a wrapper around the git archive command but is very useful for repositories containing submodules.

Issue the following command to create a single tar archive of the whole coast tree including subdirectories:

git-archive-all.sh --prefix coast_2010_2/ coast_2010_2.tar

Multiple archives variant

  1. git commit ...
  2. cd <3rdparty/oracle>
  3. git archive --prefix=3rdparty/installed/oracle/ HEAD | bzip2 > 3rdparty_oracle.tar.bz2
  4. cd -
  5. cd <webdisplay2/modules/Oracle>
  6. git archive --prefix=WWW/webdisplay2/modules/Oracle/ HEAD | bzip2 > CoastOracle.tar.bz2

Note: It is important to supply a trailing slash to the --prefix option, otherwise it won't work as expected...

Perform operation for all submodules:

git submodule foreach 'git archive \
--prefix coast_1.2/`echo $PWD | sed "s#$OLDPWD/##"`/ \
coast_2010_1.2 | \
7z a -si $OLDPWD/coast_`basename \`pwd\``_1.2.tar.7z'

If you need to split an archive into pieces:

split --size=5M FileToSplit SplittedName.

Resources