Deep Dive in Git & GitHub for DevOps Engineers
In today’s fast-paced DevOps environment, mastering version control is crucial for seamless collaboration, automation, and continuous integration. At the heart of version control lies Git, an open-source system that revolutionized how teams manage and track code. Combined with GitHub, a platform built around Git, developers and operations engineers can streamline workflows, manage repositories, and foster team collaboration more efficiently.
This blog aims to equip you with a fundamental understanding of these essential tools.
What is Git and why is it important?
First understand Git:
Git is a Distributed Version Control System (DVCS) that allows software developer to push their code to a remote repository. Allows to track their code, collabrate on variety of projects and can manage the commit history.
Importance of Git:
DVCS: In a distributed version control system each user has a complete local copy of a repository on his/her individual computer.
Version Tracking: Keeps the track on every modification on the code, ensuring a complete history on project.
Branching: Git branching strategy allows developers create multiple branch to work on multiple parts of code. Changes are personal to that particular branch without affecting the main code.
Collaboration: Multiple developers can work on the same project, track changes, and merge code seamlessly.
Simple Hand made diagram:
What is the difference between Main Branch and Master Branch?
Git uses default brach name as “Master”. In today’s modern scenario community started calling it as “Main” for inclusivity. The difference is purely naming, and users can decide to name their branches as they wish.
Key Highlights:
Master: The default branch in Git local repositories.
Main: The modern default branch used by many new repositories, especially on GitHub.
Simple Hand made diagram:
The difference between Git and GitHub?
Git:
Git is a distributed version control system (VCS) that allows developers to track changes in their codebase over time. It helps manage source code history, making it easier to collaborate on projects and revert to previous versions when necessary.
Local Repositories: Developers can work on their code offline since Git stores a full history of the codebase locally.
GitHub:
GitHub is a web-based platform built on top of Git that provides a user-friendly interface for hosting Git repositories in the cloud. It’s designed for collaboration, making it easier for teams to work together, track issues, and review code. GitHub adds features like project management, social networking, and integration with other tools to facilitate smoother workflows in software development.
Cloud Hosting: It hosts Git repositories online, allowing easy sharing and collaboration among remote teams.
In Summary:
Git is the tool that developers use to track and manage code changes locally.
GitHub is a platform for sharing, collaborating, and managing Git repositories in a centralized, cloud-based environment.
How Do You Create a New Repository on GitHub?
Here are the steps to create a new repository on GitHub:
Log in to your GitHub account.
Click the New button or navigate to Repositories > New.
Name your repository, for example, "DevOps."
Choose the visibility (public or private).
(Optional) Add a README,
.gitignore
, or license file.Click Create Repository.
Now, you have a repository where you can push your local Git repository code.
What is the Difference Between a Local and Remote Repository? How to Connect Local to Remote?
Local and remote repositories are key concepts in Git, representing where code is stored and how it is shared or accessed. Here’s the difference:
1. Local Repository:
A local repository is stored on your own machine. It contains your project files, commit history, branches, and tags.
You can work offline, create commits, and manage branches within the local repository.
Local Git Operations:
git add
,git commit
,git log
, etc., are performed within the local repository without needing an internet connection.
2. Remote Repository:
A remote repository is hosted on a server (e.g., GitHub, GitLab, or Bitbucket), allowing multiple collaborators to access, pull, and push code from anywhere.
It’s usually a centralized location where team members share their code changes.
Remote Git Operations:
git push
,git pull
, andgit fetch
are used to synchronize your local repository with the remote one, requiring an internet connection.
Connecting Local to Remote:
1. Create a Remote Repository
On GitHub (or another Git hosting service), create a new repository.
Copy the repository URL provided (e.g.,
https://github.com/username/repository.git
).
2. Initialize Your Local Repository (if not done already)
If you haven’t already initialized Git locally:
git init
3. Add Remote Repository URL
Link your local repository to the remote repository using git remote add
:
git remote add origin https://github.com/username/repository.git
Here:
origin
is the default name for the remote repository.Replace
https://github.com/username/repository.git
with your actual repository URL.
4. Verify Remote Repository
To check if the remote repository was added correctly, use:
git remote -v
Tasks
- Set Your Username and Email Address in Git
Before committing code, it’s important to associate your identity with the commits. Use the following commands to set up your Git user details:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Create a Repository Named "DevOps" on GitHub and Push Local Commits
Follow the above mentioned steps to create remote repository on GitHub.
Connect the Local Repository to GitHub:
git remote add origin https://github.com/your-username/DevOps.git
- Create a New File:
mkdir -p Devops/Git
echo "This is a day 12 task of #90DaysOfDevOps challenge..." > Devops/Git/Day-02.txt
- Stage, Commit, and Push Your Changes:
git add .
git commit -m "First Commit"
git push origin master
Now your code is pushed to remote repository on GitHub.
Happy Learning:)
Subscribe to my newsletter
Read articles from Imran Shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by