Git on Linux server refers to installing and configuring Git to track code, collaborate, and automate deployments on a Linux-hosted environment.
You’ll use native package managers, SSH, and repositories (bare or hosted) to version files, manage branches, and integrate CI/CD. This guide explains setup, security, workflows, and troubleshooting with practical, production-tested steps.
If you want to understand Git on Linux server as a beginner, you’re in the right place. As a distributed version control system, Git helps teams ship faster and safer.
On Linux, it shines thanks to native tooling, SSH, and flexible server options—everything from a simple bare repository to full platforms like Gitea or GitLab.
Below, I’ll walk you through what Git is, how to install and configure it on popular distros, how to set up a secure server, and how to avoid common pitfalls.
What is Git and Why Use it on a Linux Server?
Git is a distributed version control system that records changes to files over time. On a Linux server, Git becomes the backbone for collaboration, code review, CI/CD, and zero-downtime deployments.
Because Git replicates history to every clone, it’s resilient, fast, and ideal for both small projects and large-scale teams.
Install and Configure Git on Linux
The quickest way to install Git on a Linux server is via your distribution’s package manager. Update your packages first, then install Git.
Install Git (Ubuntu/Debian)
sudo apt update
sudo apt install -y git
git --version
Install Git (RHEL/CentOS/AlmaLinux/Rocky)
sudo dnf install -y git
# or on older systems:
# sudo yum install -y git
git --version
Install Git (Fedora, openSUSE, Arch)
# Fedora
sudo dnf install -y git
# openSUSE
sudo zypper refresh
sudo zypper install -y git
# Arch
sudo pacman -Syu --noconfirm git
git --version
Initial Configuration
Set your identity and enable a few sensible defaults. These settings are global for your user:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase false
git config --global core.editor "nano" # or vim
git config --global color.ui auto
# Optional line-ending safety (Linux servers usually don't need this)
git config --global core.autocrlf input
View your configuration at any time:-
git config --list --show-origin
Git Basics: Commands You’ll Use Daily
- Initialize or clone:
git init,git clone <url> - Stage and commit:
git add .,git commit -m "message" - Branching:
git branch feature-x,git switch feature-x(orgit checkout) - Sync:
git remote add origin <url>,git fetch,git pull,git push - Merge and rebase:
git merge feature-x,git rebase main(advanced; align with team policy) - Inspect:
git status,git log --oneline --graph --decorate,git diff
Server-Side Options: How to Host Git on a Linux Server
You have three common ways to run Git on a Linux server. Choose based on team size, security, and UX requirements.
1. Bare Repository over SSH (lightweight, fast)
Create a dedicated “git” user with no interactive shell, provision SSH keys, and host bare repositories. This is ideal for small teams needing speed and simplicity.
# Create git user and directories
sudo adduser --disabled-password --gecos "" git
sudo -u git mkdir -p /home/git/repos
sudo -u git mkdir -p /home/git/.ssh && sudo chmod 700 /home/git/.ssh
# Add collaborators' public keys
echo "ssh-ed25519 AAAA... user1@laptop" | sudo tee -a /home/git/.ssh/authorized_keys
sudo chmod 600 /home/git/.ssh/authorized_keys
sudo chown -R git:git /home/git
# Create a bare repo
sudo -u git git init --bare /home/git/repos/project.git
# Optionally restrict to git-shell
which git-shell
echo "$(which git-shell)" | sudo tee -a /etc/shells
sudo chsh -s "$(which git-shell)" git
From your workstation, add and push:
git remote add origin git@your-server:/home/git/repos/project.git
git push -u origin main
2. Git over HTTPS (Smart HTTP)
Expose repositories through Apache or Nginx with SSL/TLS and basic or token auth. This integrates well with corporate SSO and firewalls but requires more setup and maintenance.
- Pros: Works behind proxies, easy for users, integrates with TLS and auth.
- Cons: Heavier setup than SSH-only, needs web server hardening.
3. Git Platforms: Gitea/GitLab (full-featured)
Install a full platform for web UI, pull requests, issues, permissions, and CI/CD. Gitea is lightweight; GitLab is feature-rich. For teams that want a GitHub-like experience on-prem, start here.
What is Git and Why Use it on a Linux Server?
Git is a distributed version control system that records changes to files over time. On a Linux server, Git becomes the backbone for collaboration, code review, CI/CD, and zero-downtime deployments.
Secure Access with SSH Keys
SSH keys are the standard for secure Git access. Use modern algorithms and least-privilege.
# On the client
ssh-keygen -t ed25519 -C "you@domain.com"
ssh-copy-id -i ~/.ssh/id_ed25519.pub git@your-server
# Test connectivity
ssh -T git@your-server
- Disable password logins: set
PasswordAuthentication noin/etc/ssh/sshd_config, thensudo systemctl reload sshd. - Restrict users: use
AllowUsers gitor similar. - Harden permissions: ensure
~/.sshis 700 andauthorized_keysis 600. - Audit access: log SSH events and review
auth.logorjournalctl -u sshd.
Deploy from Git with Hooks (Zero-Downtime-Friendly)
Use a server-side post-receive hook to deploy after each push. For production, deploy to a release directory and symlink atomically.
# On the server, inside the bare repo
sudo -u git nano /home/git/repos/project.git/hooks/post-receive
# Paste:
#!/bin/bash
set -e
TARGET="/var/www/project"
REPO="/home/git/repos/project.git"
BRANCH="main"
read oldrev newrev refname
if [ "$refname" = "refs/heads/$BRANCH" ]; then
TMP_DIR=$(mktemp -d)
git --work-tree="$TMP_DIR" --git-dir="$REPO" checkout -f "$BRANCH"
rsync -a --delete "$TMP_DIR"/ "$TARGET"/
rm -rf "$TMP_DIR"
echo "Deployed $BRANCH to $TARGET"
fi
# Make it executable
sudo chmod +x /home/git/repos/project.git/hooks/post-receive
For advanced pipelines, integrate a CI server (Jenkins, GitLab CI, Gitea Actions) to run tests, build artifacts, and then deploy.
Team Workflows: Choose What Fits
Trunk-Based Development
- Short-lived feature branches merged into
mainquickly. - Continuous integration runs on every merge request.
- Pros: Fast releases, fewer merge conflicts.
- Cons: Requires strong CI and code review discipline.
Git Flow (Release/Hotfix Branches)
- Longer-lived
develop,release, andhotfixbranches. - Pros: Structured releases for larger teams.
- Cons: Heavier process; slower feedback loops.
Pick one workflow, document it, and enforce it with branch protection and required reviews.
Troubleshooting Common Git on Linux Server Issues
- Permission denied (publickey): ensure your public key is on the server in
~git/.ssh/authorized_keys. Check file permissions and test withssh -vvv git@server. - Repository ownership errors: ensure repo directories are owned by the
gituser and group. Usesudo chown -R git:git /home/git/repos. - Non-fast-forward errors: enable protected branches; if needed, allow force-push only for admins:
git config --system receive.denyNonFastForwards true. - Large files: use Git LFS for binaries; install and track:
git lfs install,git lfs track "*.zip". - Line endings: set
core.autocrlfappropriately, and use.gitattributesto normalize. - Slow clones: enable shallow clone (
git clone --depth 1) and considergit gc --aggressiveon the server during maintenance windows.
Maintenance, Performance, and Backups
- Garbage collection: schedule
git gcon large repos to repack and optimize. - Prune stale references:
git remote prune originand server-sidegit gc --prune. - Backup strategy: mirror repos with
git clone --mirroror snapshot the repo directory at the filesystem level. Test restores regularly. - Access control: restrict SSH to the
gituser, use firewalls, and keep Git updated. - Scaling: for many users, adopt Gitea/GitLab for built-in permissions, auditing, and CI.
Best Practices for Git on Linux Server
- Use a dedicated
gituser and limit shell access withgit-shellor command restrictions. - Store repos under a single path (e.g.,
/home/git/repos) with backups. - Enforce branch protections and code reviews before merging to
main. - Automate testing and deployment via hooks or CI/CD.
- Document your workflow, commit message conventions, and release process.
Infrastructure Tip: Picking the Right Server
Git performs best on fast CPU, NVMe SSD storage, and reliable networking. If you need predictable performance, snapshots, and security hardening, a managed VPS or dedicated server is ideal. At YouStable, our NVMe-powered VPS plans with free snapshots and DDoS protection make hosting private Git, Gitea, or GitLab deployments straightforward and secure.
Step-by-Step: Quick Private Git Server (Bare + SSH)
- Provision a Linux VPS, set hostname, update packages.
- Create a
gituser; set up~git/.ssh/authorized_keys. - Initialize a bare repo:
git init --bare /home/git/repos/app.git. - Push from your machine:
git remote add origin git@server:/home/git/repos/app.git, thengit push -u origin main. - Add a
post-receivehook to deploy to/var/www/app. - Enable backups and monitoring; review logs regularly.
FAQ’s: Git on Linux Server
Is Git different on Linux versus Windows or macOS?
Core Git functionality is identical across platforms. On Linux servers you benefit from native SSH, systemd services, and package-managed updates. Most production teams standardize on Linux for server-side Git due to performance and security tooling.
What’s the fastest way to set up a private Git server?
Use a bare repository over SSH with a dedicated git user. Generate SSH keys, create /home/git/repos/project.git with git init --bare, and push via git@server:/home/git/repos/project.git. Add a post-receive hook for deployment if needed.
How do I allow multiple users to push to one repository?
Use the shared git user with individual SSH keys in authorized_keys. For better control, run Gitea or GitLab to manage users, groups, and permissions per repo, plus audit logs and pull requests.
Which port does Git use, and can I change it?
SSH-based Git uses port 22 by default. You can run SSH on a different port by updating /etc/ssh/sshd_config and your firewall, then specify it in remotes (e.g., ssh://git@server:2222/home/git/repos/app.git).
Should I use Git LFS on a Linux server?
Yes, for large binary assets (images, videos, archives). Git LFS stores pointers in Git and the actual files separately, keeping clones fast and repositories lean. Install LFS on both clients and the server platform you use.
Conclusion
Understanding Git on a Linux server means mastering installation, SSH security, server-side repositories, and automation. Start with a lightweight bare repo and hooks, or adopt Gitea/GitLab for full workflows.
With the right server and habits—backups, branch protections, CI—you’ll ship faster and safer. Need a reliable host? YouStable’s NVMe VPS is built for private Git, CI, and high-availability deployments.