Maintain git repository in Linux

Maintaining local clones of git repositories in Linux could become tiresome especially if you are a software developer who:

  • usually works in slow Internet Connection and has low Internet bandwidth
  • switches a lot between various systems having windows OS or Linux or both
  • is working on a wide range projects all in different domains.
  • is using Puppy Linux ( mostly ) for Software development.
  • has standard (frugal) installation of Puppy Linux or frugal installation of any other Linux Distro.
  • needs to move a lot of git repositories around the system.

Now, git is a wonderful tool to maintain your software’s source code. Agreed. I prefer it over svn and mercurial too. But maintaining a lot of local clones can sometimes become a nightmare.

Say for instance, you have 50 git repositories, and you have to move all of them to different location . What do you do ?? Write a quick bash script to do the job ?? Well you could, but then you will have reconfigure those individual repositories.

Tiresome isn’t it ?

Well, it was for me too, Luckily I was helped by one of my friend who actually introduced me to Linux long back. She had suggested me to use separate partition file for all my git repositories, which is what I am going to explain in this blog post.

Creating space for git repositories

First of all we will be needing a space where we could keep all your git repositories. This space would be special as moving it will not change the git history of the repositories residing in it.

Creating a Raw Image

Start off by making a raw image:

#root dd if=/dev/zero of=git_repo.4fs bs=1M count=32

We will get some output such as this:

32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.0963162 s, 348 MB/s

This creates (34 MB) of raw image.
After this if we check our current folder in which we ran this command we will get a file named git_repo.4fs.

root# pwd 

Checking with ls or using GUI to navigate to that folder and we will find the file.

root# ls -las

With this we find the file name in the output somewhere.

    4 drwxr-xr-x  3 root root       4096 Jun 10 16:25 .
    4 drwxr-xr-x 14 root root       4096 Jun  9 16:50 ..
32768 -rwxr-xr-x  1 root root   33554432 Jun 10 16:25 git_repo.4fs

Making a Partition File

Now we have a raw image which we could hopefully convert to a partition file...

To convert it to a partition file we will make use of mkfs command. As we have put in the extension of file as .4fs we will be converting this to an ext4 (file) partition.

root# mkfs -t ext4 git_repo.4fs

We will be asked that this not a block special device, and that would we want to proceed anyways ? Confirm by putting in y and pressing enter.

mke2fs 1.42.9 (4-Feb-2014)
git_repo.4fs is not a block special device.
Proceed anyway? (y,n) y

If successful, we will get an output as such:

Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
8192 inodes, 32768 blocks
1638 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33554432
4 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
       8193, 24577

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done 
Writing superblocks and filesystem accounting information: done

Now our file partition is ready. If you are using Puppy Linux , just click the file from GUI and it will be mounted like any partition would. On the other hand, if you are using any other distro of Linux, you would have to mount it using mount command.

Mounting and unmounting the partition file

To mount the partition file, first start of by making a empty directory in /mnt/ folder.

Change to mnt directory if you are not

cd /mnt

Then, by confirming that we are the correct place make a directory called git_repos

root# pwd
root# mkdir git_repos
root# ls
cdrom  dvd    floppy     home                     msdos  swap
data   flash  git_repos  ram1   zip

Here we could mount our partition file. I have kept the partition file in home directory (where all the sfs, personal save file and all the other files of Puppy Linux are kept), so my mount command would look something like this:

root# mount home/git_repo.4fs git_repos

After changing to git_repos directory we would see something like this:

root# cd git_repos
root# ls

To unmount simply use umount :

cd /mnt
umount git_repos

And that's all , we have a partition file where you could keep your git repositories ! Move this partition file around without effecting the history of the git repositories.

Working with your git repositories

Now we have space to keep all the git repositories. However, as this is a partition file you have to treat it as any partition in your drive. Only the plus point is we could move it around anywhere like a directory.

Resizing the file partition

That said , what if we want to increase the size accomodate more git repositories later on ?? That could be done by using resize2fs. But before doing that run e2fsck over it as such:

root# e2fsck -f git_repo.4fs
e2fsck 1.42.9 (4-Feb-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
git_repo.4fs: 9749/51200 files (0.4% non-contiguous), 121125/204800 blocks

Then use resize2fs. Say if we want the partition file to have 512mb of space, so the command of resize2fs would look something like this:

root# resize2fs git_repo.4fs 512m
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on howling.4fs to 524288 (1k) blocks.
The filesystem on git_repo.4fs is now 524288 blocks long.

Maintaining the file partition

If you have abrupt power cuts, as your system shuts down or crashes suddenly while you were working with your file partition mounted, remember to e2fsck first to repair the file system before mounting it again.

root# e2fsck git_repo.4fs
e2fsck 1.42.9 (4-Feb-2014)
git_repo.4fs: clean, 11/8192 files, 5530/32768 blocks

If you find any mistakes in the article send me a pull request here.