๐Ÿ…ณ๐Ÿ…ฐ๐Ÿ†ˆ 9:Mastering Git and GitHub: The Ultimate Guide for DevOps Engineers

๐Ÿ…ณ๐Ÿ…ฐ๐Ÿ†ˆ 9:Mastering Git and GitHub: The Ultimate Guide for DevOps Engineers

ยท

10 min read

Before this article we covered the Basics of Git and GitHub there we learned how what are Git and Github, and The difference between them we also looked at the centralized and decentralized systems after that we looked at how to remotely connect terminals to the local system.

In this let's see some more terminologies about git and GitHub.

What is Git and why it is important?

Git is like a time machine for computer files. It helps people work together on projects, like writing code or creating documents, without messing things up.

Here's why it's important:

  1. Version Control: Git keeps track of changes you make to your files. If you make a mistake, you can easily go back in time to a working version. This prevents you from losing your work or making irreversible errors.

  2. Collaboration: Many people can work on the same project using Git. They can make changes, and Git helps merge those changes, so everyone's work can fit together like pieces of a puzzle.

  3. Backup: Git stores your work on different computers, which means if your computer crashes, you won't lose everything. It's like having a backup of your work.

  4. Open Source: Lots of open-source software and projects use Git. This makes it easy for people all over the world to contribute to and improve software.

So, in simple terms, Git is a tool that helps you keep your work safe, work with others, and easily go back in time to fix mistakes. It's a must-have for many developers and even non-developers working on projects.

What is the difference between "Main Branch" & "Master Branch"?

In Git, both "main" and "master" are used to refer to the default branch of a project. They essentially mean the same thing and serve the same purpose. However, there's a subtle difference:

  1. "master" branch: This term was commonly used in the past as the default branch name. It's still widely used but has been replaced by "main" in many places to avoid any association with the historical use of "master" in a non-inclusive context.

  2. "main" branch: This is a newer and more inclusive term that is being used to replace "master" in many Git repositories. It's recommended as a more neutral and respectful choice, especially in open-source and diverse communities.

In simple terms, the only difference is the name. You can choose to use either "main" or "master" as the default branch name in your Git projects, and they work in the same way. "Main" is a more modern and considerate choice, but both are the same and valid.

Difference between Git and GitHub?

GitGithub
Git is a tool or software that helps you manage and track changes to your computer files, like code, documents, or any digital content.GitHub is a web platform that uses Git under the hood. It's like a hub for sharing and collaborating on Git repositories (collections of files and their history).
It works on your local computer, allowing you to keep a history of changes, create different versions of your work, and collaborate with others.It allows you to store your Git repositories on the internet so that others can access and contribute to your projects.
Think of Git as a personal "time machine" for your files, helping you keep track of changes and manage different versions of your work.GitHub provides tools for team collaboration, bug tracking, project management, and more.

GitHub is widely used in the software development world to host open-source projects and manage code together with others.

how to create a new repository in GitHub?

All this has also been covered in the Day 8 article. you can check it out here

Creating a new repository on GitHub is easy. Here are the steps in simple language with examples:

  1. Sign in to GitHub: Go to the GitHub website (GitHub) and sign in to your GitHub account. If you don't have an account, you can create one for free.

  2. Go to your GitHub profile: After signing in, click on your profile icon in the top right corner, and select "Your profile."

  3. Create a new repository:

    • On your profile page, click the "Repositories" tab.

    • Then, click the green "New" button on the right-hand side.

  4. Fill out the repository details:

    • Give your repository a name: This is the name of your project. For example, "my-first-repo."

    • Choose visibility: You can make your repository public (visible to everyone) or private (only visible to people you invite).

    • Initialize with a README file: If you check this option, GitHub will create a README file for your repository. It's a good idea to check this to add a description to your project.

    • You can also add a .gitignore file and a license if your project needs them, but it's not required for creating a repository.

  5. Click "Create repository": After filling out the details, click the green button at the bottom to create your new repository.

That's it! You've successfully created a new repository on GitHub. Now, you can start adding your project files, make commits, and collaborate with others on your code. You'll find instructions on how to connect your local Git repository with this GitHub repository on the GitHub page after you create it.

What is the difference between local & remote repositories?

In simple language, the difference between a local repository and a remote repository in GitHub is:

  1. Local Repository:

    • A local repository is a copy of your project that exists on your computer.

    • It's where you work on and make changes to your project's files.

    • You use Git on your computer to track changes, create versions, and commit your work to the local repository.

    • Your local repository is not accessible by others unless you explicitly share it.

  2. Remote Repository (on GitHub):

    • A remote repository is like a central hub on the internet, such as GitHub.

    • It's where you can store and share your project with others. Many people can access it.

    • You can upload or "push" your local repository to the remote repository on GitHub, making your project visible and accessible to the world.

    • It's a place for collaboration, where multiple people can work together on the same project and contribute their changes.

In summary, your local repository is where you do your work and keep your project on your computer. The remote repository on GitHub is a place to store, share, and collaborate on your project with others over the internet. These two work together in Git-based workflows, allowing you to both work on your project privately and share it with a wider audience when ready.

How to connect the local repo to the remote repo?

If you have gone through the Day 8 article carefully. then you'll have no problem connecting to the local repository remotely.

you can check the detailed article here

To connect your local Git repository to a remote repository on GitHub, follow these steps:

  1. Create a New Repository on GitHub:

    • Go to GitHub and log in to your account.

    • Click on the "+" sign in the upper right corner and select "New repository."

    • Follow the prompts to set up a new repository, giving it a name and optional descriptions. You can also choose to make it public or private.

  2. Initialize a Local Git Repository:

    • Open your terminal or command prompt.

    • Navigate to your local project folder using the cd command.

  3. Connect Your Local Repository to the Remote Repository:

    • In your terminal, use the following Git command to link your local repository to the remote one on GitHub, replacing <repository_url> with the URL of your GitHub repository (you can find this URL on your GitHub repository's page):
git remote add origin (Token) https://github.com/Mliaqatpk/Devops.git
  1. Push Your Local Repository to GitHub:
  • After connecting your local repository to the remote one, you can push your local changes to GitHub using the following command:
git push -u origin main

The u flag is used to set the upstream branch, so in the future, you can simply use git push without specifying the remote and branch.

That's it! Your local repository is now connected to a remote repository on GitHub. You can continue working on your project, make changes, and use Git to push those changes to GitHub for backup and collaboration with others.

if these commands are not working then use the alternate techniques I have written below in the tasks section.

Tasks for Today:

Set your user name and email address, which will be associated with your commits.

  • To set the username use the git config command
git config --global user.name "git_username"
  • To set the user email id in Github user the git config user email command
git conig --global user.email "whatever@gmail.com"

Create a repository named DevOps on GitHub

All the steps have been mentioned above:

I have already created a repository named "DevOps" as you can see below

How to connect to the repository locally?

  • On the top right section click on your "Profile" image and then click on settings

  • Scroll down and go to the "Developer settings" and click on it

  • click on the drop-down menu of the "Personal access token" and then click on the Tokens (classic).

  • click on generate new tokens

  • click on the second option i.e generate new token (classic)

  • give any name you want in the note and select the expiration date to whatever you want

  • tick all the boxes to get all the access rights on your repo and click on "Generate Token"

  • I will not reveal my key here but you should copy the key somewhere safe because the key will not be visible afterwards.

  • after that to connect the repo.

  • open the terminal and use the cd command to navigate to the repo you made

  • make some small changes like I did in the picture above.

  • use the git push command to connect to the local repo and remote repo

COPY

COPY

git push

enter your username
  • then in the place of the password "paste the copied token" and you're done

Now you're repo has been connected

Create a few files in the DevOps folder and add some content

How to push commits to the repository in GitHub?

  • I have already added some content in the folder let's move to the next step

This is the file I have created. You can add anything you want to

after this perform the following commands

Git init
  • Git init is used to initialize git in the repository
  • After that use the git status command to check what changes you have made

        git status
    

    if you see files in red it means that you still have files you have yet to commit.

  • Now use the git add to add the files to the stash

        git add *
    

    Normally you can use the filename instead of "*" but here you can use the "*" flag to add everything to the stash

    Now everything has been added. Now use the gut status command again to check if everything has been added properly.

        git status
    

    Now you can see that the files have been turned to Green color which means the files have been added

  • The next step is to commit the files to GitHub with a proper message

  • use the git commit command to commit the files to the git

        git commit -m
    

    you can now see that the commits have been added using the "-m" flag to commit with the proper message

  • next step is you push the committed changes to the GitHub repo using the git push command

  •     git push origin main
    

    use this command to make the changes in the remote repo

    Now you have successfully made the changes to the remote repo.

ย