Git important commands


What is Git?

Git is a distributed version control system (VCS) that helps developers track changes in their code, collaborate with others, and manage projects efficiently. It was created by Linus Torvalds in 2005 to handle the development of the Linux kernel.

With Git, multiple developers can work on a project simultaneously without overwriting each other’s work. It keeps a history of all changes, so you can easily revert to a previous version if something goes wrong.

Advantages of Git:

  1. Version Control – Keeps track of every change in the project.

  2. Collaboration – Multiple developers can work on the same project without conflicts.

  3. Branching & Merging – Create separate branches for features and merge them when ready.

  4. Speed & Performance – Git is fast, even for large projects.

  5. Distributed System – Every developer has a full copy of the repository.

  6. Security – Uses cryptographic hash functions to protect data.

  7. Open Source – Free to use and widely supported.


What is GitHub?

GitHub is a cloud-based platform that hosts Git repositories and provides tools for collaboration. While Git is the technology for version control, GitHub acts as a remote service where developers store their projects, share code, and contribute to open-source projects.

Why Do We Need GitHub?

While Git is a great tool for version control, it has limitations when it comes to collaboration, remote access, and project management. GitHub solves these problems by:

  1. Centralized Collaboration – Instead of manually sharing Git repositories, teams can work together in a single place online.

  2. Remote Storage & Backup – Your code is stored securely in the cloud, preventing data loss.

  3. Easy Contribution to Open Source – Developers worldwide can contribute to projects via pull requests.

  4. Issue Tracking & Documentation – Helps manage bugs, feature requests, and discussions.

  5. Automated Workflows (CI/CD) – Automates testing, deployment, and integrations with GitHub Actions.

  6. Access Control & Security – Teams can control who can view or modify the code.

GitHub is widely used by both individuals and enterprises to collaborate on software development projects, making it one of the most popular platforms for open-source and professional coding.

Stages of Git

  1. Working Directory – This is where you create and modify files. It contains your project files before they are added to version control.

  2. Staging Area – A temporary storage where changes are added before committing. This helps you organize your updates before saving them permanently.

  3. Git Repository (Local Repo) – The place where Git permanently tracks all changes. Once you commit changes, they are stored in the repository.

  4. Remote Repository – A version of your project stored on a cloud platform like GitHub, GitLab, or Bitbucket. It allows collaboration with others.


Types of Version Control Systems

  1. Centralized Version Control System (CVCS)

    • Uses a single central server where all changes are stored.

    • Developers pull files from the central server and push changes back.

    • Example: Subversion (SVN), Perforce.

    • Problem: If the central server crashes, all history may be lost.

  2. Distributed Version Control System (DVCS)

    • Every developer has a full copy of the repository, including all history.

    • Works offline and allows multiple developers to collaborate without depending on a central server.

    • Example: Git, Mercurial.

    • Advantage: No risk of losing history if the central server crashes.


Difference Between CVCS and DVCS

FeatureCVCS (Centralized)DVCS (Distributed)
Storage LocationCentral serverEach developer has a full copy
Offline WorkNot possiblePossible
SpeedSlowerFaster
Risk of Data LossHigh (if server fails)Low (backup copies exist)
ExamplesSVN, PerforceGit, Mercurial

How to Install Git

To install Git, follow the official installation guide:

🔗 Git Official Documentation

Installing Git on Ubuntu

  1. Open Terminal (Ctrl + Alt + T).

  2. Run the following command to update the package list:

     sudo apt update
    
  3. Install Git using:

     sudo apt install git -y
    
  4. Verify the installation:

     git --version
    

    This should display the installed Git version.

There is a small mistake in the command you provided. The correct syntax to set up your Git username and email globally is:

Set Git Username and Email on Ubuntu

  1. Open Terminal and run the following commands:

     git config --global user.name "YourUsername"
     git config --global user.email "yourgmailid@gmail.com"
    
  2. Verify the configuration to check if your details are set correctly:

     git config --global --list
    

    You should see an output like

    Steps to Create a GitHub Repository

    1. Log in to GitHub
      Go to GitHub and log in with your credentials. If you don't have an account, create one by clicking on "Sign Up" and following the steps.

    2. Create a New Repository
      After logging in, follow these steps to create a new repository:

      • Click the "+" button in the top right corner of the GitHub home page.

      • Select "New repository" from the dropdown.

    3. Configure the Repository

      • Repository Name: Name your repository. For example, git-tutorial.

      • Description: Add a brief description of your repository. For example: "A simple Git tutorial repository."

      • Initialize the Repository:

        • Check the box for "Add a README file".

        • You can choose a license if needed (optional).

    4. Create the Repository
      Once you've filled out the necessary information, click on "Create repository".

Clone the GitHub Repository to Your Local Machine (Ubuntu)

Now, let's clone the repository to your Ubuntu system:

  1. Copy the Clone URL
    On your GitHub repository page, you will see a green button called "Code". Click on it to reveal the repository's URL for cloning.

    For example, the URL might look like this:
    https://github.com/gauravhalnawar1011/git-tutorial.git

  2. Open Terminal on Ubuntu
    Press Ctrl + Alt + T to open the terminal.

  3. Clone the Repository
    Run the git clone command with the repository URL you copied from GitHub:

     git clone https://github.com/gauravhalnawar1011/git-tutorial.git
    
  4. Explanation of the URL
    The URL https://github.com/gauravhalnawar1011/git-tutorial.git has the following parts:

    • https://: This indicates the protocol for communication.

    • github.com: This is the domain where the repository is hosted.

    • gauravhalnawar1011: This is the GitHub username or organization name.

    • git-tutorial: This is the name of the repository.

    • .git: The .git suffix indicates it is a Git repository.

  5. Verify the Cloning
    After cloning, navigate to the directory where the repository is cloned:

     cd git-tutorial
    

    You can now list the files in the directory:

     ls
    

    You should see the README.md file that was created when you initialized the repository.


Working with Branches in Git

  1. What Are Branches in Git?
    A branch in Git is like a separate version of your project where you can make changes without affecting the main version (usually called main or master). You can create new branches to work on different features or fixes and then merge them back into the main branch when ready.

  2. Check the Current Branch
    To check which branch you're currently on, use the following command:

     git branch
    

    This will list all branches. The current branch will have an asterisk (*) next to it.

  3. Create a New Branch
    To create a new branch, use the following command:

     git branch <branch-name>
    

    For example, to create a branch called feature-hello-world, run:

  4.  git branch feature-hello-world
    
  5. Switch to a New Branch
    After creating a new branch, you need to switch to it to start working on it:

     git checkout <branch-name>
    

    To switch to the newly created branch feature-hello-world:

     git checkout feature-hello-world
    

    Alternatively, you can combine the branch creation and checkout commands with:

     git checkout -b <branch-name>
    

    This will both create and switch to the new branch in one step.


Create and Edit Your First File

  1. Create hello-world.txt Using vi
    Use the vi editor to create a new file called hello-world.txt and add some content to it:

     vi hello-world.txt
    

    Press i to enter insert mode, then type:

     Hello, World!
     This is my first file in Git.
    

    Press Esc to exit insert mode, and type :wq to save and quit.


Basic Git Commands for Tracking Changes

  1. Check the Current Branch
    As mentioned earlier, use this command to see which branch you're on:

     git branch
    

    You should see something like this:

     * feature-hello-world
       main
    

    The asterisk next to feature-hello-world shows you're currently on that branch.

  2. Add the New File
    To add hello-world.txt to Git (or any other file), use the following command:

    • To add only the specific file:

        git add hello-world.txt
      
    • To add all files (including new, modified, and deleted files):

    •   git add .
      
  3. Commit the Changes
    After adding the files, commit the changes with a message describing what you did. This message helps others understand the purpose of the changes.

     git commit -m "Added hello-world.txt for Git tutorial"
    

    It's good practice to write meaningful commit messages to make it clear what changes you made.

  4. Check Git Status
    Use git status to see the state of your repository. This will tell you if there are any uncommitted changes or files that need to be added.

     git status
    
  5. Push Changes to GitHub
    After committing your changes, it's time to push them to GitHub. Use the following command to push to the main branch:

     git push -u origin feature-hello-world
    
    • -u: This sets the upstream branch, so you don't need to specify origin main every time you push.

    • origin: This refers to your remote repository (GitHub).

    • feature-hello-world: This is the branch you're pushing to. If you're pushing to a different feature-hello-world, replace feature-hello-worldwith the branch name.


Recap of the Key Commands:

  1. Check Branch:

     git branch
    
  2. Create a Branch:

     git branch <branch-name>
    
  3. Switch Branch:

     git checkout <branch-name>
    
  4. Create and Edit File:

     vi hello-world.txt
    
  5. Add File:

     git add hello-world.txt
    

    Or to add all files:

     git add .
    
  6. Commit Changes:

     git commit -m "Message describing changes"
    
  7. Push to GitHub:

     git push -u origin main