Roland's homepage

My random knot in the Web

Initializing a new git repo for a project

date: 2014-04-01
modified: 2020-07-26
reading time: 2 min.
category: howto
tags: git

This article contains a list of things I do to set up a new git repository.

Note

As of 2020, some of this is obsolete. I’ve added notes to indocate what and why.

General

Create a directory for the new project:

mkdir newproject
cd newproject

Initialize for git:

git init

Tell git which types of files to ignore, e.g. compiled Python files:

printf '*.pyc\n*.pyo' >.gitignore

Note

It is tempting to put often ignored files into your global .gitignore. However, this creates problems when you are using the repo for collaboration. In that case, it is best to put everything in the repo’s local .gitignore.

Filters

Note

Currently I don’t use filters anymore. I rely on my editor to add and modify a line to the file when it was created and when it was last modified.

Using filters we can change do keyword expansion during check-out. A filter named kw is defined in ~/.gitconfig:

[filter "kw"]
  clean = kwclean
  smudge = kwset

In this case, the scripts kwclean and kwset function like rcs-style keyword expansion. The kwset script looks for the strings $Date$ and $Revision$ and add respectively the commit date and short hash, like this; $Date: 2014-04-01 13:24:45 +0200 $ and $Revision: 206bdb8 $. The kwclean script restores the original strings.

To activate this filter for e.g. Python source code, it has to be set in a file called .gitattributes:

echo "*.py filter=kw" >.gitattributes

Do not enable filtering on binary files such as program executables, pictures such as JPEG and PNG or videos such as MP4! This will most probably screw up the file in question.

Another step that has to be taken is to force checkout of all modified files (or otherwise all files) after a commit. the former is performed by the update-modified-keywords.py script, while the latter is done by the update-all-keywords.py script.

To make use of one of these, the post-commit hook is pointed to one of them:

cd .git/hooks
ln -s ~/bin/update-modified-keywords post-commit

Start using the repo

Now you can create new files. After than the initial check-in is done:

git add .
git commit -m "Initial check-in."

For comments, please send me an e-mail.


Related articles


←  Setting the terminal title with urxvt and tcsh Keyword expansion with git  →