2. Git Fundamentals

Git is a distributed version control system. This means that each developer has a complete copy of the project history on their machine. Unlike centralized systems, Git lets you work offline and then synchronize your changes.

Our first step will be to install Git and set up our environment.

Installation and initial setup

Installation

On Linux (Ubuntu/Debian):

sudo apt-get update
sudo apt-get install git

On macOS:

brew install git

On Windows:

Download the installer from git-scm.com and follow the instructions.

Verify the installation

git --version

You should see something like git version 2.40.0.

Initial configuration

Before using Git, you need to configure your identity. This information will be included in every commit you make.

git config --global user.name "Your Name"
git config --global user.email "you@email.com"

It will be used to identify your commits.

To verify your configuration:

git config --list

Basic concepts

Repository

A repository (or "repo") is a directory that contains your project and its entire change history. Think of it as a special folder that Git monitors.

Commit

A commit is a snapshot of your project at a specific moment. It's like a photograph of all your files. Each commit has:

  • A unique identifier (hash)
  • A descriptive message
  • The author and the date
  • The changes made

Staging Area

This is an intermediate space where you prepare changes before confirming them. It lets you choose exactly which changes to include in each commit.

graph LR
    A[Working Directory
edited files] -->|git add| B[Staging Area] B -->|git commit| C[Repository]

Essential commands

git init

Creates a new Git repository in the current directory.

mkdir my-project
cd my-project
git init

You will see the message: Initialized empty Git repository in /path/my-project/.git/

git status

Shows the current state of your repository: which files have changed, which are staged, etc.

git status

git add

Adds files to the staging area.

# Add a specific file
git add file.txt

# Add all files
git add .

# Add all files with the .txt extension
git add *.txt

git commit

Saves the changes from the staging area to the repository.

# Commit with an inline message
git commit -m "Add login feature"

# Commit that opens the editor to write a longer message
git commit

Practical example:

# Create a file
echo "# My Project" > README.md

# Check the status
git status

# Add to staging
git add README.md

# Confirm the changes
git commit -m "Add initial README"

git log

Shows the commit history.

# View the full history
git log

# View a one-line summary per commit
git log --oneline

# View the last 5 commits
git log -n 5

# View the history as a graph
git log --oneline --graph --all

The .gitignore file

There are files you don't want to version: temporary files, dependencies, passwords, etc. The .gitignore file tells Git what to ignore.

Create a .gitignore file in the root of your project:

# Dependencies
node_modules/
venv/
__pycache__/

# Local configuration files
.env
.env.local
config.local.js

# IDE files
.vscode/
.idea/
*.swp

# System files
.DS_Store
Thumbs.db

# Logs
*.log

Profile configuration: .gitconfig

Your .gitconfig file contains personal settings. It lives in your home directory (~/.gitconfig).

# View your configuration
cat ~/.gitconfig

# Edit the configuration
git config --global --edit

Useful settings:

# Set the default editor
git config --global core.editor "code --wait"

# Colorize Git output
git config --global color.ui auto

# Useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'

Now you can use git st instead of git status.

Visual clients

Although the command line is powerful, visual clients can help you better understand the state of your repository.

Popular clients:

  • GitKraken: Cross-platform, intuitive interface
  • SourceTree: Free, from Atlassian
  • GitHub Desktop: Simple and effective, especially for GitHub
  • Git GUI: Created by the Git developers

Recommendation: Learn the command line first, then use visual clients to complement it.

Activity 1
  1. Install Git on your system and verify the version.
  2. Configure your username and email.
  3. Create a new directory called my-first-repo and initialize a Git repository.
  4. Create a README.md file with some content.
  5. Add the file to the staging area and make your first commit.
  6. Create a .gitignore file that ignores .log files.
  7. Check the history with git log.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.