Git Your Own: A Mini Git Server Project
Take control of your code's history.
Photo by Roman Synkevych on Unsplash
Welcome, fellow beginner! Ever wished you had a central location to store your code's history and collaborate with others? Look no further than Git! This project will guide you through setting up your own mini Git server, taking control of your versions and diving into the world of version control.
By the end, you'll have a basic Git server running, ready to accept your projects and become the central hub for your coding adventures. We'll break down the process into easy-to-follow steps, explaining the commands and concepts along the way.
So, grab your terminal, a cup of coffee (or your favorite beverage), and let's embark on this journey together!
Ever wondered how fancy developers keep track of all their code changes? It's all thanks to something called Git, a powerful tool that acts like a magic code history book. But what if you could ditch the whole "cloud" thing and have your own personal code vault? That's where setting up a Git server comes in! It might sound complicated, but don't worry – this post will be your guide to setup a mini Git server on the AWS instance , step-by-step. just a curious mind and a desire to take charge of your projects. Buckle up, let's explore the world of Git servers together!
I would like to thank Piyush Garg for explaining the concepts like above mentioned with ease. I will be tagging his LinkedIn, YouTube channel and GitHub for you to explore such amazing content.
What is a GIT Server?
Imagine having a special box for all your project files, like code for a game or a website. This box keeps track of every single change you make, like adding new features or fixing bugs. That's kind of what Git is, but instead of a box, it's a software program.
Now, a Git server is like renting a safe deposit box at a bank for your Git stuff. It lets you store your project's entire history (all those changes) on a computer that's always on and accessible from anywhere. This is handy because:
Multiple People, One Project: You and your friends can work on the same project together, even if you're in different places. The server keeps everything organized and avoids any accidental overwrites.
Safe and Sound: If your computer crashes, no worries! The server has a copy of everything.
History Detective: Ever wonder "what did this code look like last week?" With a Git server, you can rewind and see exactly what changed and when.
Credit : Piyush Garg
As you can see the demonstration of GIT Server and how it should be accessible for local machines for pushing the developmental features in the GIT server.
Setup of AWS Instance
Let us start the process of creating GIT server on AWS instances. Think of it as having your own personal computer in the cloud, but without the physical machine. You can customize this virtual server by choosing the amount of processing power, memory, and storage you need. This lets you run applications, websites, and other software projects without having to manage your own hardware. AWS offers a wide variety of instance types optimized for different tasks, so you can get exactly what you need for your project.
You can sign up on AWS for free-tier services also which is suitable for students who wants to pursue the cloud technologies and services.
Below is the snapshot for the AWS console dashboard which is powered by my free-tier account.
As we can see, currently there are no instances or no services are running on my free tier account. For successful creation of GIT server and deployment, It is required to create an instance and deploy it.
It is recommended that you choose the instance in the zone which is near by of your own zone / region . It is compulsion but it is recommended due to better availability of resources.
You may choose to click on Launch instance
You can also see the option of Instance menu in the navigation bar.
You can also use the search bar where you can use the search term as "EC2" which will lead to below snapshot.
As you can see in the snapshot above, it is setup screen for doing the setup for the EC2 Instance.
You need to name your EC2 instance uniquely, due to that, you can see that I have given the name mentioned in the snapshot "Git-Server-Initial-Project".
After naming your EC2 instance, You need to select the Amazon Machine Image which acts as host operating system for the EC2 instance. As We know, Most off Servers has common operating system as host which is Ubuntu.
We have selected the OS Image of Ubuntu for Host OS.
It is recommended that you select t2.medium or t2.large machine types which has the optimum machine configurations and storage for server instance which is beyond the applicable of free-tier services.
For current implementation, We have to consider t2.micro for free-tier AWS services.
Now as we are creating the server instance for GIT server setup, It is understandable that we need to be able to access the server for pushing the files for version controlling for software.
For above mentioned purpose, AWS provides you creation of key pair so that we can store the key on the server for SSH and authorization for accessing the instance.
We will create the file named "git-server-kp" which is based on RSA algorithm**.** We will select .pem extension for SSH purpose.
After filling the key-pair name and type with the key file format, we will create the key pair by clicking on create button.
You can see the file is being downloaded on your downloads section. we need to save the file in the location where we can retrieve it easily for further process for RSA.
After that we should click on Launch instance for deployment of our EC2 Instance with the configurations.
It will take around 2-3 minutes for our instance to be up and running with deployment. which will be shown as below screenshot.
We are going the terminate the instance as soon as the implementation gets completed. the reason of the action is that we do not want to be charged more of credits.
After successful launch of our EC2 instance, We need to see how we can connect to the instance for further setup for the GIT server setup.
Method 1: EC2 Instance Connect
We can use EC2 instance connect setup option for connecting EC2 instance where we can use the public IP address for connection using the username mentioned in the above snapshots.
Method 2: SSH Client
We can also use the SSH client for accessing the instance and in our current setup, we will prefer to access using SSH client because it becomes easy to access and work on server using SSH. The Secure Shell (SSH) protocol is a method for securely sending commands to a computer over an unsecured network.
Developer SSH Setup
Now as we have our EC2 instance deployed on the AWS, It's time to setup the SSH from developer side for making our GIT server fully functional for creating repositories to pushing and pulling your codes to the pipelines and production servers.
As you know, we are using the SSH connect for accessing our deployed EC2 Instance, It is required that we use our security private key pair which we created at the time of launching our instance.
If you have Unix based OS then you need to run the below command on your terminal for changing your key file's permissions to non publicly viewable mode.
chmod 400 "[YOUR-FILE-NAME]"
If you are having the Windows OS, then you need to follow the procedure as per the below screenshot:
It is an alternative to the command mentioned above. You can find the Youtube link from where I got this alternative solution.
After completing the process, now we are set to do the task of accessing the instance and setup of further steps.
You can find the necessary SSH command in the snapshot as per below also you can also find the command on your own particular launched EC2 instance.
After copying the command, now we are set to explore the unchartered territories. Let's begin the real work!!!!!!
GIT Server Setup
Let us take it one step ahead this time, Coming back to terminal to your personal computer and we will paste the SSH command which we had copied from SSH Client connection method in AWS Instance.
As you can see in the snapshot, as soon as we hit the Enter button after pasting the command which contains the public IP address for your deployed AWS EC2 instance, we have successfully got in the inner world of the instance.
After doing SSH into the instance, currently you are logged in as root user which is Ubuntu in our case.
Normally, we should have the practice for upgrading the packages for our instances for which we will use the below command.
sudo apt-get upgrade
The basic yet most simplest definition of a GIT server could be,
It is a server, on which the GIT is installed.
First of all, we will check whether GIT is already installed in our instance or not!! Normally, in Unix based Operating systems, GIT is always pre-installed yet we will check and install the GIT.
For checking the Version of GIT
git --version
For installation of GIT on your machine
sudo apt install git
After running the installation command, we can see the snapshot
We want to create a user named GIT. For that we need to use the below command in our instance terminal. We can name the user anything but for the simplicity purpose we will use the name GIT.
sudo adduser [YOUR_USERNAME]
As soon as we hit the command, Prompt will ask for password. You may give the password as per your accordance. It will also ask for your basic details, you may choose to give information or else you may press the ENTER button. At last, you have to answer regarding the verification of information you entered.
For switching the user we will use below command
sudo su [YOUR_USERNAME]
As soon as we switch the user from root ( Ubuntu ) to current user (GIT) . We will be having the empty user directory for current user.
We can go backwards in the path by using the cd (change directory) command.
Now, as we know that in the $home directory -> $ubuntu directory and $git directory.
GIT Repository Setup
Let us understand the concept of repository with a ideation.
In the world of Git, a repository, often shortened to "repo," is like a special folder that holds the magic. Here's how to think about it:
Imagine your project as a plant growing over time. Every new leaf, flower, or branch is a change you make.
A repo is like a time capsule for this plant. It stores every single version of your project, from the tiny sprout to the full-grown wonder.
This way, you can see how your project evolved, revert to older versions if needed, and even collaborate with others who can access the same repo and contribute their own "plant growth."
There are two main types of repos:
Local Repo: This is like having your own personal plant pot on your computer. It holds all the project history, but only you can access it.
Remote Repo: This is like storing your plant in a community garden (like GitHub). Now others can see your project, contribute their own changes, and everyone can work together on the same plant!
Now suppose we want to create the GIT Repository in the path mentioned in the below snapshot.
for the path we need to create the directory in the location we want to create our GIT repository.
mkdir [YOUR-DIRECTORY-NAME]
After creating the directory , we need to be in the newly created directory using the change directory command in the terminal.
We need to initialize the GIT repository in the location where we just created the directory
Here as per the snapshot, I have created the repository named firstApp.git which is to distinguish that the given repository is GIT repository.
After changing the present working directory to newly made GIT repository, now we will initialize the GIT using the below command
git init --bare
You may have the question that why we are using --bare flag?
Imagine you and your friends are working on a project together. You all need a central place to store different versions of your work. That's what a Git repository is like.
Normally, with git init
, you create a full Git repository on your computer. This includes a workspace where you can edit files and a separate area to store the history of changes (like a version control system).
Now, let's say you want to create a central storage for everyone's work on a server (like a Dropbox folder for code). This is where git init --bare
comes in.
The --bare
flag tells Git to create a repository that only stores the history of changes, not a workspace to edit files. It's like a central archive for your project versions.
Here's the key difference:
Regular
git init
: Creates a full repository for you to work in and track changes.git init --bare
: Creates a central storage on a server to store only the project's version history, for pushing and pulling changes from other computers.
Think of it like this:
Regular
git init
is your personal notebook where you write and track edits.git init --bare
is a shared library where everyone stores different versions of the same book.
For more documentation , you can go the link from Atlassian documentation and read more about this.
As soon as the GIT is initialized, we can see the various folders being created in the repository, which we will talk about later!
Pushing and Pulling the files from repository
We have initialized the repository on our EC2 instance in previous steps. Now for utilizing the functionality for GIT is that we should be able to pull the changes in the codebase from the change log and pushing the changes you are making in your current local repository to the server where we are storing the codebase.
Take a note, in the GIT Server, we are not storing the code files, it is for the purpose of tracking the changes in your repositories in terms of commits.
Now it is necessary to pull the repository to contribute as per your requirement. Let us understand the anatomy for cloning the repository for setup of repository for the first time in the local development environment.
git clone [GIT_USERNAME]@[PUBLIC_IP_GIT_SERVER]:[PATH_TO_THE_REPOSITORY]
In our case, it would be
git clone git@13.126.249.103:var/lib/git/firstApp.git
This command is asking the GIT to
clone the firstApp.git repository
which is residing in the filepath : var/lib/git
which is using the GIT user named git
from the server address of 13.126.249.103
You might think where is the public ip address is available for the instance, it is found in the EC2 instance dashboard
Now take a note, we can map a domain too for the public IP part of the command. Let us take a example of popular platform like GitHub / GitLab / Bitbucket.
git@github.com:darshanjoshi16/forage-jpmc-swe-task-3.git
For cloning the repository in the local development environment, we need to open a different terminal window. Do not close the terminal window in which the instance is currently in the running mode.
We will paste the command which we have mentioned earlier for cloning the repository from server in the newly opened terminal window.
Oops !!!! we can see that we do not have the permission to access the repository as we could not do the SSH to the instance.
For solving the above mentioned problem, we need to store the public SSH key generated from our system into the server instance.
- Create a file called "authorized_keys" inside the ".ssh" folder.
You need to go back to terminal window where our EC2 instance is currently running, after that we need to go to the folder of our user named git using the change directories command.
After completing the above mentioned step, we need to create the directory called .ssh and change the present working directory to .ssh and create the file for authorization public keys
mkdir .ssh
cd .ssh
vim authorized_keys
- Add the SSH key to the "authorized_keys" file using the Vim editor.
You may already have the SSH key generated for your local system, if not you may go to link by clicking on the snapshot below to understanding generate the SSH key for your local operating system.
After generating the SSH key using the procedure mentioned in the documentation,
Open your new terminal window
paste the below command
cat ~/.ssh/id_ed25519.pub
copy the output of the command.
go to the terminal where the server instance is running.
open the authorized_keys file using vim command.
go to insert mode using pressing the i key on the keyboard.
paste the copied content.
press the escape key and write :wq and press enter to save the key into the file.
After completing the process, Once I again I tried to pull the repository using the clone command.
Voila!!! The repository we have created in the server instance is now copied into our local development environment!!!
Now It is time to create a file for performing the actions regarding the version control.
I am using Visual Studio Code as the code editor for editing the files. You may have installed other code editors like Sublime-Text or Notepad++.
As you can see in the snapshot we have created the sample file name index.html for demo purpose. After creating the file you need to perform the below commands in the terminal for pushing the file to the repository.
git add .
git commit -m "[YOUR-COMMIT-MESSAGE]"
After committing the file, it is required to push the committed changes to the repositories.
git push --set-upstream origin ["LOCAL_BRANCH"]
This command pushes your local branch to the remote origin
and sets the origin/master
branch as its upstream. Now, whenever you run git pull
, Git will automatically know which remote branch to fetch changes from.
Now your files are set to be pushed and pulled from the remote branch in the repository.
Git Web UI
There are two main approaches to using a web interface with Git:
- Self-Hosted Web UIs:
These are tools you install on your server to provide a web interface for a specific Git repository. Here's an example:
- git-webui: This is a self-hosted web UI written in Python. It allows you to browse the repository history, view file changes, and commit changes directly from a web browser https://github.com/alberthier/git-webui.
- Web UIs offered by Git Hosting Platforms:
Many Git hosting platforms like GitHub, GitLab, or Bitbucket provide their own web interfaces for interacting with Git repositories. These web UIs offer features like browsing code, creating pull requests, and collaborating with others.
Here are some things to consider when choosing a Git web UI:
Self-hosted: Offers more control and privacy but requires setup and maintenance.
Platform-based: Easier to use but may have limitations depending on the platform.
Additional Options:
- Desktop Clients with GUIs: There are desktop Git clients with graphical user interfaces (GUIs) that provide a more user-friendly experience compared to the command line. These can be a good alternative to web UIs, especially for frequent Git users.
We will use the self hosted UI for setting up our GIT operation.
You need to go back to the terminal where our instance is still in running status, first of all we need to check some dependencies for Web-UI. We will follow the documentation for which I have linked it on the given snapshot.
If you will follow the documentation, it will come to notice that, this particular service is running on top of Ruby and lightweight web-servers like lighttpd
or webrick
.
It is required to install dependencies based on Ruby and web-servers.
sudo apt install ruby-full
sudo apt install webrick
If you’re running macOS, Leopard comes preinstalled with Ruby, so webrick
may be your best bet.
After installation of Ruby and the web-servers gets completed, now it's required that first of all we set the permissions for the instance to be accessed from outside network for Web-UI. for that purpose, we need to set the inbound rules to accept the traffic in our instance.
Let us go back to the AWS Instance console. In EC2, we will find the option named the security groups, which is the default security group for permissions created while we launched our instance.
Now we need to click on "Edit Inbound Rules" button.
As we can see, the provision of SSH access to instance was already there for us by default. Now, we need to enable the provision for web access, for that we will select "Custom TCP" in the type dropdown options.
We need to also define the Port by which we are allowing the traffic to access the instance for that we need to run the GitWeb UI command with defining the web-server.
git instaweb --httpd=webrick
After executing the command, we will get the address and port where our web server is now activated. We will note the port number and will paste it down in the security inbound rules in console where we need to define the port number.
We need to also select the source, in which for one rule we will select IPV4 type addresses and we also add the inbound rule with same process for the source IPV6 addresses.
After following the process, we will go to the new browser tab, we will take the public IP address from AWS EC2 console and will append the port where our web-servers are exposed for serving.
https:[PUBLIC_IP]:[PORT_NUMBER]
And we will be redirected to the GIT Web UI !!!!!.
As we can see in the snapshot, We can see the project, we can see the description, as well as we can see the last change time with options like summary | shortlog | log | tree.
We can also see the commit logs by exploring the options, before that let us make changes in file and pushing it to server to check the status in the real-time.
We added the new heading tag with content of welcome and committed the file and pushed the file to remote repository. before pushing it is suggested to perform the pull repository command. Because if we do not the pull the repository before making the changes to our local code, it may create the conflict while pushing in the collaborative development environment because it may affect the changes being the done by the other developers who are working on the same project.
git pull
Let us check the commit log after the pushing the second commit in the repository.
We can also perform as many commits we want in the local repositories and push them to reflect to the Web-UI.
Now you must know, we can also map the domain to public IP as I mentioned before, also you can modify the functionalities in the user interface as per your requirement.
Now we can terminate the EC2 instance by going to the console dashboard and select the running instance from the list and select the terminate option in the menu.
GIT Server Repository Structure
HEAD: This is a special reference in Git that points to the latest commit in the current branch. Since this is a newly created repository, the HEAD will be detached (not pointing to any specific branch).
branches: This directory typically stores information about the branches in a Git repository. Since this is a new repository, there are currently no branches.
config: This file stores configuration settings for the Git repository.
description: This file can be used to store a description of the project.
hooks: This directory can contain custom scripts that are triggered by specific Git events, such as before a commit or after a push.
info: This directory can contain additional informational files about the Git repository.
objects: This directory stores the actual Git objects, which are the building blocks of Git repositories. These objects can be blobs (which store file content), trees (which represent directory structures), and commits (which reference trees and contain metadata about changes).
refs: This directory stores references to Git objects, such as branches and tags. The HEAD reference is typically stored here.
Congratulations! We've successfully set up a basic Git server. This is a fantastic first step into version control and server administration. While there's always more to learn about security, user management, and advanced configurations, we now have a solid foundation to explore further. Remember, practice makes perfect. Experiment with pushing and pulling code to server, and feel free to explore online resources for deeper dives into specific topics.
Thank you very much for reading the blog till end, as it is my first blog of exploration and journey to learning new things. Stay tuned for such more blogs!!!
The Accidental Techie signs off!!!.