In this blog, we will setup git repository in Ubuntu VPS. We are going to follow these steps:
- Create a git user in Ubuntu server
- Setup authentication for a developer
- Append developer SSH public key in .ssh/authorized_keys file
- Create a empty git repository
- Clone a empty git repository in local system
- Add sample file
- Push latest change to the git repository
- View git log
ssh YOUR-SERVRE-USER@YOUR-SERVER-IP-ADDRESS
ssh root@140.82.46.248
It will ask you to enter password, type password and press enter key.
We have to install git. Before installation we need to check git is already installed or not.
git --version
We can see, git is already installed in Ubuntu 20.04 server. So no need to install.
The command "sudo adduser git" will create a new git user and a new git group. It will ask you to type password for git user, type password and press enter. Again retype password then press ente. For other values press enter, ifyou don't want to change.
sudo adduser git
We have successfully created the git user and group.
cd /home/git/
pwd
su git
mkdir .ssh && chmod 700 .ssh
ll
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
cd .ssh
ll
I will add my SSH public keys to the authozized_keys file.
cd .ssh
dir
cat id_rsa.pub
Make sure you are in /home/git/.ssh folder.
vi authorized_keys
It will open vi editor, press "i" to insert then paste the SSH public key.
Save and exit vi editor.
In this example, i am creating git repository inside /home/git/git_repositories/ folder. You can create git repository directly in /home/git/ directory also, but i would not recommend to do that. So creating git_repositories directory.
cd /home/git
mkdir git_repositories
cd git_repositories
mkdir project1.git
cd project1.git
git init --bare
We have created the git repository in Ubuntu VPS. Now we have to clone in my local system.
I want to clone repository in G:/projects/git-repos in this directory. You can clone git repository according to your project directory.
cd G:/projects/git-repos
You have to use git clone command to clone the git repository. You have to use your server IP and path of git repository.
git clone git@YOUR-SERVRE-IP:/PATH-OF-GIT-REPOSITORY/
git clone git@140.82.46.248:/home/git/git_repositories/project1.git/
We have successfully cloned the git repository in my local system. We can see in window explorer also.
Let us add one sample file. I have added test.txt file...
Open command prompt in you local system. Go to project folder and run following commands.
cd G:\projects\git-repos\project1
git add --all
git commit -m "Added test.txt file by nutan"
git push origin master
We can see without any error, we have pushed latest chanes to the git repository. Now we will check git log or status in server and local system.
git log
Connect to the server, go to the git repository directory and view git log.
cd /home/git/git_repositories/project1.git
git log
It is showing latest commit log.