Just Git like a pro
Tags: git, github, opensource, programming
Published: 2024-02-27 09:41:31

In the ever-evolving world of software development, managing code changes is paramount. This is where Version Control Systems (VCS) come into play. A VCS is a system that tracks changes made to files over time, allowing developers to revert to previous versions, collaborate efficiently, and maintain a clean history of their project's evolution. Using a VCS can require some special tips and tricks to consider.

## What are Version Control Systems
![Thinking Badman](https://i.gifer.com/1Vv.gif)
At its core, a Version Control System is a tool that records changes to a file or set of files over time, enabling you to recall specific versions later. In a version control we take snapshots of changes brought to our code overtime and each snapshot identify by a message and its meta data.

## Types of VCS
VCS exiting in two main types: Centralized VCS and Distributed VCS. Let's hear more from this.

### Centralized Version Control System (CVCSs)
These are VCS that rely on a centralized server to store all versions of a project's files. While this setup simplifies the versioning process for many scenarios, it also introduces a single point of failure. If the server goes down or the repository is corrupted, the history of the project could be lost. Some of which include the SVN.

### Distributed Version Control Systems (DVCS)
Just like Git and Mercurial, allow multiple copies of the entire repository to exist simultaneously on different systems. This methodology not only provides a backup but also enables more flexible collaboration and branching strategies.

## Why Git is best?
![thinking man](https://i.pinimg.com/originals/b0/61/11/b06111ca4f61490fe7dc535e0250d603.gif)

Git, created by Linus Torvalds in 2005 for Linux kernel development, has become the de facto standard for distributed version control. Its popularity stems from its robustness, flexibility, and efficiency in handling projects of any size. Unlike CVCS, Git's distributed nature allows every contributor to have a full copy of the project's history, making operations like branching and merging faster and reducing reliance on a central server. Git is a free and open-source DVCS that efficiently handles small to very large projects with speed and accuracy. It's designed to handle everything from small to very large projects with speed and efficiency. Git enables multiple developers to work together on the same project without stepping on each other's toes. It’s a system that records changes to a file or set of files over time so that you can recall specific versions later.

### Git features
![Git Features](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lndve7vyt3oofi70879.png)
Git provides features like:
- **Branching**: Allows creating isolated copies of the codebase for independent development, perfect for bug fixing and feature implementation.
- **Merging**: Combines changes from different branches back into the main codebase.
Version tracking: Maintains a complete history of all changes, enabling reverting to previous states and reviewing code evolution.
- **Collaboration**: Enables seamless collaboration among developers by allowing them to share their branches and merge changes easily.

## Let's dive in (Customizing your Git experience) 🚀
![Diving person](https://i.pinimg.com/originals/7c/76/12/7c7612ad733d4c7ecfde3f79cfde779f.gif)
Now let do something cool, customizing some experience we have in git and doing some ease configurations to ease our experience with git VCS.
### Setting Global Git Configurations:
Let's explore some custom configuration, some of which are necessary and others are all optional.
```bash
# command you must run
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

# optional: beautifying
git config --global color.status.added green
git config --global color.status.changed yellow

# Configure default branch
git config --global init.defaultbranch main
```
### Ignoring files globally
Create a `.gitignore` file in your home directory to specify files or folders you don't want Git to track. Common examples include:
```bash
# for Linux or bash terminals
touch .gitignore
```
After creation you can open and drop whatever content you want in there.
### Secure Collaboration with SSH and GPG:
#### **Secure Shell (SSH)** is a protocol used to securely log into another computer over a network, to execute commands in a remote machine, and to move files from one machine to another. It provides a secure channel over an unsecured network in a client-server architecture.

GitHub and other platforms use SSH keys to securely communicate with your machine. To handle multiple accounts on a single PC, you can generate separate SSH keys for each account and add them to the SSH agent. This setup involves creating a config file for SSH to manage these keys efficiently.
```bash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```
- Next step to follow will be on [GitHub](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account)
![Adding ssh to GitHub](https://i.stack.imgur.com/in7KG.gif)


#### GNU Privacy Guard (GPG):
This is a tool for encrypting and signing your data and communications. GitHub uses GPG signatures to verify that commits and tags are created by a specific account. Configuring GPG involves generating a GPG key pair, adding the GPG key to your GitHub account, and configuring Git to use your GPG key for signing commits and tags.

By understanding and implementing these configurations, developers can enhance their Git experience, ensuring efficient and secure management of projects. Git's flexibility and robustness, combined with its ability to be customized to fit any development workflow, make it an unparalleled tool in the arsenal of modern developers. Whether you're working on a small project or a massive enterprise, mastering Git and its configurations is a step toward professional growth and enhanced collaboration in the software development world.

Let's do that:
```bash
# install gpg and generate a key pair
gpg --gen-key

# configure git to use it
git config --global user.signingkey your_key_id
```

- Next on [GitHub](https://docs.github.com/en/authentication/managing-commit-signature-verification/adding-a-gpg-key-to-your-github-account)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykaleiwj3xy39q477rea.png)

These configurations, however, are specific to a single GPG key. To manage multiple accounts on a single PC, consider using tools like gpg-agent and dedicated configuration setups for each account.

## SSH and GPG Configuration for Multiple Git Accounts
- For each account from remote VCS create an SSH and GPG using the previously given commands.
- Create or modify the `~/.ssh/config` file to add configurations for each Git account. Here's an example setup for GitHub:
```txt
# Account 1
Host github.com-account1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_account1

# Account 2
Host github.com-account2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_account2
```
Replace id_rsa_account1 and id_rsa_account2 with the actual file names of your SSH keys. Remember to use the name without `.pub`, the `.pub` will be dropped entered on GitHub.

- As for the GPG we will play a little around with the `.gitconfig` file at the home of your directories.
 - For each remote VCS account you are use, you need a configuration file for it.
 - Each account should have a folder which becomes that account default working directory.
 - These are some measures we take in order to play around with git conditioning.

A Sample of my `.gitconfig` file:
```txt
[init]
  defaultBranch = main

[commit]
  gpgsign = true

[includeIf "gitdir:~/Work1/"]
  path = ~/.gitconfig.work1

[includeIf "gitdir:~/work2/"]
  path = ~/.gitconfig.work1

[includeIf "gitdir:~/Personal/"]
  path = ~/.gitconfig.personal
[push]
        default = current
        autoSetupRemote = true
[core]
        excludesfile = ~/.gitignore_global
```
> The default value for `excludesfile` is `.gitignore`.

## Summarize
### Table 1:  Types of Version Control Systems Comparison

| Feature                         | CVCS (e.g., SVN)            | DVCS (e.g., Git, Mercurial) |
|---------------------------------|-----------------------------|-----------------------------|
| **Architecture**                | Centralized repository      | Distributed repositories    |
| **Access Speed**                | Fast for centralized access | Fast for local operations   |
| **Internet Connection Required**| Yes, for most operations    | No, for most operations     |
| **Collaboration**               | Managed through central server | Peer-to-peer collaboration |
| **Branching and Merging**       | Possible but less efficient | Highly efficient            |
| **Backup and Restore**          | Single point of failure     | Every clone is a full backup|
| **Learning Curve**              | Moderate                    | Steep for beginners         |
| **Use Case**                    | Small to medium teams/projects | Any size team/project     |


### Table 2: Git Features Summary Comparison
| Feature                   | Git                               | SVN                  | Mercurial           |
|---------------------------|-----------------------------------|----------------------|---------------------|
| **Type**                  | DVCS                              | CVCS                 | DVCS                |
| **Branching & Merging**   | Easy and fast                     | More complex and slower | Easy but slightly less efficient than Git |
| **Local Operations**      | Almost all operations are local   | Limited              | Most operations are local |
| **Storage Efficiency**    | Highly efficient (compression)    | Less efficient       | Efficient but less so than Git |
| **Network Performance**   | Excellent (fetch/push what's needed) | Can be slower        | Very good           |
| **Integrity**             | SHA-1 hash for content identification | Relies on revision numbers | SHA-1 hash for content identification |
| **Staging Area**          | Yes (index)                       | No                   | No                  |
| **Learning Curve**        | Steep                             | Moderate             | Moderate            |
| **Community and Support** | Extremely large and active        | Large                | Large but smaller than Git |

## My POV
In summary, the dominance of Git in the software development world is a testament to its flexibility, power, and the vibrant community that supports it. Whether you're working on open-source projects or enterprise software, Git provides the tools and ecosystem to manage your codebase effectively, making it an indispensable tool for modern software development.

## For the curious ones
{% embed  https://www.youtube.com/watch?v=aolI_Rz0ZqY %}

## Let's wrap up with this
The evolution from Centralized to Distributed Version Control Systems (VCS), particularly with Git's ascendancy, marks a significant milestone in software development, offering unparalleled flexibility, robustness, and efficiency in managing codebases. Git not only revolutionizes collaboration through its distributed nature and integration with platforms like GitHub, GitLab, and Bitbucket but also caters to a wide range of project sizes with its superior handling of branches, merges, and local operations. Mastering Git's complexities unlocks its full potential, enhancing code quality, project manageability, and collaboration. As a cornerstone of modern software development, Git's widespread adoption underscores its indispensable role in facilitating dynamic development workflows and community-driven innovation.