Getting Started with Git: The Essentials to Survive in a Repository

Automated English translation of the original Spanish article. Code examples keep their original identifiers.

When you start working on code projects, whether alone or on a team, you quickly run into a challenge: how do you keep track of changes without everything turning into total chaos? That’s where Git comes in as your lifesaver. Think of Git as a giant puzzle (the repo) where everyone builds their own section (branch) without messing up the main design. When your pieces are ready (commit), you add them to the shared board (push). Git takes care of fitting the pieces together without conflicts, and if there are any, it alerts you so you can adjust them manually. It’s a version control system that lets you track every change, collaborate with others without overwriting their work, and even roll back if something breaks.

Before diving into the commands, here are the key concepts so you don’t get lost:

  • Repository (or repo): Where the entire history of the project is stored.
  • Branch: A separate workspace to test or build features without touching the main history.
  • Commit: Saving your changes with a message describing what you did.
  • Staging area: Where you prepare what you’re about to save.

Keep in mind that you use Git locally on your computer, while platforms like GitHub or GitLab, which act like online storage for repositories, are used to share your work and collaborate with others.

1. Initial Configuration

Before you start working with Git, it is essential to configure it with your user information. This ensures that every commit is associated with the person who made the change. It is also recommended to verify that Git is correctly installed.

To configure your name and email, run:

git config --global user.name "Tu Nombre"
git config --global user.email "[email protected]"

Verify your Git installation with:

git --version

2. Basic Git Commands

2.1 Cloning Repositories

If you find a project you like on a platform like GitHub and want to download it to your machine, use git clone:

git clone https://github.com/usuario/repositorio.git

2.2 Branch Management

Branches allow you to work in an isolated environment without affecting the main version of the project.

  • Create a new branch:
git branch nueva-rama
  • Switch branches:

In recent documentation, you’ll see git switch as the recommended way to jump between branches:

git switch nueva-rama

I still use git checkout out of habit:

git checkout nueva-rama

Both commands work fine. Switch is clearer and more intuitive, but there’s no problem if you prefer sticking to classic checkout.

2.3 Repository Status

To see the status of your files, detect pending changes, or check if there are any untracked files, use:

git status

2.4 Staging Changes

Before committing, specify which changes should go to the staging area:

  • To stage a specific file:
git add archivo.txt
  • To stage all modified files:
git add .

2.5 Undoing Changes Before Committing

If you realize you need to unstage a change from your working area without deleting the modifications in the file:

git restore --staged archivo.txt

Alternatively, if you want to completely discard changes made to a file, you can run:

git restore archivo.txt

2.6 Committing Changes

Once changes are staged, commit them with:

git commit -m "Descripción del cambio"

2.7 Synchronizing with Remote Repositories

  • Push changes to the remote repository:
git push origin main
  • Download and integrate remote changes:
git pull origin main

Keep in mind that git pull automatically merges remote changes. If you only want to download them to review beforehand, you can use:

git fetch origin

3. Advanced Commands

3.1 Reverting Changes

If you need to undo a commit, Git provides the option to revert changes:

git revert <commit-hash>

Note that revert creates a new commit that reverses the previous one instead of altering history.

3.2 Merging Branches

To integrate changes from one branch into another, you can perform a merge:

git merge feature-rama

4. Practical Case: Resolving a Merge Conflict

4.1 Scenario

Imagine you have two branches: main and feature-navbar. Both branches contain changes to the same file, for instance, index.html.

4.2 Generating the Conflict

When attempting to merge feature-navbar into main:

git checkout main
git merge feature-navbar

Git will detect conflicting differences and halt the merge, alerting you that conflicts exist.

4.3 Identifying the Conflict

To identify which files are in conflict, run:

git status

Then, when you open index.html, you will see sections marked like this:

<<<<<<< HEAD
<!-- Cambios en main -->
=======
<!-- Cambios en feature-navbar -->
>>>>>>> feature-navbar

4.4 Resolving the Conflict

To resolve the conflict, edit the file manually by keeping the changes you want. Once resolved, delete the conflict markers (<<<<<<<, =======, >>>>>>>), save the file, and add the fixed version to the staging area:

git add index.html

Complete the merge by committing the changes:

git commit -m "Resuelve conflictos entre main y feature-navbar"

5. Conclusion

With this guide, you now command the Git essentials: configuring it, working with repositories, managing branches and commits, and even resolving conflicts like a pro. Knowing how to handle these conflicts is key to keeping workflows smooth, especially on a team.

If you want to go further, the official documentation and online resources are packed with tips to keep learning.

Mastering Git gives you control over your code and streamlines teamwork. Practice a bit more and you’ll see how everything falls into place; you’ll move from merely surviving to becoming the master of your repos. Keep coding and exploring the power of Git! Let’s keep coding.