How to Create a Base Git Repository Using a Bash Script
Creating a "bare" Git repository is a common practice in development environments where multiple people collaborate on the same project, or when you need a centralized point to synchronize code without keeping a working directory. A "bare" repository doesn’t have a working directory associated with it; it only holds the version control data and history.
In this post, we will explore how to create a base repository using a simple Bash script.
The Script
Here is the script you can use to set up a "bare" Git repository:
#!/bin/bash
user="USER_HERE"
repository="api-site-app"
echo "[TASK $((++count))] Configuring git bare repository"
dir="/path/app/${repository}"
[ -d "${dir}" ] && echo "Message: Path ${dir} already exists."
[ -d "${dir}.git" ] && echo "Message: Path ${dir}.git already exists." && exit
mkdir -p ${dir}
mkdir -p ${dir}.git
pushd ${dir}.git > /dev/null 2>&1
git init --bare > /dev/null 2>&1
echo "git --work-tree=${dir} --git-dir=${dir}.git checkout -f" > ${dir}.git/hooks/post-receive
chmod +x ${dir}.git/hooks/post-receive
chown -R ${user}:${user} {${dir},${dir}.git}
popd > /dev/null 2>&1
echo "[TASK $((++count))] Done!"
Script Breakdown
- Defining the user and repository name
- The
user
variable should be set to the system user who will own the repository. - The
repository
variable defines the name of the repository to be created.
- The
- Checking if the directory already exists
- The script checks if the directory where the repository will be stored (
/path/app/${repository}
) exists. If so, it prints a message. - It also checks if the
.git
directory already exists, which would mean the repository was already initialized. If it does, the script exits.
- The script checks if the directory where the repository will be stored (
- Creating the necessary directories
- If the directories do not exist, the script creates the repository and
.git
directories, preparing them for Git initialization.
- If the directories do not exist, the script creates the repository and
- Initializing the "bare" Git repository
- Inside the
.git
directory, thegit init --bare
command is run to initialize a "bare" repository. This type of repository is central storage where code will be pushed, without a working tree (no code files).
- Inside the
- Configuring the
post-receive
hook- The script sets up a
post-receive
hook. A Git hook is a script that Git runs automatically during certain events. In this case, thepost-receive
hook is triggered after agit push
to the repository. - The hook is configured to check out the files into the working directory when a new push is received.
- The script sets up a
- Adjusting permissions and ownership
- The script changes permissions and assigns ownership of the files to the specified user, ensuring the repository and directories have the correct access rights.
Conclusion
This script is perfect for quickly setting up a "bare" Git repository, which can serve as a central repository for your applications. It creates the required structure, initializes Git, and configures the post-receive
hook to automate the deployment of code after a push. This setup is ideal when you want to maintain centralized code on a server without the need for a working directory.
To use this script:
- Edit the
user
variable with the appropriate system username. - Configure the path and repository name in the
repository
variable. - Run the script on your server.
Now you will have a properly configured "bare" Git repository ready for use.