Advance Git & GitHub

Advance Git & GitHub

·

8 min read

What is Git and Why It's Important⁉️

  • Git is a version control system that allows you to track file changes and collaborate on files with numerous people. It is most typically used in software development, but it may be used to track changes to any collection of files.

Let's understand why It is important:

  • Better Teamwork: With Git, each team member has their own copy of the project and may work on it independently. It's like having your own personal playground to experiment with ideas without affecting others. When you're ready to demonstrate your work to the team, you may effortlessly share your edits with them.

  • Avoiding Mistakes: Think of Git as a time machine for your project. If you make a mistake, you may quickly go back in time to when everything was working correctly. No more worrying about making mistakes!

  • Keeping Things Organised: Git is similar to a virtual notebook in that it preserves a complete history of all modifications made to the project. It's like having a magical book that tells you who did what, when they did it, and why. It assists you in comprehending the project's journey and maintains everything organised.

  • Working Anywhere: With Git, you can work on your project even when you're not connected to the internet. You can keep making changes and saving them in your own space, and when you're back online, you can share your work with others.

  • Combining Everyone's Work: Git acts as a superpower that enables your team to easily combine everyone's edits. It's similar to blending various components to make a great cake while making sure every component blends seamlessly.

What is the difference between main branch and master branch⁉️

  • Assume you're constructing a lovely garden with your friends, and you're using a magical blueprint to guide your efforts. This blueprint functions as a version control system, allowing you to keep track of all changes and progress.

    You will be working on two main sections in this garden project: the flower garden and the vegetable garden. You want to keep things organised, so you divide your blueprint into two sections: one for the flower garden and one for the vegetable garden.

  • Main branch:

    • The main branch is similar to a central section of your blueprint, but it focuses on a specific aspect of the project. In our case, it is solely for the flower garden. It includes all of the information and updates about the flowers, their colours, and how they will be arranged.

    • In our garden project, for example, the main branch represents the plan for the flower garden. It includes information about the different types of flowers, where they are placed, and any changes or improvements made to that section.

  • Master branch :

    • The master branch is similar to the main section of your blueprint, where you put all of the important and final project information. It functions as a central hub, containing the most recent and polished version of your garden plan.

    • In our garden project, for example, the master branch represents the overall plan for the entire garden. It contains important details such as the layout, design, and main elements of both the flower and vegetable gardens.

  • Summary :

    • The master branch holds the entire project's final version, including both the flower and vegetable garden plans.

    • The main branch is like a subset of the master branch, focusing only on the flower garden part of the project.

    • The main branch and the master branch function as distinct sections of your magical blueprint. The master branch contains the overall project plan, whereas the main branch focuses on a specific component of the project. You can easily keep your garden project organised and create a beautiful masterpiece by using both branches!

    • When everything is in order, merge the main branch back into the master branch so that the final plan includes both the flower and vegetable gardens.

What is the difference between Git and GitHub🤔⁉️

  • Git is a tool that allows developers to keep track of their code changes, whereas GitHub is an online platform that allows them to store and share their code notebooks with others. They make coding a fun and collaborative adventure when they work together!

What is the difference between local Repository and Remote Repository⁉️

Let's understand the difference local and remote repository:

  • Local Repository :

    • A local repository is similar to your own private stash of files and folders on your computer. It houses all of your important work and changes. Consider it a magical treasure chest in which you can save and organise everything related to your project.

    • Assume you're working on a story and have a special notebook on your desk. You save anything new you write or changes you make to the story in this notebook. That notebook is your local repository, where you keep all of your creative work safe and secure.

  • Remote Repository:

    • A remote repository is similar to fantastic cloud storage where you can collaborate with friends and share your work. It works like a special library where your team members can collaborate on the same project while also seeing what you've accomplished.

    • Consider a sizable library where many people can add and borrow books. Your remote repository resembles one of those library books. You place a copy of your notebook in the library (the remote repository) when you're ready to share your story with others or solicit their assistance. Your story is now available for all of your friends to see, comment on, and even adopt.

  • Summary :

    Your local repository is your private workspace where you do all your work, while the remote repository is like a shared library where you can showcase your work, collaborate with others, and make magic together

How to connect local repository to Remote repository⁉️

  • From Remote : This happens with the help of the Git Fork command when the repository is already available on GitHub. User makes a git fork or git clone to the same repository at local.

  • From local : This happens with the help of the Git Remote command when the repository is first created on local. And then the user connects it to the remote repository

  • We can use " git remote " command to connect a Git local repository with GitHub remote repository :

      # to connect to the remote repository
      git remote add origin "url of repository on github"
      git remote add origin https://github.com/srahul0502/practice.git
    
      # push the repository from local repo to remote repo
      git push -u origin master
    
    • Even if you clone the repository from github and use git clone in your system

      you will get connected to the remote repository

        git clone https://github.com/srahul0502/practice.git
        # it gets connected to the remote repo as we have cloned from there
        # we just have config username and email to push the changes to origin
        # to check if your are connected to any remote repository or not
        git remote -v
      

How to set your user name and email address in the config file ⁉️

To set your username and email address associated with your github account we use :

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

  • After doing these you can easily commit the changes and push them to the remote repository

Password Authentication 🤔⁉️

  • You might get error while pushing the changes to the remote repository , it will as you for username and password associated with github .

  • I know it is difficult to enter your credentials for each push you make and even If you get error after getting credentials you can follow the steps below to solve or make it easy :

    • Go to GitHub settings > Developer settings(scroll down you will find it in left bottom) > Personal access tokens > Tokens Classic

      • Click on Generate new token and then go to Generate new token (classic).

      • Give the name you want to and select the number of days you want the token to be active and click on Repo check box , scroll down and generate token.

      • Copy the token and go to cmd

          git remote set-url origin https://token-key@github url
          # example 
          git remote set-url origin https://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@github.com/srahul0502/practice.git
        
      • Now , you can easily push the changes to the remote repo without giving any credentials for the days you have created the token.

Hands-on😁

  • Create a repository named "Devops" on GitHub

  • Connect your local repository to the repository on GitHub.

  • Create a new file in Devops/Git/Day-02.txt & add some content to it

  • Push your local commits to the repository on GitHub

Let's create a repository as we did in our previous blog , https://srdev.hashnode.dev/basics-of-git-github (if you didn't go through, please go through it once)

  • After naming the repo click on check box to add README file and create repo

Now , Clone this repo to your local system using git clone :

git clone https://github.com/srahul0502/Devops.git
# go to the repo by using cd
cd Devops

Now , create folder "Git" using mkdir and use nano filename to create and edit the file

After creating and modifying the content of the file , go back to the Devops folder using " cd.. " to add all the contents present in Devops to Staging area using " git add " , git add . (adds all the content presend to staging area) and use git status to check status.

Now commit your changes with a commit message :

git commit -m "Folder and file added "

Now push them to the remote repository using "git push"

git push origin main

Now got Github and refresh your page and tadaa🥳

I hope you all understood the concept

Stay tuned for more updates

Happy Learning :D