Git Tutorial for Beginners: Step by Step Guide

Git Tutorial for Beginners: Step by Step Guide

Git is a very essential tool for a developer. Whether you're just starting your coding journey or looking to adopt Git for your projects, this beginner's guide will help you get started with the fundamentals of Git.

What is Git?

Git is a version control system that tracks changes in your code. It helps you collaborate with others, keep a history of your work, and manage different versions of your software projects. It's like having a superpower for managing your software projects!

Basic Concepts or Terminologies

  1. Repository : It is storage space where we can store the project code, or we can a space where project lives. Platforms that are used to store : GitHub, GitLab. It can contain all the file of your project , all the changes made to file can also be tracked.

  2. Commit : Commit is like a checkpoint, every time you make any change to your project (adding new lines of code , adding new page, editing existing one), you create a commit to remember that changes.

  3. Branch : Branches are like path , when you are working on project , you can different path to work on different ideas and features or fix issues without affecting the main path or branch.

  4. Merge : Merge is a process of combining different changes from one branches to another, Git automatically merges changes if it can do so cleanly.

  5. Pull/Pull Request : In Git, pull refers to fetching changes remote changes and merging them into your local repository.

  6. Push : Pushing refers to sending your commited changes to remote repository.

  7. Clone : Cloning is a process of making a local copy of remote repository. You can work on local copy without affecting the main codebase.

Setting up the Git

Installing Git based on Operating System :

  • Git is available for Windows, Linux, Macos.

  • On Windows, you can download the installer from the Git website and follow the installation wizard.

  • On Linux, you can install Git using your distro package manager.

  • On MacOs, Git comes pre-installed with Xcode Command Line Tools.

Configuring Git

  • After installing Git , you need to configure the Git with your username and email.

  • You can use command git config

  •   git config --global user.name "Your Name"
      git config --global user.email "your.email@example.com"
    

Few Basic Command to work with Git

  1. git add

    • This command adds changes from the working directory to the staging area.

    • It allows you to select which changes you want to include in the next commit.

    •   git add file1.txt        # Add changes in file1.txt to the staging area
        git add .                # Add all changes in the working directory to the staging area
      
  2. git commit

    • This command records the changes from the staging area into the repository's history.

    • It creates a new commit with a unique identifier (hash) and includes a commit message to describe the changes.

    •   git commit -m "Add new feature"     # Commit changes with a descriptive message
      
  3. git push

    • This command pushes local commits to a remote repository.

    • It's used to share your changes with others or back up your work on a remote server.

    •   git push origin master      # Push commits from the local 'master' branch to the remote repository
      
  4. git pull

    • This command fetches changes from a remote repository and merges them into the current branch.

    • It's used to sync your local repository with the latest changes from the remote repository.

    •   git pull origin master      # Fetch and merge changes from the remote 'master' branch into the local branch
      
  5. git status

    • This command shows the current status of the working directory and staging area.

    • It displays which files have been modified, added to the staging area, or are untracked.

    •   git status                  # Check the status of the working directory and staging area
      
  6. git log

    • This command displays the commit history of the repository.

    • It shows a list of commits, including the commit hash, author, date, and commit message.

    •   git log                     # View the commit history of the repository
      

These are the basic Git commands to form the foundation of version control with Git.

In this blog, we have covered only the basics to help you get started with Git. I will be writing a few more blogs on Git to explain advanced concepts. Happy Learning!

Did you find this article valuable?

Support Priyanshu Raj by becoming a sponsor. Any amount is appreciated!