Introduction
Git is an essential tool for developers, system administrators, and anyone working with code or version-controlled files. It enables collaboration, keeps track of changes, and allows you to revert to previous versions when needed. Whether you’re working alone or as part of a team, Git makes version control simple and efficient.
This guide will introduce you to Git, how to install it on various operating systems, configure it, and start using its most common commands.
What is Git?
Git is a distributed version control system created by Linus Torvalds in 2005. It allows multiple users to collaborate on a project, track changes, and manage different versions of code efficiently. Unlike centralized systems, Git operates locally and does not require an internet connection to manage repositories.
Why Use Git?
- Version Control: Keeps track of changes and allows you to revert when needed.
- Collaboration: Multiple people can work on the same project without conflicts.
- Branching & Merging: Create different versions of a project and merge them seamlessly.
- Speed & Efficiency: Git is optimized for performance and can handle large projects.
Installing Git
- Download Git for Windows from the official website: https://git-scm.com
- Run the installer and follow the instructions.
- Open Git Bash to start using Git.
On MacOS:
If you’re using MacPorts
:
$ sudo port install git
If you’re using Homebrew (Recommended):
$ brew install git
On FreeBSD:
To install the pre-compiled package:
$ pkg install git
To install it using portmaster
:
$ portsnap fetch update && portmaster -d -B git
If you prefer to compile it yourself:
Using portmaster
:
$ portmaster -v devel/git
Or, the traditional way:
$ cd /usr/ports/devel/git
$ make install clean
$ rehash
Such as: Debian / Ubuntu / Linux Mint / Devuan / elementary OS / MX Linux / antiX / deepin / Linux Lite / Zorin OS / LXLE / Peppermint OS / SparkyLinux:
$ sudo apt update && sudo apt install git
If apt
wasn’t installed by default, then use apt-get
or aptitude
instead.
On Fedora / RedHat 8+ / CentOS 8+ / Scientific Linux 8+ / AlmaLinux / Rocky Linux:
$ sudo dnf install git
On old RedHat 7 / CentOS 7 / Scientific Linux 7:
$ yum install git
On ArchLinux and its its derivatives: (like: Manjaro / KaOS / ArchBang Linux / BlackArch Linux / Parabola GNU/Linux-libre / Chakra GNU/Linux):
$ pacman -S git
On Gentoo:
$ emerge --ask dev-vcs/git
On Void Linux:
$ xbps-install -S git
On NixOS:
$ nix-env -iA nixpkgs.git
On Slackware:
$ slackpkg install git
On Alpine:
$ apk update && apk add git
After installation, verify Git with:
git --version
Configuring Git
Once installed, configure Git with your user information:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
To check your configuration:
git config --list
If you prefer to use VSCode as your Git editor:
git config --global core.editor "code --wait"
To edit your configuration:
git config --global -e
Handling Line Endings with core.autocrlf
Git handles line endings differently on various operating systems. The core.autocrlf
setting ensures that line endings are converted correctly based on your OS.
Recommended Settings for Different OS:
- Windows:
git config --global core.autocrlf true
This setting converts LF (Linux/macOS style) to CRLF when checking out files and converts them back to LF when committing. - Linux/macOS/FreeBSD:
git config --global core.autocrlf input
This ensures that Git converts CRLF to LF when committing but does not modify files on checkout, keeping them in LF format. - If you want to disable automatic conversion:
git config --global core.autocrlf false
This setting leaves line endings unchanged.
Using the correct core.autocrlf
setting prevents unnecessary changes in repositories due to line ending differences, making collaboration across different operating systems smoother.
Getting Help in Git
Git provides built-in help for commands:
git help <command>
For example, to get help with clone
:
git help clone
You can also check the official documentation at https://git-scm.com/doc.
Most Used Git Commands (Cheat Sheet)
Command | Description |
---|---|
git init | Initialize a new Git repository |
git clone <repo> | Clone a repository |
git status | Show changes in the working directory |
git add <file> | Stage a file for commit |
git commit -m "message" | Commit changes with a message |
git push origin <branch> | Push changes to a remote repository |
git pull origin <branch> | Fetch and merge updates from a remote repository |
git branch | List branches |
git checkout <branch> | Switch to a branch |
git merge <branch> | Merge changes from another branch |
git log | Show commit history |
git reset --hard <commit> | Reset to a specific commit |
git stash | Save changes temporarily |
git tag <name> | Create a tag for a commit |
git remote -v | Show remote repositories |
Conclusion
Git is a powerful and widely used tool for version control, making collaboration and project management easier. By installing and configuring Git, you can start tracking changes, collaborating with others, and improving your workflow. Whether you are a solo developer or working in a team, mastering Git will greatly enhance your productivity.
Happy coding!