Getting Started with Git Installation
Installing on Linux
For Linux distributions, installing Git through the native package manager is straightforward.
On Red Hat-based systems:
sudo yum install git
On Debian-based systems:
sudo apt install git
Installing on Windows
Download the appropriate installer from the official Git website matching your system architecture. Alternatively, the GitHub for Windows package bundles both a graphical interface and the command-line tool, along with PowerShell integration, credential caching, and sensible line-ending defaults.
Enstalling on macOS
Two common approaches exist:
- Using Homebrew: run
brew install git. - Through Xcode: after installing Xcode from the App Store, navigate to
Xcode→Preferences→Downloads, selectCommand Line Tools, and clickInstall. Xcode ships with Git pre-integrated.
Why Version Control Matters
A version control system provides a central repository to store all code and documentation. Every modification gets recorded and becomes traceable. Team members can commit work and integrate changes seamlessly. Key benefits include:
- Historical tracking of all changes pushed to the repository.
- Safe retrieval of code from the repository if local copies are lost.
- Branching strategies to isolate feature development or bug fixes.
- Automated build triggers on commit in enterprise environments for continuous quality checks.
- Automatic rollback capabilities when builds fail.
Configuring Git Identity
The initial step after installing Git is to define your identity. This information embeds into every commit permanently.
git config --global user.name "alexjordan"
git config --global user.email "alex.jordan@example.dev"
The --global flag applies these values system-wide. To override them for a specific project, run the same commands without --global inside that project's directory. Use git config --list to inspect all active settings. Graphical clients often prompt for these details during first launch.
Accessing Help
Git includes built-in documentation accessible in three ways:
git help <verb>
git <verb> --help
man git-<verb>
For example, to view the manual for config:
git help config
Initializing a Repository
A repository is a directory under Git management where all file modifications—additions, edits, deletions—are tracked and can be reverted.
mkdir sample_project
cd sample_project
git init
Executing git init creates a .git subdirectory containing the repository's internal metadata. Once initialized, files can be staged and committed.
Working with the Staging Area
New files are not tracked automatically. Running git status reveals their current state. The git add command moves changes into the staging area.
Stage all modifications:
git add .
Stage files by extension:
git add *.cpp
Stage files starting with a specific prefix:
git add hello*
Stage files matching a single-character wildcard:
git add hello?.*
To remove files from the staging area without deleting them from disk, use:
git rm --cached <file>
Recording Changes with Commits
Staging alone does not finalize tracking. The git commit command persists staged changes to the repository's history.
Commit all staged and previously tracked modifications:
git commit -a -m "initial setup"
Commit specific files directly:
git commit Makefile app.js -m "update build config and add entry point"
Crafting Meaningful Commits
git commit file1.js file2.css file3.md -m "descriptive message"
The -a flag includes all tracked changes from the staging area but does not stage new, untracked files—contrary to some misconceptions.
Amend the most recent commit (rewriting history locally):
git commit --amend
A Structured Commmit Convention
Adopt a consistent format inspired by widely-used conventions:
<type>(<scope>): <subject>
<body>
<footer>
- type: The category of change:
feat: a new featurefix: a bug fixrefactor: code restructuringdocs: documentation updatesstyle: formatting adjustments (not CSS)test: test-related changeschore: maintenance tasks like dependency updates
- scope: The affected component or module (e.g.,
router,auth,build). - subject: A concise summary of the commit.
- body: Detailed explanation of the motivation and changes.
- footer: References to breaking changes or linked issues.
Using a Commit Template
Configure a template to streamline adherence:
In ~/.gitconfig:
[commit]
template = ~/.gitmessage
Create ~/.gitmessage:
<type>(<scope>): <subject>
<body>
<footer>
# type options:
# feat, fix, docs, style, refactor, test, chore
# scope: affected module (optional)
# subject: brief description