The first time I heard about Git, I thought it was just some tech word only programmers use. But then, I started working on websites and writing small programs. I quickly learned that Git wasn’t just helpful—it was something I needed. If you’re working on a project and want to keep track of your changes, share your code, or work with others, Git makes life much easier.
So what exactly is Git?
Git is a tool that helps you keep track of changes in files. It’s often used for coding projects, but it works for any type of file. You can save versions, go back to earlier ones, or work with teammates without messing things up. Think of Git as a time machine and a team player, rolled into one.
In this guide, I’ll walk you through setting up Git on a Linux server. I’ll explain what everything means and show you each step in a simple way.
What You’ll Need
Before we begin, make sure you have:
- A Linux server (Ubuntu or Debian works great)
- Access to the server through SSH
- A regular user account (not root)
- A little patience
If you’re not sure what SSH is—no worries. SSH (short for Secure Shell) lets you log in to another computer safely, like having a long-distance keyboard for your server.
Why Use Git on a Server?
You might be wondering—can’t I just use Git on my laptop?
Yes, you can. But having Git on a server has some big benefits:
- You can share code with others easily.
- Your work is backed up in a different place.
- You can deploy (put your code live) to your server directly.
Imagine working on a school project with a friend. You can both push your updates to the server, and it keeps track of who did what and when.
Step-by-Step Guide to Set Up Git
Let’s break it down into easy steps.
1. Install Git
First, log in to your Linux server using SSH:
ssh yourusername@your-server-ip
Once logged in, run this to update the system:
sudo apt update && sudo apt upgrade -y
Now install Git:
sudo apt install git -y
Check the version to make sure it installed correctly:
git --version
You should see something like:
git version 2.34.1
2. Set Up Your Identity
Git needs to know who you are. This helps when you save changes, so it can label them with your name and email.
Type this (use your real info):
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
You can check if it worked:
git config --list
You should see your name and email there.
3. Create a Git User (Optional but Recommended)
If you’re setting up Git for a team or for hosting repos (short for repositories), it’s safer to make a special user just for Git.
sudo adduser git
Set a password when asked.
This user will manage your Git projects.
4. Create an SSH Key
Now let’s make a secure key from your computer (or another server) so you can connect without typing a password every time.
On your local computer:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Press Enter to use the default path.
Then, copy the key:
cat ~/.ssh/id_rsa.pub
Now go back to the server and switch to the git
user:
sudo su - git
Make a .ssh
folder:
mkdir ~/.ssh && chmod 700 ~/.ssh
Create the authorized_keys
file and paste your public key inside:
nano ~/.ssh/authorized_keys
Paste the copied key here. Save and exit.
Set the right permissions:
chmod 600 ~/.ssh/authorized_keys
Now you can connect as the git
user using SSH without a password.
5. Create a Repository
Let’s make a new Git project (repo for short) on the server.
Still logged in as the git
user, create a folder:
mkdir myproject.git
cd myproject.git
Turn it into a Git repository:
git init --bare
What does “bare” mean?
A bare repo doesn’t have files like a normal project. It’s made just for sharing. People send and receive code from it, but don’t edit it directly.
6. Clone the Repository
Now, from your local machine, you can “clone” (copy) the repo:
git clone git@your-server-ip:/home/git/myproject.git
This will create a folder on your computer. You can now work inside it, make changes, and push (upload) your code back to the server.
A Quick Look at Git Commands
Here are some common Git commands I use every day:
git add .
— tells Git to track all your changesgit commit -m "message"
— saves your changes with a short notegit push
— sends your work to the servergit pull
— gets the latest version from the server
Want to try it?
- Add a new file:
echo "Hello World" > hello.txt
- Run:
git add . git commit -m "Add hello.txt" git push
Boom—you’ve just saved your work on the server!
Benefits of Using Git on a Server
Let’s go over why this setup is useful.
Here’s what you get:
- Backups: If your laptop dies, your work is safe on the server.
- Teamwork: Multiple people can work together without overwriting each other’s files.
- History: Git remembers everything. You can go back to any point in time.
Tips for Beginners
When I first started, I got stuck a lot. But here are a few things that helped me:
- Always write clear commit messages (like “fix bug in login page”).
- Commit often. Don’t wait until you’ve made tons of changes.
- Keep your repos organized in folders so you don’t get confused later.
Three Lists to Help You Out
Tools You’ll Use
git
— the main toolssh
— for connecting to the servernano
orvim
— to edit files on the server
Problems You Might Face
- Permission errors — fix with
chown
orchmod
- SSH denied — double-check your SSH keys
- Wrong repo path — make sure you’re using the full path
Good Habits to Build
- Use branches for new features
- Test before pushing to the main repo
- Pull often to avoid conflicts
Comparing: Git on Server vs GitHub
You might have heard of GitHub. It’s an online Git server. So why use your own?
GitHub:
- Easy to use
- Works with teams
- Public repos are free
Your own server:
- More control
- Private by default
- No monthly fee
If you’re just starting out, GitHub is fine. But if you want more privacy or control, your own Git server is the way to go.
Final Thoughts
Setting up Git on a Linux server might sound hard at first. But once you do it, it feels like unlocking a superpower. You can save your work, share it with others, and never worry about losing anything.
I’ve used this setup for personal websites, small games, and school projects. It’s simple, safe, and just works.
What kind of project are you thinking of using Git for? A game? A blog? Something for school?
Whatever it is, you’re on the right path.
Let me know if you’d like help with Git hooks, working with branches, or making automatic deployments next.