Saturday, June 11, 2016

Initializing a bare git repository for a remote server

# Creating a bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ mkdir myproject
$ cd myproject/
$ mkdir gitdir.git
$ mkdir workingtree
$ cd gitdir.git/
$ git init --bare
Initialized empty Git repository in /absolute/path/to/myproject/gitdir.git/
$ touch hooks/post-receive
$ echo "git --git-dir=/absolute/path/to/myproject/gitdir.git --work-tree=/absolute/path/to/myproject/workingtree checkout -f" > hooks/post-receive 



# Pushing to a bare repository
# -------------------------------
# inside local machine
# -------------------------------
$ cd /path/to/myproject
$ git remote add remoteserver [email protected]:/absolute/path/to/myproject/gitdir.git
$ git push remoteserver master



# Looking at git branches and working tree files in bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git branch
$ cd ../workingtree
$ ls



# Pushing multiple branches to a bare repository
# -------------------------------
# inside local machine
# -------------------------------
$ cd /path/to/myproject
$ git push remoteserver staging
$ git push remoteserver nightly
# etc
# -------------------------------
# inside local machine
# -------------------------------
cd /path/to/myproject/gitdir.git
git branch



# Checking out a different branch in bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git checkout staging
fatal: This operation must be run in a work tree
$ cd ../workingtree
$ git checkout staging
fatal: Not a git repository (or any of the parent directories): .git



# So what do we do here?
# We shall edit the bare git repository's configuration file slightly
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ nano config 
# add a new entry 'worktree' inside core group
# set its value to 'absolute/path/to/myproject/workingtree' inside double-quotes
# like this:
    [core]
        repositoryformatversion = 0
        filemode = true
        bare = true
        worktree = "/absolute/path/to/myproject/workingtree"
# refer to this stackoverflow discussion here 



# Unfortunately, even setting the worktree explicitly didn't solve the problem.
# This time, I got a new fatal error
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git checkout staging
fatal: core.bare and core.worktree do not make sense
# Hence, I edited the git repository's configuration file once again 
# There I changed the value of core.bare to False



# Checking out a different branch in bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git checkout staging
Switched to branch 'alpha'
# I am not convinced this is a completely correct solution. 
# Maybe this is a hack approach. 
# But it worked for my case.



# Comments / criticisms / suggestions / improvements welcome below