Git and Linux: Complete Guide for Developers

Introduction

Git has become the world's most popular version control system, and its integration with Linux is almost inseparable. Many developers prefer to work on Linux distributions because they offer a stable, powerful and highly customizable environment for software development. In this article we will explore how to install Git in the main Linux distributions, configure it correctly and make the most of its features to manage projects of any size. From basic commands to advanced workflows with remote and branches, this guide will provide you with the necessary knowledge to use Git efficiently in your Linux workstation.

Git installation in Linux

The easiest way to install Git is by the package manager of your distribution. In Ubuntu and Debian, runsudo apt update && sudo apt install git. In Fedora, usesudo dnf install git. In Arch Linux, the command issudo pacman -S git. After installation, check the version withgit --version. It is recommended to configure your global name and email withgit config --global user.name 'Tu Nombre'andgit config --global user.email 'tucorreo@ejemplo.com'. These adjustments ensure that each commit carries your right identity, which is essential when you work together on shared projects.

Basic work flow

The typical flow of Git begins with the initialization of a repository:git init. Then add files to the preparation area withgit add <archivo>orgit add .to include all changes. Then you confirm the changes withgit commit -m 'Mensaje descriptivo'. To see the status of the repository, usegit statusand to review the history,git log. If you need to undo unprepared changes,git restore <archivo>returns them to the last commit; to undo published commitments, you can usegit revertorgit resetdepending on the situation. These commands form the basis on which more advanced operations are built.

Work with branches

The branches allow to develop characteristics, correct errors or experiment without affecting the main development line. You create a new branch withgit branch nombre-ramaand you change to it bygit checkout nombre-ramaor shortcutgit switch nombre-rama. Alternatively,git checkout -b nombre-ramacreate and change the branch in one step. When the work is complete, you merge the branch into the main withgit merge nombre-rama. If conflicts arise, Git will mark the affected files and you will have to edit them manually before completing the merger. Finally, it removes obsolete branches withgit branch -d nombre-rama.

Collaboration with remote repositories

To work with other developers, you need a remote repository hosted in services such as GitHub, GitLab or Bitbucket. Add the remote withgit remote add origin https://github.com/usuario/proyecto.git. Then send your local changes to the remote bygit push -u origin rama-principal. To get remote updates, usegit pull, which combinesgit fetchandgit merge. If you prefer to review the changes before merging them, you can create a pull request on the remote platform, allowing for code reviews and discussions before integrating the work. Keeping your branch up-to-date with the remote reduces conflict risks and facilitates teamwork.

Integration with CI / CD

Many continuous integration and continuous deployment pipelines depend on Git to shoot build and test. Platforms such as GitHub Actions, GitLab CI and Jenkins can be configured to run tasks each time a commitment is pushed or a request is opened. A typical configuration file, like.github/workflows/ci.yml, defines the steps: code checkout, unit installation, test execution and deployment to a staging environment. By combining Git with Linux, runners are usually run in containers or virtual machines based on distributions such as Ubuntu, which ensures consistency between the development and production environments.

Advanced tips and tricks

Some useful commands includegit stashto save temporary changes without making a commitment; andgit stash applyto recover them later. Usegit rebaseto rewrite the history and keep a clean line, but be careful when rewriting public commitments. The commandgit bisecthelps to locate the commit that introduced a binary search error. To display changes in a graphic way, tools likegitkor integrated interfaces in editors like VS Code improve experience. Finally, configure aliases in your file.gitconfigto shorten frequent commands, for example,alias co=checkoutoralias br=branch.

Conclusion

Dominating Git in Linux not only improves your individual productivity, but also facilitates collaboration in distributed development teams. The combination of the power of the Linux command line with the flexibility of Git creates an ideal environment to manage source code safely and efficiently. Following the steps and good practices described in this article, you will be well equipped to face any project, from small personal scripts to large business applications. Continue to explore official documentation and experiment with advanced workflows to take your skills to the next level.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish