Prerequisites #
Git installed
on your local machine and the production server.SSH access
to the production server.A Git repository
on your local machine.
Step #
- Initialize a Git Repository
First, make sure you have a Git repository set up on your local machine. If you don’t have one, you can initialize it using:
cd /path/to/your/project
git init
- Set Up the Remote Repository
You’ll need a bare repository on your production server to which you will push your code.
# On your production server
mkdir -p /var/repo/your_project.git
cd /var/repo/your_project.git
git init --bare
- Add the Remote Repository
On your local machine, add the production server repository as a remote:
git remote add production user@production_server:/var/repo/your_project.git
Replace user with your username on the production server and production_server with the server’s address.
- Push Code to the Production Server
Push your local repository to the production server:
git push production master
- Post-Receive Hook for Deployment
Create a post-receive hook on the production server to automatically deploy the code when pushed.
# On your production server
cd /var/repo/your_project.git/hooks
nano post-receive
Add the following script to post-receive:
#!/bin/bash
GIT_WORK_TREE=/var/www/your_project git checkout -f
Make the hook executable:
chmod +x post-receive
- Automate Sync with Git Hooks (Optional)
To automate the push to the production server every time you commit or push to your local repository, you can use Git hooks on your local machine.
Post-Commit Hook
This hook will push changes to the production server every time you commit.
# On your local machine
cd /path/to/your/project/.git/hooks
nano post-commit
Add the following script:
#!/bin/bash
git push production master
Make the hook executable:
chmod +x post-commit
Post-Push Hook
This hook will push changes to the production server every time you push to your remote repository.
# On your local machine
cd /path/to/your/project/.git/hooks
nano post-push
Add the following script:
#!/bin/bash
git push production master
Make the hook executable:
chmod +x post-push
- Test the Setup
Make a commit and push it to see if everything works as expected.
git add .
git commit -m "Test commit"
git push
Check your production server to ensure the changes are deployed.
Notes #
- Make sure your production server is secure and only accessible by authorized users.
- Consider setting up a more advanced deployment tool (like Capistrano, Ansible, or Docker) for larger projects or more complex deployment needs.
- Always have backups and a rollback strategy in place in case something goes wrong during deployment.
This setup ensures that your code is automatically uploaded and synchronized with your production server whenever you commit or push changes to your local repository.
Configure Git on Server #
Add user to handle the repositories:
# sudo adduser git
Log in as a git user
# sudo su — git
Initiate a new empty repository using following command:
# git init — bare ~/hostnextra.git
Enable post-update hook by copying the sample file as follows:
# cd hostnextra.git/hooks/
# cp post-update.sample post-update
That’s it for server side.
Configure Git on Client #
Submit inflammation about yourself so that commit messages will be generated with correct information attached:
# git config — global user.name “git”
# git config — global user.email “git@hub.hostnextra.com”
Create a directory where you can keep all your projects
# mkdir ~/dev
# cd ~/dev
Now, create a clone the hostnextra.git repository that we have created earlier in the server
# git clone git@hub.hostnextra.com:~/hostnextra.git hostnextra.git
Cloning into ‘hostnextra.git’…
It will ask to enter git user password:
git@hub.hostnextra.com’s password:
warning: You appear to have cloned an empty repository.
Go to respository
# cd hostnextra.git
You can see the repository is empty, so lets create some files
# echo “my test file” > file1.txt
Add these file to our git repository
# git add .
Commit the changes
# git commit -am “My First Commit”
[master (root-commit) b337197] My First Commit
1 file changed, 1 insertion(+)
create mode 100644 file1.txt
Push these changes to the remote git repository at hub.hostnextra.com
# git push origin master
you will be asked for password, enter git user password
git@hub.hostnextra.com’s password:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 229 bytes | 76.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To hub.hostnextra.com:~/hostnextra.git
* [new branch] master -> master
Verify the changes, access the git server and run following command to check the logs
# git log
Output will be similar like:
commit b3371975bd44fb4aca344e365fa635180967f7fe (HEAD -> master)
Author: git <git@hub.hostnextra.com>
Date: Wed Apr 14 10:06:06 2021 +0000
My First Commit
We have successfully install Git server on Ubuntu 22.04.