Git is a version control system that helps developers track and manage changes to their code. Here's a brief overview of what Git can do:
- Track Changes: Git keeps a record of every modification to the code. If you make a mistake, you can revert to a previous version.
- Collaboration: Multiple developers can work on the same project simultaneously. Git manages changes from different sources efficiently.
- Branching: You can create branches in Git, allowing you to work on new features or bug fixes without affecting the main codebase. Once you're done, you can merge your changes back.
For example, imagine you're writing a story and decide to take it in two different directions. With Git, you can create two branches (one for each direction) and work on them separately. When you decide which one to go with, you can merge it into your main story.
In essence, Git is like a time machine and collaboration tool for your code, making it an essential tool for developers.
Git manages our project files through three main states: modified, staged, and committed, which correspond to the workflow steps:
- Modified: We've made changes to our files, but these changes are only on our computer. For example, we edit a file called index.html.
- Staged: We select the changes we want to save (commit). For example, we stage the index.html file using
git add index.html
. Now, Git knows we want these changes in the next snapshot but hasn't saved them yet. - Committed: By running the
git commit
command, we save the staged changes to Git's database on our computer. This means our changes are securely stored.
These states are part of three sections in a Git project:
- Working Tree: Our current project files that we can edit.
- Staging Area (Index): A file that tracks what will be in the next commit.
- Git Directory: Where Git stores our project's history and metadata.
The workflow is simple: modify files in our working tree, stage the ones we want to commit, and then commit them. This process ensures our project's history is well organized and our changes are safely recorded.
Installing Git on a Mac is pretty straightforward. We will explore two ways to install Git on a Mac.
Xcode Command Line Tools is a set of tools provided by Apple for developing software on macOS. We can check if Git is already installed on our Mac by running the following command in the Terminal.
git --version
This command will prompt us to install Xcode Command Line Tools if it is not already installed. We follow the on-screen instructions to install it, which includes Git.
Homebrew is a package manager for macOS and we can use it to install Git. We can install git using Homebrew by running the following command in the Terminal.
brew install git
To verify that Git has been installed successfully, we can run the following command in the Terminal.
git --version
This command will display the version of Git installed on our Mac.
Installing Git on Windows can be done with or without WSL. Let's explore both ways.
WSL is a compatibility layer for running Linux binary executables natively on Windows. We have a separate guide on how to install WSL on Windows. You can check it out here. You can install Git on your Windows using WSL by following these steps:
Step 1: Open your WSL terminal.
Step 2: Update the package list using the following command.
sudo apt update
Step 3: Install Git using the following command.
sudo apt install git
Step 4: Verify the installation by running the following command.
git --version
This command will display the version of Git installed on your system.
To install Git on Windows without WSL, we can download the installer from the official Git website. The installer will download the latest version of Git for Windows and provide us with an installation wizard. Follow the on-screen instructions to install Git on your Windows system. You can check out this link for navigating through the installation process. BigBinary academy doesn't recommend anyone to use git without WSL.
To get the basic Git tools on your Linux system, you can install it using the package manager that comes with your distribution. Let's see how we can install Git on some popular Linux distributions. Please open your terminal and follow the instructions below.
To install Git on Ubuntu, or any other Debian-based distribution, we can use the apt
package manager. We can install Git on Ubuntu by running the following command in the Terminal.
sudo apt install git-all
To install Git on Fedora, CentOS, or any other RPM-based distribution, we can use the dnf
package manager. We can install Git on Fedora by running the following command in the Terminal.
sudo dnf install git-all
We can verify the installation by running the following command.
git --version
This command will display the version of Git installed on our system.
If you're looking for more ways to install Git on different types of Linux systems, the Git website has easy-to-follow instructions for many Linux versions. Just visit the official Git website to find them.
The git init
is a command we use to initialize git on a directory. Think of it as setting up a new directory that Git will watch over, keeping track of all the changes we make to the files inside it. We can use the git init
command to initialize a new project or track the changes on an existing project. To do this,
- Open the terminal. (Git Bash application if you are using git natively in Windows)
- Create a new directory if you are starting with a new project:
mkdir directory-name
. You can skip this step if you want to initialize git on an existing project/directory. - Navigate inside the directory where you want to initialize Git:
cd directory-name
. - Initialize Git:
git init
.
By running the git init
command, Git creates a hidden directory called .git in our project directory. This .git
directory is where Git stores all the information about our project's history and changes. We will work with this directory only in rare cases, but it's important that it stays there.
Now Git has been initialized inside our project directory and it will start tracking all the changes we make within the directory.
When working with a GitHub repository, you will often need to identify yourself to GitHub using your username and password. An SSH key is an alternate way to identify yourself that doesn't require you to enter you username and password every time.
SSH keys come in pairs, a public key that gets shared with services like GitHub, and a private key that is stored only on your computer. If the keys match, you are granted access.
The cryptography behind SSH keys ensures that no one can reverse engineer your private key from the public one.
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
-
Open terminal
-
Enter
ls -al ~/.ssh
to see if existing SSH keys are present. This command lists the files in your.ssh
directory, if they exist -
Check the directory listing to see if you already have a public SSH key. By default, the filenames of supported public keys for GitHub are one of the following.
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub
If you don't already have an SSH key, you must generate a new SSH key to use for authentication.
First of all we need to create the directory where the key can be stored, if not already existing.
mkdir "$HOME/.ssh"
Then generate the ssh key.
ssh-keygen -t ed25519 -C "[email protected]"
If you are using a legacy system that doesn't support the Ed25519 algorithm, use this instead.
ssh-keygen -t rsa -b 4096 -C "[email protected]"
So in the above commands, [email protected]
should be replaced with the primary email that you had used to create your Github account.
This creates a new SSH key, using the provided email as a label.
> Enter a file in which to save the key (/Users/you/.ssh/id_algorithm): [Press enter]
When you are prompted to Enter a file in which to save the key press Enter. This accepts the default file location.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
At the prompt, type a secure passphrase. For more information, refer to Working with SSH key passphrases.
ssh-agent
is a program that stores your private keys. It starts when you log in. We will add a copy of our private key to this SSH agent.
Follow this detailed article by Github to add ssh key to ssh-agent
.
This is the final step. We will add the generated ssh key to Github.
Follow this detailed article by Github to add ssh key to your Github account.
As to verify everything is working as intended, we can run the following in the terminal.
ssh -T [email protected]
And it should ideally output something like this.
Hi "your-name"! You've successfully authenticated, but GitHub does not provide shell access.
Enforcing git add --patch
At BigBinary, we enforce using git add --patch
to review and stage changes selectively. This helps maintain clarity in commits and avoids unrelated changes being grouped together.
Using Aliases Consistently
Aliases can significantly simplify repetitive git commands. To maintain consistency across teams, we encourage setting up standard git aliases for common commands.
For example:
git add --patch
can be shortened togapa
.git commit --message
can be shortened togcmsg
.
For a more comprehensive list of useful Git aliases, refer to the Oh My Zsh Git Plugin documentation. This resource provides a curated set of aliases to optimize your git experience.