How to Install and Use Git on CentOS
Git is a distributed version control system used to track source code, configuration files, scripts, and documentation. On a CentOS server, it helps administrators review changes, restore known-good versions, and collaborate without copying directories manually.
The commands below suit CentOS Stream 9 and similar RHEL-based systems. Package names and repository availability can differ on older CentOS releases, so check the operating system version before installing software. This is especially useful for teams working across Sydney, Melbourne, or Brisbane, where Git can keep server changes consistent between locations and time zones.
Check the CentOS Environment
Start by identifying the release and confirming that the system can reach configured repositories:
cat /etc/centos-release
sudo dnf repolist
CentOS Stream 9 normally uses dnf, while older CentOS versions use yum. The yum command may still work as a compatibility wrapper, but dnf is the standard package manager on current systems.
Update package metadata before installing Git:
sudo dnf makecache
sudo dnf update -y
On a production server, schedule updates during an approved maintenance window. Australian businesses may also need to consider the Privacy Act 1988 when repositories contain personal information, customer records, or application secrets.
Install Git From The Package Repository
Install Git with the distribution package:
sudo dnf install -y git
Verify the installed version:
git --version
rpm -q git
The repository version is generally the safest choice for server administration because it receives packaging and security updates through the normal CentOS process. Installing from source is useful only when a project requires a newer feature or a specific build configuration.
If the package cannot be found, inspect enabled repositories and DNS connectivity:
sudo dnf repolist --enabled
getent hosts mirror.stream.centos.org
Avoid downloading random RPM files from unofficial sites. A controlled repository or an internally approved package mirror makes later maintenance easier.
Set Your Git Identity
Git records an author name and email address in each commit. Configure them globally for the current user:
git config --global user.name "Alex Morgan"
git config --global user.email "alex.morgan@example.com"
Review the settings with:
git config --global --list
For a shared administration account, prefer a named Unix user wherever possible. Clear identities make audit trails easier to understand, particularly when several engineers maintain servers in Perth and Canberra.
Useful defaults include:
git config --global init.defaultBranch main
git config --global core.editor vim
git config --global color.ui auto
These values affect only the selected account unless they are written to a system-wide configuration file.
Choose The Right Installation Method
Most administrators should use the repository package. Source compilation creates additional update responsibility and can make vulnerability scanning more complicated.
| Method | Best use | Advantages | Limitations |
|---|---|---|---|
dnf install git |
Normal CentOS administration | Simple updates and dependency handling | May not be the newest Git |
| Enterprise package mirror | Managed business environments | Reproducible and centrally controlled | Requires repository maintenance |
| Build from source | Special feature or version requirement | Fine-grained version control | Manual upgrades and dependency work |
A source build typically requires development tools, a downloaded release archive, and a separate installation prefix. Unless a documented application requirement justifies that work, the CentOS package is the more maintainable option.
Once Git is installed, record the version in your server build notes. Reproducible documentation matters when replacing a virtual machine or rebuilding a host after an incident.
Create And Clone A Repository
Create a local repository for a project or administration script:
mkdir ~/server-scripts
cd ~/server-scripts
git init
Add a file and inspect its state:
printf '# Server scripts\n' > README.md
git status
git add README.md
git commit -m "Add project documentation"
git status shows untracked and modified files, while git diff displays changes that have not been staged. This review step helps prevent passwords, private keys, and temporary files from entering history.
To obtain an existing project, clone it into the current directory:
git clone https://example.org/team/project.git
cd project
git branch --show-current
For private services, SSH authentication is usually preferable to placing credentials in a URL. Keep secrets outside the repository and add suitable patterns to .gitignore.
Work With Branches And Remotes
A branch allows changes to be developed separately from the stable line:
git switch -c update-nginx-config
git status
git add nginx.conf
git commit -m "Tune Nginx worker settings"
Review recent commits and compare changes before merging:
git log --oneline --decorate --graph -10
git diff main...update-nginx-config
Connect a local repository to a remote server with:
git remote add origin git@example.org:team/project.git
git remote -v
git push -u origin update-nginx-config
Use git pull --rebase only when the team’s workflow supports it. On a server, never pull unreviewed changes directly into a production directory. Fetch the revision, test it, and deploy through a controlled process.
For terminal users who edit long configuration files, Emacs code folding can make large Git-managed files easier to inspect without changing their contents.
Protect The Repository And Server
Git does not encrypt files in a repository. Restrict the working directory and its metadata:
chmod 700 ~/server-scripts
find ~/server-scripts -type f -exec chmod 600 {} \;
Use .gitignore for logs, environment files, compiled output, and local credentials:
cat > .gitignore <<'EOF'
.env
*.log
/tmp/
/build/
EOF
If a repository is hosted on the CentOS machine, protect its network exposure with firewalld security rules. Allow only required services, such as SSH or HTTPS, and avoid opening Git-related ports without a defined hosting design.
The essential habit is to treat every commit as a permanent record: review the diff, exclude secrets, identify the author, and use a trusted remote. Git installation is quick, but disciplined repository management is what makes version control dependable on CentOS.