Basic Concepts
Git is a distributed version control tool that allows developers to manage and record changes to a software project in a collaborative and efficient manner.
Basic commands
-
git init`: Initializes a new repository in a directory.
- Example: If you have a folder called my_project, you can initialize a repository inside it using:
git init
This will create a new empty repository in the current directory, allowing you to start versioning your project.
-
git remote add
: Adds a remote repository to which changes can be committed.- Example: To add a remote repository named origin, you can use:
git remote add origin https://domain.com/usuario/repo.git
This links your local repository to the specified remote repository, which will allow you to later use commands such as git push
or git pull
to interact with the remote repository.
-
git clone`: Clone an existing repository from a server or remote location.
- Example: To clone a repository from a remote repository, you can use:
git clone https://domain.com/usuario/repo.git
This will download a local copy of the specified remote repository.
-
git add`: Add files to the staging area to be part of the next commit.
- Example: If you have modified a file called index.html, you can add it to the preparation area with:
git add index.html
This indicates that you want to include index.html in the next commit.
-
git commit
: Saves the changes prepared in the repository history, creating a snapshot of the current state.- Example: To make a commit with a descriptive message, you can use:
git commit -m "Añadir nueva funcionalidad a la página principal"
This will save the changes in the history with the message provided.
-
git status
: Shows the current status of the work area, the modified files, and those ready to be "committed".- Example: To see which files have changed or are ready to be added, simply run:
git status
This will give you a clear view of the current state of the repository.
-
git push
: Sends local commits to a remote repository.- Example: To commit your changes to the main branch of the remote repository, use:
git push origin main
This will update the remote repository with the local changes.
-
git pull
: Updates the local repository with the changes from the remote branch.- Example: To synchronize the most recent changes to the main branch, use:
git pull origin main
This will download and merge the changes from the remote repository into your local branch.
-
git branch`: Manages branches of the project, allows to create, list or delete branches.
- Example: To create a new branch called new_functionality, use:
git branch nueva_funcionalidad
You can also list all existing branches with git branch without parameters.
-
git merge
: Merges changes from one branch with another, integrating modifications made by different developers.- Example: If you want to merge the new_functionality branch with the current branch, use:
git merge nueva_funcionalidad
This will integrate the changes from the new_functionality branch into the branch you are in.
To interactively learn and practice these commands, we recommend visiting Learn Git Branching, a platform that allows you to experiment with different commands in a safe and friendly environment.
With these basic concepts, you will be able to start using Git in your projects and benefit from its advantages in code management and effective collaboration.