Today, we will see how we can setup a remote git repository server for a code base. So the purpose is, we will use a server/machine/host driven by openSUSE 42.1 Linux distribution where we will setup and configure our remote git repository server. And then we will use a separate machine/computer/host and from there we will use git client to clone, commit, push & pull to that remote git repository server. Let’s start then.
What is Git?
If you are a software developer then you must be familiar with Git. Git is vastly used for version/revision control for software development for controlling source code. It is a distributed revision control system. Git is better than SVN for speed, data integrity and also support non-linear workflows. Git was initially designed and developed by Linus Torvalds for Linux kernel development purpose. Yes you read it correct. The creator of the Linux kernel designed and developed Git. Git was initially used for updating source code of the Linux kernel from around the world. Like any other version control systems, every Git working directory has a full-fledged repository with complete history and full version-tracking capabilities. Git is free software distributed under the terms of the GNU General Public License. Git utility or git tool is available with almost every Linux distributions.
How it works?
Git deals with data more like a set of snapshots, a snapshots of a miniature filesystem. Every time you commit your file, or save the state of your project in the git project repository, it basically takes a snapshots. So, it stores a reference to that snapshot of what all of your files look like at that moment of commit. To be efficient, fast & accurate if files have not changed, Git does not store that file again for further more changes with your commit. It just simply stored a link to the previous identical file it has already stored.
Why OpenSUSE 42.1 Linux?
Question is why we are selecting openSUSE over here? Answer is simple. Now a days, it is one of the very famous Linux distribution and widely used by System Administrators and too good for software development purpose and specially for Production / LIVE environment to deploy your web applications. Beyond the distribution, the openSUSE project provides a web portal for community involvement and it is very much strong & active with a good number of user base. It is built for software developers and system administrators, also has a user-friendly desktop, and feature-rich server environment. It has both KDE & GNOME Desktop environment. Download openSUSE 42.1 from here.
So, I am hoping that you had already installed openSUSE on your server family hardware and openSUSE 42.1 is up and running.
First, you must have a static IP address assigned on your host/server machine. This is mandatory and you can not use dynamic DHCP sever to obtain an IP address for your server. I hope you know how to assign an static IP address for your server/host. I am setting 192.168.1.11 for my server IP address manually.
Also, you need to set a host name for your server. Let’s set a host name for your server. To set a host name “git.repository.com” open a terminal and type the following. Here I am using vim tool to edit the file. You can use your favourite editor to edit the file /etc/hosts.
sudo vim /etc/hosts
And type the following to set your hostname as “git.repository.com” with an alias name “git”.
192.168.1.11 git.repository.com git
Note: As I set my server IP address 192.168.1.11 earlier for my server, so I am using that IP address. You have to replace 192.168.1.11 by your own IP address.
Now, edit the file /etc/hostname and type “git.repository.com“. Save & exit from the file /etc/hostname. We are done setting our hostname as git.repository.com.
If you ping the host git.repository.com or host git, you must get an ICMP echo reply. A reboot may require to set the host name permanently.
If you don’t have Apache web server installed on your server, please do install it by doing
sudo zypper install apache2 apache2-utils
a2enmod cgi alias env rewrite
Start the Apache service and let it to start automatically on every reboot for your server.
sudo systemctl start apache2 sudo systemctl enable apache2
Let’s install Git on your openSUSE 42.1 Linux server.
zypper install git-core git
For more different Linux distributions, there are instructions for installing on this link.
If you don’t encounter any problems with the above command then we are done installing git on our server. To check which version of git is installed on your server, type the following
Creating a Git repository
Now, move to this directory /usr/share/gitweb by issuing the following command.
It’s time to create our first git repository. Say we have a project called “Demo Project”. Let’s create a folder named “demoproject” and initial the git over there. “demoproject” folder will be our working directory for our project “Demo Project”.
Now, issue the below commands.
sudo mkdir demoproject
sudo chmod -R 755 demoproject
sudo git init demoproject.git
Once you typed the last command in your terminal which is “sudo git init demoproject.git“, you will see a message in your terminal saying
“Initialized empty Git repository in Initialized empty Git repository in /usr/share/gitweb/demoproject/demoproject.git/.git/”.
Note: Make sure /usr/share/gitweb/demoproject directory is accessible over http protocol for Apache. Configure your Apache web server accordingly so that you can access /usr/share/gitweb/demoproject from another computer by using a web browser by using http protocol. This is must.
Create your identity
First, you need to set your user name and email address with git. This is very important as every Git commits you made uses this information.
sudo git config --global user.name "Mohammad Forhad Iftekher"
sudo git config --global user.email email@example.com
Adding Files to Git Repo
Now, we need to put a demo file and need to make the first commit in order to get started. To do so, create a demo file.
sudo vim demo.txt
Put some demo text in the demo.txt file. Save & exit from the demo.txt file.
Now, add this demo.txt file to the git repository.
sudo git add demo.txt
Now, commit this file.
sudo git commit -m "Adding demo.txt file"
After issuing the above command, you will see something like this.
[master (root-commit) e6d0fb3] Adding demo.txt file 1 file changed, 1 insertion(+) create mode 100644 demo.txt
So, your demo.txt file is now added and committed to the git repository.
Checking Git status
You can check the status of files in the index versus the working directory for your git repository by using the following command
sudo git status
Deleting a file from Git
Say, you need to remove a file named “documentation.txt” from your git. Just do the following one after another.
sudo git rm documentation.txt
sudo git commit -m "documentation.txt file removed"
sudo git push
You are done removing documentation.txt from your git repository.
Viewing log file
If you want to see a listing of commits on a master branch including details with logs, type the following
sudo git log
We are done with our remote git repository server. Now, it’s time to use another machine to fetch files from remote git repository server that we have just created.
Cloning the Git repository
Now, move into another computer and open a terminal and make a folder which will be used for your working directory. The following command will fetch the full repository into your current working directory.
You must clone the git repository for your project to start with and only then you can commit your changes.
sudo git clone http://192.168.1.11/git/demoproject/demoproject.git/
I hope you noticed that I used my remote git server IP address which is 192.168.1.11. You must use your own.
Committing your changes
Open the previously added demo.txt file. Now, add new contents on it.
And now let’s commit these new changes to the remote git repository.
sudo git commit -m "Changing demo.txt file"
Now, push the changes to the repository.
sudo git push
This will push the latest copy of demo.txt to the remote git repository server which is running on 192.168.1.11 IP address.
If you want to reset your index and working directory to the state of your last commit or to any commit then issue the following command in the terminal
sudo git reset --hard commit number/HEAD
Say you made changes in one of your file but you don’t want to commit it to the repo then you can stash it and also you can push it later as well.
sudo git stash
If you want to sync your local git codebase with the latest codebase of the git remote server repository, you need to use the following command. It will fetches files from the remote repository and merges it with your local one.
sudo git pull
The process that I showed you so far is by using command line terminal. But there are few GUI based tools like git-gui for doing the whole thing. I strongly recommended to use SourceTree GUI tool. Here is the link for SourceTree.
I tried to cover all the very basics of git’s server & client side in this article. I don’t want to make it complex or dive deep onto this. I hope that you all will like it. Thank you.
You Can use this link to know more about Git command