Day9-Deep Dive in Git & GitHub for DevOps Engineers.

Day9-Deep Dive in Git & GitHub for DevOps Engineers.

What is Git and why is it important?

Git is a tool used in software development to manage changes in code. It keeps track of every modification made to a project's files, allowing multiple people to work on the same project simultaneously.

It's like having a safety net for your project, ensuring that no matter what changes are made, you can always return to a stable state. This makes collaboration smoother, helps in avoiding mistakes, and ensures the project progresses steadily.

In simple terms, Git is like a superhero for developers, saving the day by keeping their projects organized, safe, and easily accessible.

Why is Git Important?

  1. Version Control: Git allows developers to keep track of changes made to their code over time.

  2. Collaboration: Git enables multiple developers to work on the same project without interfering with each other's work.

  3. Backup and Recovery: With Git, all project files are stored in a repository, providing a backup in case of accidental deletion or loss. It also facilitates easy recovery of previous versions.

  4. Branching and Merging: Git allows developers to create separate branches to work. These branches can later be merged back into the main codebase.

What is difference Between Main Branch and Master Branch?

In Git, the terms "main branch" and "master branch" are default branch of Git repository. The default branch means it created automatically when the git repo isinitialize and It typically represents the primary line of development in a project.

Main Branch:

  • The "main" branch is the default branch in a Git repository.

  • All changes and updates to the project are typically made on this branch.

  • "Main" is considered a more inclusive and neutral term compared to "master."

Master Branch:

  • The "master" branch is the historical default branch name in Git.

  • It serves the same purpose as the "main" branch but may carry different connotations due to its historical usage.

    Can you explain the difference between Git and GitHub?

    Git:-

  • Git is a software and command-line tool.

  • Git is installed locally on the system and maintained by Linux.

  • Git is focused on version control and code sharing.

  • Git was first released in 2005.

  • Git has no user management feature.

  • Git is open source licensed.

  • Git competes with CVS, Azure DevOps Server, Subversion, Mercurial, etc.

    GitHub:-

  • GitHub is a service and graphical user interface.

  • GitHub is hosted on the web and maintained by Microsoft.

  • GitHub is focused on centralized source code hosting.

  • Git was first released in 2008.

  • GitHub has a built-in user management feature.

  • GitHub includes a free-tier and pay-for-use tier.

  • GitHub competes with GitLab, Bit Bucket, AWS Code Commit, etc.

    How do you create a new repository on GitHub?

  • Login to your GitHub account.

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

  • Enter your Repository name, add a description if you want, and click on the "Create repository" button.

    What is difference between local & remote repository?

  • A local repository is one that already exists or is already stored on your machine. It can be a repository that you already cloned before or was cloned outside of SourceTree (ex. using git commands on the command line). You don't clone local repositories, you just need to add their folders to the SourceTree.

  • A remote repository is one that exists somewhere else, in Github, Gitlab, Bitbucket, or in any other server that hosts Git repositories. This is the one you need to clone into your own machine, where it can now become a local copy of the remote repository.

    How to connect local to remote?

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

    1. Create a Remote Repository:

      If you haven't already created a remote repository on GitHub. You'll get a URL for your remote repository.

    2. Navigate to Your Local Repository: Open your command line interface (CLI) and navigate to the directory of your local Git repository using the cd command.

    3. Initialize Your Local Repository (if not already done): If your local repository is not yet initialized as a Git repository, use the following command:

       git init
      
    4. Link Your Local Repository to the Remote:

      Use the git remote add origin command to link your local repository to the remote repository. Replace <remote_URL> with the URL of your remote repository.

       git remote add origin <remote_URL>
      
    5. Verify the Connection: To verify that your local repository is now connected to the remote repository, you can use the git remote -v command.

       git remote -v
      
    6. Push Your Local Changes to the Remote: If you have any changes in your local repository that you want to push to the remote, you can do so using the git push command.

       git push origin main
      

Now, your local Git repository is connected to the remote repository, and you can push and pull changes between them as needed.

Task 1 - Set Your User Name and Email Address:

Configuring your user name and email address is a crucial step in Git, as it associates your commits with your identity. Follow these steps to set your user information:

  1. Open your terminal or Git Bash.

  2. Execute the following command to set your User name:

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

    Replace "Your Name" with your actual name.

  3. Execute the following command to set your email address:

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

    Replace "" with your actual email address.

Setting this information globally ensures that it is associated with all your commits across different repositories on your machine.

Task 2 - Create a Repository, Connect to Local Repository, create a file, Push and pull Commits:

Git bash tutorial clone pull push - singlelasopa

  1. Create a Repository Named "Devops" on GitHub:

    • Log in to your GitHub account.

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

    • Name your repository "Devops."

    • Add an optional description if desired.

    • Click "Create repository."

  2. Connect Your Local Repository to the GitHub Repository:

    After creating the GitHub repository, you need to establish a connection between your local repository and the newly created GitHub repository. In your terminal, navigate to the local repository directory and execute the following commands:

      git remote add origin https://github.com/your-username/Devops.git
    

    Replace "your-username" with your GitHub username. This command adds a remote named "origin," pointing to your GitHub repository.

  3. Create Directory: If the directory doesn't exist, you'll need to create it before creating the file. You can create the directory using the mkdir command.

     mkdir -p Devops/Git
    
  4. Create a New File in "Devops/Git/Day-02.txt" and Add Content:

    open your terminal and execute the following command to create a new file.

       touch Devops/Git/Day-02.txt
    

    This command creates a new text file named "Day-02.txt" in the specified directory.

    Add some content to the file using your preferred text editor.

  5. Push Your Local Commits to the GitHub Repository:

    Once the file is created and content is added, you need to commit and push your changes to GitHub.

    In your terminal, execute the following commands:

       git add .
       git commit -m "Add Day-02.txt with content"
       git push origin main
    
    • git add .: Stages all changes for commit.

    • git commit -m "Add Day-02.txt with content": Commits the changes with a descriptive message.

    • git push origin main: Pushes the changes to the 'main' branch on your GitHub repository.

  6. Pull Changes from the GitHub Repository:

    • If there are changes made by others on the GitHub repository, you might want to pull those changes to keep your local repository up to date.

    • In your terminal, navigate to the local repository directory and execute the following command:

          git pull origin main
      

      This command fetches changes from the 'main' branch on the GitHub repository and merges them into your local branch.

      This is the #Day09 of the #90DaysofDevOps challenge! Hope you found this article informative and useful so please share it with others who might benefit from it.

      Thanks for reading this article.

      Keep Learning...