Git

Introduction

Git is a powerful, open-source version control system (VCS) designed to handle projects of all sizes with speed and efficiency. It allows developers to track changes in source code, collaborate on projects, and maintain a history of edits. Git is widely used in software development and is the backbone of many DevOps workflows.


History

  • Created by Linus Torvalds: Git was developed in 2005 by Linus Torvalds, the creator of Linux, to manage the Linux kernel's development after a dispute over the BitKeeper version control system.
  • Designed for Collaboration: Torvalds emphasized speed, simplicity, and distributed workflows, making Git ideal for large teams working on complex projects.
  • Open-Source Success: Over time, Git has become the standard for version control, widely adopted by developers and organizations globally.

Linus Torvalds and the Canoeing Analogy

Linus Torvalds often described Git and its terminology using the analogy of canoeing. He compared version control to managing multiple canoes traveling down a river:

  1. Branches:

    • Represent canoes navigating their own paths (branches) in the river.
    • Each canoe is independent but can eventually rejoin (merge) the main flow of the river.
  2. Merging:

    • Merging branches is akin to two canoes meeting and combining their progress into a single route.
  3. Commits:

    • Each paddle stroke represents a commit—individual contributions that propel the canoe forward.
  4. Rebase:

    • Rebase is like picking up your canoe and moving it to a different part of the river to follow another route, aligning with the main current.

This analogy simplifies understanding Git's workflows and highlights its purpose: to enable teams to collaborate seamlessly without losing individual progress or diverging too far from the main goal.


Importance of Git

  1. Distributed Version Control: Every user has a complete copy of the repository, ensuring redundancy and enabling offline work.
  2. Branching and Merging: Git makes it easy to experiment with changes using branches, which can be merged into the main project once tested.
  3. Collaboration: Teams can work on the same codebase simultaneously without overwriting each other's work.
  4. History Tracking: Every change is logged, allowing developers to understand who made changes, what was changed, and when.
  5. Integration: Git integrates with platforms like GitHub, GitLab, and Bitbucket for enhanced collaboration and project management.

Usage

Git is primarily used for:

  • Version Control: Track changes in code over time.
  • Collaboration: Enable teams to work together on the same codebase.
  • Backup and Restore: Protect work with a distributed repository.
  • Experimentation: Safely test new features using branches.

Common Git CLI Commands

Configuration

  • git config --global user.name "Your Name"
    Set your username globally.
  • git config --global user.email "your.email@example.com"
    Set your email globally.
  • git config --global core.editor "nano"
    Set the default text editor.

Repository Initialization

  • git init
    Initialize a new Git repository in the current directory.
  • git clone <repository-url>
    Clone an existing repository.

Working with Files

  • git add <file>
    Stage changes for the next commit.
  • git add .
    Stage all changes in the current directory.

Committing Changes

  • git commit -m "Commit message"
    Commit staged changes with a message.
  • git commit -a -m "Commit message"
    Stage and commit changes in one step.

Branching

  • git branch
    List all branches.
  • git branch <branch-name>
    Create a new branch.
  • git checkout <branch-name>
    Switch to a branch.
  • git checkout -b <branch-name>
    Create and switch to a new branch.

Merging

  • git merge <branch-name>
    Merge the specified branch into the current branch.

Viewing History

  • git log
    View commit history.
  • git log --oneline
    View a condensed commit history.

Remote Repositories

  • git remote add origin <repository-url>
    Add a remote repository.
  • git push -u origin main
    Push changes to the remote repository's main branch.
  • git pull origin main
    Pull the latest changes from the remote repository.

Undoing Changes

  • git reset <file>
    Unstage a file.
  • git checkout -- <file>
    Discard changes to a file.

Generating an Ed25519 SSH Key for GitHub

Step 1: Generate the SSH Key

 ssh-keygen -t ed25519 -C "your.email@example.com"

Step 2: Check SSH Key Agent Status

eval "$(ssh-agent -s)"

Step 3: Add SSH Key to Agent

ssh-add ~/.ssh/id_ed25519

Step 4: Copy SSH Key to clipboard

cat ~/.ssh/id_ed25519.pub | xclip
  • Copy and paste into github ssh settings.

Example Pubkey

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBnX2RcH3jFrJv0G7G7Q0n3kF1HQoYF1tHkK9jEMj8ez example@example.com