This content originally appeared on DEV Community and was authored by Andrés Ordaz
If you are starting in the world of programming or have just started your first job as a developer, you have probably already heard about Git. This powerful version control tool is essential for managing your code and collaborating with other developers. But if you still don’t fully understand how it works or why it’s so important, don’t worry! We’ve all been there.
In this article, I will guide you through the basic Git commands you need to master. Imagine you have just joined a development team. You are assigned your first project and told to clone the repository, create a branch for your work, and finally, synchronize your changes with the team. It may seem overwhelming, but by the end of this article, you will have a clear understanding of these processes and be ready to use them in your daily work.
Cloning a Repository
The first step to working with an existing project is to clone the repository to your local machine. To do this, follow these steps:
- Go to the repository page on the platform you are using (GitHub, GitLab, Bitbucket, etc.).
- Copy the repository URL (there is usually a button that says “Clone” or “Clone with HTTPS“).
Command: git clone
Example:
git clone https://github.com/user/repo.git
Explanation:
This command clones the specified repository to your local machine, creating an exact copy of the project.
Entering the Cloned Repository
After cloning the repository, you need to navigate to the project directory.
Command: cd
Example:
cd repo
Explanation:
Switch to the cloned repository directory to start working on it.
Creating and Switching Branches
Branches allow you to work on different features or fix bugs without affecting the main code. To create a new branch and switch to it, use the following commands:
Command: git branch and git checkout
Example:
git branch new-branch
git checkout new-branch
Explanation:
git branch new-branch
creates a new branch called “new-branch”, and git checkout new-branch
switches your working environment to that branch.
Adding Changes to the Index
After making changes to your files, you need to add these changes to the index (staging area) before committing.
Command: git add
Example:
git add file.txt
Explanation:
This command adds file.txt to the index, preparing it for the commit.
Making a Commit
Once you have added the changes to the index, you need to confirm these changes by creating a commit.
Command: git commit
Example:
git commit -m "Commit message"
Explanation:
This command creates a new commit with a descriptive message of the changes made.
Updating the Branch (Pull)
Before pushing your changes to the remote repository, it is good practice to ensure that your local branch is updated with the changes from the remote repository.
Command: git pull
Example:
git pull origin main
Explanation:
This command brings the changes from the remote repository to your local branch, ensuring you are working with the most recent version of the code.
Synchronizing with the Remote Repository (Push)
After making a commit and updating your branch, you can push your changes to the remote repository.
Command: git push
Example:
git push origin new-branch
Explanation:
This command pushes the commits from your local branch to the remote repository, updating the corresponding remote branch.
Checking the Repository Status
To check the current status of your repository, including unadded changes and pending commits, use the following command:
Command: git status
Example:
git status
Explanation:
This command shows the status of the files in the working directory and the index, indicating which changes have been made and which are pending confirmation.
Viewing the Commit History
You can review the commit history to see all the changes made to the project.
Command: git log
Example:
git log
Explanation:
This command shows the commit history with details such as the author, date, and commit message.
Undoing Changes
If you need to undo changes in your working area, you can use the following command:
Command: git checkout
Example:
git checkout -- file.txt
Explanation:
This command restores file.txt to the last committed version, undoing uncommitted changes.
Removing Changes from the Index
To remove files from the staging area, use the git reset command.
Command: git reset
Example:
git reset file.txt
Explanation:
This command removes file.txt from the index but keeps the changes in the working area.
In summary, we have covered the basic Git commands that will allow you to manage your code versions and collaborate on projects efficiently. Practicing these commands on personal projects will help you reinforce your understanding and become familiar with their use. Keep practicing and exploring Git’s capabilities!
This content originally appeared on DEV Community and was authored by Andrés Ordaz