Git
Track changes, collaborate on code, and manage server configurations with Git version control on Linux, macOS, and Windows.
- Git
- What Git Does and When to Use It
- How to Install Git
- Core Concepts of Git
- Git Repository and Working Directory
- Git Branching and Merging
- Git Remote Repositories
- Common Tasks with Git
- How to Initialize a Git Repository
- How to Clone an Existing Repository with Git
- How to Stage and Commit Changes with Git
- How to View Commit History with Git
- How to Create and Switch Branches with Git
- How to Undo the Last Commit with Git
- Git Troubleshooting
- Related Tools and Guides
Git
Git is a distributed version control system that tracks changes to files, enables collaboration through branching and merging, and maintains a complete history of every modification on Linux, macOS, and Windows.
What Git Does and When to Use It
Git records snapshots of file changes in a repository. Every commit stores the state of all tracked files, who made the change, when, and a message describing why. System administrators use Git to version-control server configurations (
/etc/nginx/,
/etc/systemd/system/), deployment scripts, Infrastructure as Code (IaC) definitions, and documentation.
Git is distributed — every clone contains the full repository history. This means operations like log, diff, and blame run locally without network access. Remote repositories (on GitHub, GitLab, Bitbucket, or a self-hosted server) serve as central collaboration points, but Git does not require one.
Git is not a backup system. It tracks text-based files efficiently but stores every version in its history, making large binary files problematic. For large binaries, use Git LFS (Large File Storage). For official documentation, see git-scm.com/doc.
How to Install Git
=== "Ubuntu / Debian"
sudo apt install git=== "RHEL / CentOS / Fedora"
sudo dnf install git=== "macOS"
Git ships with Xcode Command Line Tools:
xcode-select --installConfigure identity after installation:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"Core Concepts of Git
Git Repository and Working Directory
A Git repository is a directory containing a
.git/ subdirectory that holds the commit history, branches, and configuration. The working directory contains the current checked-out version of the files. The staging area (index) holds changes that will be included in the next commit.
Git Branching and Merging
A branch in Git is a lightweight pointer to a commit. The default branch is typically
main (formerly
master). Creating a new branch copies the pointer — it does not duplicate files. Merging combines the changes from one branch into another. Git performs fast-forward merges when possible and three-way merges when branches have diverged.
Git Remote Repositories
A remote is a reference to a repository hosted elsewhere (e.g., on GitHub or a self-hosted GitLab server).
origin is the conventional name for the default remote.
git push sends local commits to the remote;
git pull fetches and merges remote changes into the local branch.
Common Tasks with Git
How to Initialize a Git Repository
Git creates a new repository in the current directory with
git init:
cd /etc/nginx
sudo git init
sudo git add .
sudo git commit -m "Initial commit of Nginx configuration"How to Clone an Existing Repository with Git
Git downloads a complete copy of a remote repository with
git clone:
git clone git@github.com:user/repo.gitHow to Stage and Commit Changes with Git
Git tracks changes in two steps — staging (
git add) and committing (
git commit):
git add nginx.conf
git commit -m "Increase worker_connections to 4096"How to View Commit History with Git
Git displays the commit log with
git log. Use
--oneline for a condensed view:
git log --oneline --graph --allHow to Create and Switch Branches with Git
Git creates a new branch and switches to it in one command:
git checkout -b feature/ssl-configOr using the newer
switch command:
git switch -c feature/ssl-configHow to Undo the Last Commit with Git
Git removes the last commit but keeps the changes staged with
--soft:
git reset --soft HEAD~1Discard the last commit and all its changes with
--hard:
git reset --hard HEAD~1Git Troubleshooting
| Error / Symptom | Cause | Fix |
|---|---|---|
fatal: not a git repository | Current directory is not inside a Git repository | → Full article |
error: failed to push some refs | Remote has commits not present locally; pull before pushing | → Full article |
CONFLICT (content): Merge conflict in file | Two branches modified the same lines in a file | → Full article |
HEAD detached at commit | Checked out a specific commit instead of a branch | → Full article |
Related Tools and Guides
SSH key-based authentication is required for Git operations over SSH (e.g.,
git@github.com). See the
SSH article.
Ansible playbooks and roles should be version-controlled with Git. See the Ansible article.