This chapter, introduction to Git, discusses a distributed version control system that helps you track changes in your code, collaborate with others, and manage projects efficiently. By the end of this chapter, you’ll be able to create repositories, commit changes, work with branches, and collaborate using Git.

Introduction to Git

Git is a vital tool for managing code and collaborating on projects. It enables:

Git is essential for scripting projects (Chapter 25) or managing backups (Chapter 26), empowering you to organize and share your work. This chapter presents this useful command line tool.

Getting Started with Git

The first step in our chapter introduction to git, is installing git.

Install Git on Debian 12:

$ sudo apt install git

For Red Hat/Fedora, use:

$ sudo dnf install git

Configuring Git

Set your identity for commits:

$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"

Checking Git Version

Verify installation:

$ git --version

Creating and Cloning Repositories

Creating a Repository

Initialize a new repository:

$ git init

Cloning a Repository

Clone a remote repository:

$ git clone https://github.com/username/repository.git

Basic Git Workflow

Tracking Changes

Manage changes with these steps:

Example:

$ git status
$ git add file.txt
$ git commit -m "Add file.txt"

Viewing Commit History

Review commits:

$ git log

Working with Branches

Creating a Branch

Create a feature branch:

$ git branch new_feature

Switching Branches

Switch to a branch:

$ git checkout new_feature

Merging Branches

Merge changes into main:

$ git checkout main
$ git merge new_feature

Deleting a Branch

Remove a merged branch:

$ git branch -d new_feature

Remote Repositories

Adding a Remote

Link a remote repository:

$ git remote add origin https://github.com/username/repository.git

Pushing Changes

Upload commits:

$ git push origin main

Pulling Changes

Download updates:

$ git pull origin main

Resolving Conflicts

Identifying Conflicts

Git marks conflicts in files during merges, e.g., <<<<<<< markers.

Resolving Conflicts

Steps to resolve:

  1. Edit conflicting files to merge changes.
  2. Stage resolved files: git add file.txt
  3. Complete merge: git commit

Example:

$ git add file.txt
$ git commit

Modern Git Tools

LazyGit and Tig

Visual interfaces simplify Git tasks:

$ sudo apt install lazygit
$ lazygit
$ sudo apt install tig
$ tig

Git Hooks

Automate tasks like linting:

$ echo '#!/bin/sh\neslint .' > .git/hooks/pre-commit
$ chmod +x .git/hooks/pre-commit

Practical Examples

Feature Branch Workflow

Create and merge a feature:

$ git branch feature_branch
$ git checkout feature_branch
$ git add .
$ git commit -m "Add feature"
$ git checkout main
$ git merge feature_branch

Collaborative Project

Clone and push changes:

$ git clone https://github.com/username/repository.git
$ git add .
$ git commit -m "Update project"
$ git push origin main
Image for blog post Introduction to git
Image for blog post Introduction to git

Practice Time!

Test your skills:

  1. Create a Git repository and add a file.
  2. Commit changes and view history.
  3. Create and merge a branch.
  4. Push changes to a remote repository.

Try This: Run git init and share your success on X with #LinuxCommandLine!


Conclusion

Git is an indispensable tool for version control and collaboration. Using this chapter, introduction to Git, you can efficiently manage your projects, collaborate with others, and maintain a history of your work. Whether you’re working on a small personal project or a large team effort, Git will help you stay organized and productive.


Previous: Chapter 27 | Next: Chapter 29