DEV Community

Cover image for Learn How to Use Git in 30 Minutes: A Beginner’s Guide
Biz Maven
Biz Maven

Posted on

Learn How to Use Git in 30 Minutes: A Beginner’s Guide

Git is an essential tool for developers, especially when it comes to managing code versions and collaborating with others. If you're new to Git, this guide will help you get started with the basics in just 30 minutes. We'll cover the fundamentals of Git, including tracking changes, branching, resolving conflicts, and collaboration workflows.

What is Git?

Git is a distributed version control system that lets you track changes in your code, collaborate with others, and manage different versions of your project. It’s like a time machine for your code, allowing you to go back to previous versions if something goes wrong.

Getting Started with Git

Before diving into Git commands, you’ll need to install Git on your computer. You can download it from git-scm.com. Once installed, you can verify the installation by opening a terminal or command prompt and typing:

git --version
Enter fullscreen mode Exit fullscreen mode

This should display the version of Git installed, confirming that it’s ready to use.

Setting Up a Git Repository

A Git repository is where your project's files and history are stored. To create a new repository, navigate to your project folder in the terminal and run:

git init
Enter fullscreen mode Exit fullscreen mode

This command initializes a new Git repository, creating a hidden .git folder that tracks your project's history.

Tracking Changes

One of Git’s primary functions is tracking changes in your code. Here’s how to get started:

Adding Files to the Staging Area

Before you can save changes to your project’s history, you need to add the files you want to track to the staging area. Use the following command:

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

To add all files at once, use:

git add .
Enter fullscreen mode Exit fullscreen mode

Committing Changes

After staging the files, you need to commit them, which means saving a snapshot of your project at that point in time. Use:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

The commit message should be a brief description of the changes made.

Branching in Git

Branches allow you to work on different features or fixes without affecting the main codebase. Here’s how to manage branches:

Creating a Branch

To create a new branch, use:

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Switching Branches

To switch to another branch, use:

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Merging Branches

When you're ready to merge changes from one branch into another, first switch to the branch you want to merge into, then use:

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

This command combines the changes from the specified branch into your current branch.

Resolving Conflicts

Merge conflicts happen when changes in different branches contradict each other. Git will alert you when this occurs. Here’s how to resolve them:

  1. Identify the Conflict: Git will highlight the conflicting sections in your code.
  2. Resolve the Conflict: Manually edit the conflicting code to decide which changes to keep.
  3. Stage the Resolved Files: After resolving the conflict, add the files back to the staging area using git add.
  4. Commit the Changes: Finally, commit the resolved changes with a message explaining how the conflict was resolved.

Collaboration Workflows

Git shines in collaborative environments. Here's how to work with others using Git:

Cloning a Repository

If you're contributing to an existing project, start by cloning the repository to your local machine:

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Pushing Changes

After making changes, push your commits to the remote repository so others can see your work:

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Pulling Changes

Before starting new work, it’s good practice to pull the latest changes from the remote repository to keep your local copy up-to-date:

git pull
Enter fullscreen mode Exit fullscreen mode

This command fetches and merges changes from the remote repository into your current branch.

Conclusion

In just 30 minutes, you’ve learned the basics of Git—how to track changes, work with branches, resolve conflicts, and collaborate with others. Git may seem complex at first, but with practice, you'll find that it greatly enhances your ability to manage code and collaborate effectively. Keep practicing these basic commands, and soon you'll be ready to explore more advanced Git features. Happy coding!


For More Latest Articles! Visit: InsightLoop.blog

Top comments (0)