How to set up a Git repository in minutes on Linux
If you need a quick code repository, you have everything you need with git and SSH. Jack Wallen shows you how it’s done.
Sometimes you just need to deploy a quick Git repository so you can collaborate on a project, back up your code or house your files on a remote machine. When you feel like doing that, you don’t want to have to spend an hour or so deploying a server and setting it up … especially when you already have everything you need at your fingertips.
I’m going to show you how you can quickly deploy a Git repository using just git and ssh. You should be able to spin this up in less than five minutes (two, if you type fast).
Let’s get busy.
What you’ll need
The only thing you need is a Linux machine (either a desktop or a server) to host the repository and a user with sudo privileges. That’s it.
SEE: Hiring Kit: JavaScript Developer (TechRepublic Premium)
How to install git
You probably already have git installed on your local machine. On the off chance you don’t have it installed on the remote machine, log into it and install with:
- For Debian-based systems – sudo apt install git -y
- For Red Hat-based systems – sudo dnf install git -y
Git should now be installed.
Stay logged into the remote machine.
How to create a git user and copy your SSH keys
On the remote machine create a git user with:
sudo adduser git
Give the new user a password and answer the remaining questions.
Change to the git user with:
su git
Change into the git users’ HOME directory with:
cd
Create the .ssh directory and give it the proper permissions:
mkdir .ssh chmod 700 .ssh
Create the authorized_keys file and give it the correct permissions with:
touch .ssh/authorized_keys chmod 600 .ssh/authorized_keys
Now, on your local machine view the id_rsa.pub file of the user who will be working on the git repository with the command:
cat /home/USER/.ssh/id_rsa.pub
Where USER is the user who’ll be working with git. Copy that key to your clipboard.
Open the authorized_keys file on the remote machine with:
nano /home/git/.ssh/authorized_keys
Paste the key and then save and close the file.
How to create the repository
Back on your remote machine create a repository directory (still as the user git) in the git users’ home with:
mkdir /home/git/git_repo
Change into that directory with:
cd /home/git/git_repo
Create a new project directory (we’ll name it PROJECTX) with:
mkdir PROJECTX
Change into that new directory with:
cd PROJECTX
Initialize the new (bare) git repository:
git --init bare
How to clone the new repository
Back at your local machine, issue the command:
git clone git@REMOTE:/home/git/git_repo/PROJECTX
Where REMOTE is the IP address of the remote machine housing the git project.
You should be prompted for your SSH key authentication password. Upon successful authentication, the project will clone and you’re ready to go. If you want to test this repository, create a README in PROJECTX (on the local machine) with:
cd PROJECTX touch README
Now, we’ll push the changes to the repository by adding the files, creating a commit, and pushing with:
git add --all git commit -m "Added README file" git push origin master
There ya go. You’ve created a repository on a remote machine, initialized a new project, cloned the project to a local machine, made changes, and pushed your changes to the repository. This should take you less than five minutes.
Done.
Also see
For all the latest Technology News Click Here
For the latest news and updates, follow us on Google News.