Git Tutorial for Beginners

Photo by Farhat Altaf on Unsplash

Welcome to our Git tutorial for beginners! Git is a powerful version control system that helps developers track changes in their code and collaborate with others. If you’re new to Git, don’t worry! This tutorial will guide you through the basics, step-by-step, using simple language and practical examples.

What is Git?

Git is a version control system (VCS) that allows you to keep track of changes in your files and coordinate work on those files among multiple people.

Imagine you’re working on a project and you accidentally delete a crucial file or make changes that you later regret. With Git, you can easily revert to previous versions of your project, making it a lifesaver for developers.

Why Use Git?

There are several reasons why developers use Git:

  • Version Control: Git keeps a record of every change made to your files. This means you can go back to any previous version if something goes wrong.
  • Collaboration: Multiple people can work on the same project simultaneously. Git helps manage changes from different contributors, merging them seamlessly.
  • Backup: Git acts as a backup system. Your project is stored both locally and on a remote server (like GitHub), so you don’t lose your work.

Getting Started with Git

Before you can start using Git, you need to install it on your computer. You can download Git from HERE. Follow the installation instructions for your operating system.

Once installed, you can use Git from your command line (Terminal on macOS/Linux or Command Prompt/PowerShell on Windows).

Basic Git Commands

Let’s dive into some basic Git commands. Open your terminal and follow along!

Setting Up Git

The first step is to configure Git with your name and email. This information will be used in your commits.

git config --global user.name "Your Name"

git config --global user.email "youremail@example.com"

Creating a Repository

A Git repository is where your project lives. To create a new repository, navigate to your project folder and initialize Git.

cd your-project-folder

git init

This command creates a hidden .git directory in your project folder, which Git uses to track changes.

You may like: OOP in python

Staging and Committing Changes

When you make changes to your files, Git doesn’t automatically track them. You need to tell Git which changes you want to include in your next “snapshot” (commit).

Staging Changes

To stage changes, use the git add command. This command adds your changes to the staging area.

This is all shell-based. If you want to see what files you changed and their exact changes, you can also use git-gui.

git add filename

You can also stage all changes at once:

git add .

Committing Changes

Once your changes are staged, you need to commit them. A commit is like taking a snapshot of your project at a specific point in time.

If you are a gamer, you can relate to what a checkpoint is. In case we want to go back to the previous state of the game, we can start from the previous checkpoint.

git commit -m "Your commit message"

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

Viewing Commit History

To see a log of all your commits, use the git log command.

git log

This command shows a list of commits, including the commit hash, author, date, and commit message.

Creating Branches

Branches allow you to work on different features or fixes simultaneously without affecting the main codebase. To create a new branch, use the git branch command.

git branch new-branch

To switch to the new branch, use the git checkout command.

git checkout new-branch

You can also create and switch to a new branch in one command:

git checkout -b new-branch

Merging Branches

Once you’ve finished working on a branch, you may want to merge your changes back into the main branch. First, switch to the main branch:

git checkout main

Then, merge the changes from your feature branch:

git merge new-branch

Pushing to a Remote Repository

A remote repository is a version of your project that is hosted on the internet or another network. GitHub is a popular platform for hosting Git repositories. To push your local changes to a remote repository, you need to add the remote URL and push your changes.

Adding a Remote

git remote add origin https://github.com/yourusername/your-repo.git

Pushing Changes

git push -u origin main

The -u flag sets the remote origin as the default for the main branch, so you only need to use git push in the future.

Recommended Article: SOLID Principles in Python

GitHub Basics

GitHub is a web-based platform for hosting Git repositories. It provides a visual interface for managing your repositories and collaborating with others. Here are some common GitHub actions:

Creating a Repository on GitHub

Go to GitHub and sign in. Click the “New” button to create a new repository. Give your repository a name and description, and click “Create repository“.

Cloning a Repository

To clone a repository means to create a local copy of it on your computer. Use the git clone command followed by the repository URL.

git clone https://github.com/yourusername/your-repo.git

Pull Requests

A pull request is a way to propose changes to a repository. When you create a pull request, you’re asking the repository owner to review and merge your changes.

  • Fork the Repository: If you don’t have write access to the repository, you need to fork it first. This creates a copy of the repository under your GitHub account.
  • Clone Your Fork: Clone your forked repository to your local machine.
  • Create a Branch: Create a new branch for your changes.
  • Make Changes and Commit: Make your changes and commit them.
  • Push Your Branch: Push your branch to your forked repository.
git push origin new-branch
  • Create a Pull Request: Go to the original repository on GitHub and click the “New pull request” button. Select your branch and create the pull request.

Conclusion

Git is an essential tool for modern software development. By mastering the basics, you can manage your code more effectively and collaborate with others seamlessly. In this tutorial, we covered the foundational Git commands and concepts, from setting up Git to creating branches and pushing to a remote repository. Practice these commands and explore GitHub to become more comfortable with version control. Happy coding!

Software Engineer | Website | + posts

Talha is a seasoned Software Engineer with a passion for exploring the ever-evolving world of technology. With a strong foundation in Python and expertise in web development, web scraping, and machine learning, he loves to unravel the intricacies of the digital landscape. Talha loves to write content on this platform for sharing insights, tutorials, and updates on coding, development, and the latest tech trends

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *