DEV Community

Cover image for Git for Beginners: Basic Commands...
Andrés Ordaz
Andrés Ordaz

Posted on • Updated on

Git for Beginners: Basic Commands...

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:

  1. Go to the repository page on the platform you are using (GitHub, GitLab, Bitbucket, etc.).
  2. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (7)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Well done!

In case that anyone wants to learn more, here is a free ebook:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the eBook use one of the following links:

📘 Chapters

Collapse
 
tombeek profile image
Thomas Beek

Thank you very much for sharing this link. By the way, it doesn't mention it in the readme but GitHub can be very useful for managing all kinds of publishing projects also.

Collapse
 
codeveronica profile image
Luma

Thank you for sharing this content!

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

This is Good but beginner should know about

  • git status

  • git diff

  • git reset --hard HEAD^

  • git reset --soft HEAD^

  • git log --grep ="commit msg"

  • git log --no-merges

Collapse
 
menelion profile image
André Polykanine

A great post, but why don't you mention git switch and git restore? I use them extremely often personally.

Collapse
 
okayiy profile image
Okon Inyang

Thank you! I read books and listened to videos on git but yours is much easier to learn from.

Collapse
 
codeveronica profile image
Luma

Nice article!! I used to get stuck on git when I had conflicts.