Table of contents
- Day 8 Task: Basic Git & GitHub for DevOps Engineers.
- What is Git?
- What is Github?
- What is Version Control? How many types of version controls we have?
- Why we use distributed version control over centralized version control?
- How to install Git on your local system/computer:
- Create a GitHub Account
- Exercise 1: Create a New Repository on GitHub
- Exercise 2: Clone the Repository to Your Local Machine
- Exercise 3: Make Changes, Commit, and Push
- What is Git ?
- Why is Git Important?
- Difference Between Main Branch and Master Branch?
- Difference Between Git and GitHub?
- How to Create a New Repository on GitHub?
- Difference Between Local & Remote Repository and How to Connect Them?
- Connect Local to Remote Repository
- Task-1: Set User Name and Email Address
- Task-2: Create "Devops" Repository on GitHub
- Task-3: Connect Local to Remote Repository
- Task-4: Create a New File and Push Commits
- Git Branching
- Git Revert and Reset
- Git Rebase and Merge
- Task 1: Hands-On
- Task 2: Hands-On
- Git Stash:
- Cherry-pick:
- Resolving Conflicts:
Hello everyone !!
In this vlog, I'll share Git and GitHub concepts and commands.
Here you can explore all the commands and concepts with industry standards.
I have tried to make easy and understandable concepts. here you can learn and practice detailed Git and GitHub concepts. I hope this will be helpful.
Day 8 Task: Basic Git & GitHub for DevOps Engineers.
What is Git?
Git is a version control system that allows you to track changes to files and coordinate work on those files among multiple people. It is commonly used for software development, but it can be used to track changes to any set of files.
With Git, you can keep a record of who made changes to what part of a file, and you can revert back to earlier versions of the file if needed. Git also makes it easy to collaborate with others, as you can share changes and merge the changes made by different people into a single version of a file.
What is Github?
GitHub is a web-based platform that provides hosting for version control using Git. It is a subsidiary of Microsoft, and it offers all of the distributed version control and source code management (SCM) functionality of Git as well as adding its own features. GitHub is a very popular platform for developers to share and collaborate on projects, and it is also used for hosting open-source projects.
What is Version Control? How many types of version controls we have?
Version control is a system that tracks changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more.
There are two main types of version control systems: centralized version control systems and distributed version control systems.
A centralized version control system (CVCS) uses a central server to store all the versions of a project's files. Developers "check out" files from the central server, make changes, and then "check in" the updated files. Examples of CVCS include Subversion and Perforce.
A distributed version control system (DVCS) allows developers to "clone" an entire repository, including the entire version history of the project. This means that they have a complete local copy of the repository, including all branches and past versions. Developers can work independently and then later merge their changes back into the main repository. Examples of DVCS include Git, Mercurial, and Darcs.
Why we use distributed version control over centralized version control?
Better collaboration: In a DVCS, every developer has a full copy of the repository, including the entire history of all changes. This makes it easier for developers to work together, as they don't have to constantly communicate with a central server to commit their changes or to see the changes made by others.
Improved speed: Because developers have a local copy of the repository, they can commit their changes and perform other version control actions faster, as they don't have to communicate with a central server.
Greater flexibility: With a DVCS, developers can work offline and commit their changes later when they do have an internet connection. They can also choose to share their changes with only a subset of the team, rather than pushing all of their changes to a central server.
Enhanced security: In a DVCS, the repository history is stored on multiple servers and computers, which makes it more resistant to data loss. If the central server in a CVCS goes down or the repository becomes corrupted, it can be difficult to recover the lost data.
Overall, the decentralized nature of a DVCS allows for greater collaboration, flexibility, and security, making it a popular choice for many teams.
How to install Git on your local system/computer:
Install Git on your computer (if it is not already installed). You can download it from the official website at https://git-scm.com/downloads
Create a free account on GitHub (if you don't already have one). You can sign up at https://github.com/
Create a GitHub Account
Open your web browser and go to github.com.
On the GitHub homepage, click on the "Sign up" button.
Fill in the required information, like your username, email, and password.
Choose a plan that suits you (Free or one of the paid options) based on your needs.
Complete the verification process, which might include solving a CAPTCHA or confirming your email.
Once you finish these steps, congratulations! You've successfully made your GitHub account. Now, you can start sharing and collaborating on your projects.
Exercise 1: Create a New Repository on GitHub
Open your web browser and visit github.com.
Log in to your GitHub account.
On the GitHub homepage, click the "+" button at the top-right corner, then select "New repository" from the menu.
Give your repository a meaningful name eg. "git-demo".
You can also add a description to give more details about your repository.
Choose whether you want the repository to be public or private, depending on your needs.
Finally, click the "Create repository" button to create your new repository.
Now, Your central code storage is ready to go.
Exercise 2: Clone the Repository to Your Local Machine
Go to your repository page on GitHub and click on the "Code" button.
Copy the repository URL.
Open the terminal or command prompt on your computer.
Navigate to the directory where you want to save the repository.
Use the command "git clone" followed by the repository URL you copied. For example: " git clone github.com/your-username/your-repository.git "
Press Enter, and the repository will be cloned to your local machine.
now, you can see your repository by command "ls".
Exercise 3: Make Changes, Commit, and Push
Move to the repository directory.
Create files using the touch command.
Use the command "git status" to see the changes you made. It will show the modified files.
Use the command "git add" followed by the file names to prepare the tracking changes. For example: "git add filename.txt" or "git add ." to track all changes. Now check the status, and our file is now tracked.
Use the command "git commit" to save the changes with a meaningful message describing the modifications. For example: git commit -m 'first commit'
Finally, use the command "git push" to upload the committed changes back to the repository on GitHub. For example: "git push origin main" or "git push origin master," depending on the branch name.
Day 9 Task: Deep Dive in Git & GitHub for DevOps Engineers
What is Git ?
Git is a distributed version control system.
It tracks changes in source code during software development.
Why is Git Important?
Facilitates collaborative coding among developers.
Ensures a detailed history of code changes, helping track bugs and roll back to previous versions.
Enables branching and merging, making experimentation and feature development seamless.
Promotes a flexible and scalable workflow for development teams.
Difference Between Main Branch and Master Branch?
Main Branch
The default branch represents the most stable version of the codebase.
Synonymous with the production-ready code.
More inclusive terminology, reflecting a respectful and diverse environment.
Master Branch
Historically used as the default branch before the shift to "main."
Some repositories may still refer to it as the main branch.
Difference Between Git and GitHub?
Git
A version control system installed on a local machine.
Allows developers to manage code changes, branches, and commit history.
Operates offline, perfect for individual developers or small teams.
GitHub
A web-based hosting service for Git repositories.
Provides a centralized platform for sharing and collaborating on code.
Offers additional features like issue tracking, pull requests, and project management tools.
How to Create a New Repository on GitHub?
Follow these steps to create a new repository "Devops" on GitHub:
Log in to your GitHub account.
Click on the "+" sign in the top right corner and select "New Repository."
Name your repository "my-git-repo" and add an optional description.
Choose the repository's visibility (public or private).
Click on "Create Repository."
Difference Between Local & Remote Repository and How to Connect Them?
Local Repository
Exists on your computer and holds the complete history of a Git project.
Developers can make changes, create branches, and commit locally.
Remote Repository
Hosted on a server or a platform like GitHub.
Acts as a centralized location for collaboration and sharing.
Connect Local to Remote Repository
In the terminal, navigate to your local repository's root folder.
Run this below command, replacing <GitHub_Repository_URL> with your GitHub repository URL:
git remote add origin <GitHub_Repository_URL>
Task-1: Set User Name and Email Address
Open the terminal or Git Bash.
Run these commands, replacing "Your Name" and "your@email.com" with your details:
Task-2: Create "Devops" Repository on GitHub
Log in to your GitHub account.
Name the repository "Devops" and add a description.
Choose visibility (public or private).
Click on "Create Repository."
Task-3: Connect Local to Remote Repository
In the terminal, navigate to your local repository's root folder.
Run this command, replacing <GitHub_Repository_URL> with your GitHub repository URL:
git remote add origin <GitHub_Repository_URL>
Task-4: Create a New File and Push Commits
Create "Day-02.txt" inside "Devops/Git" directory.
Add content to the file.
In the terminal, navigate to the local repository's root folder.
Add the changes to the staging area:
git add Devops/Git/Day-02.txt
Commit the changes:
git commit -m "Added Day-02.txt file in the remote repo."
Push the changes to the remote repository on GitHub:
git push origin main
Day 10 Task: Advance Git & GitHub for DevOps Engineers.
Git Branching
Branching in Git allows us to work on different features, bug fixes, or experiments in isolation, without affecting other parts of the repository. Each repository has a default branch, usually called "master" or "main," and we can create multiple other branches to work on specific tasks.
Benefits of Branching:
Isolate development work.
Develop features and fix bugs separately.
Git Revert and Reset
Sometimes, we need to undo changes that we've made in previous commits. Two commonly used tools for this purpose are git reset
and git revert
.
Git Revert:
Reverts one or more commits by creating a new commit that undoes the changes.
Safer for collaborative workflows, as it doesn't rewrite history.
Useful for public repositories or shared branches.
Git Reset:
Resets the current branch to a specific commit, discarding any changes after that point.
More powerful but riskier, as it can rewrite history.
Suitable for private branches or when working on local repositories.
Git Rebase and Merge
In Git, we have two methods to integrate changes from one branch to another: git rebase
and git merge
.
Git Rebase:
Integrates changes from one branch onto another by moving or reapplying commits.
Results in a cleaner, linear history without unnecessary merge commits.
Useful for keeping the commit history tidy and more manageable.
Git Merge:
Combines changes from one branch into another by creating a new merge commit.
Preserves the commit history of both branches, resulting in a more complex history.
Useful when maintaining a more accurate record of branch integration points.
Task 1: Hands-On
Let's put our newfound knowledge into practice with a series of tasks:
Step 1 - Add New File:
Create a text file called
version01.txt
inside theDevops/Git/
directory.Write the content "This is the first feature of our application" in the file.
Create a new branch named
dev
from themaster
branch .git checkout -b dev
Commit your changes with the message "Added new feature."
git add
version01.txt
git commit -m "Added new feature"
Step 2 - Push to Remote:
Push the
dev
branch to the remote repository for review.git push -u origin dev
Task 2: Hands-On
Continuing from Task 1, let's move on to the next set of actions:
Step 3 - Add More Content:
Switch to the
dev
branch.Edit
version01.txt
and add the following lines:1st line>> This is the bug fix in the development branch
Commit this with the message "Added feature2 in the development branch."
2nd line>> This is gadbad code
Commit this with the message "Added feature3 in the development branch."
3rd line>> This feature will gadbad everything from now.
Commit with the message "Added feature4 in the development branch
# Switch to the 'dev' branch
echo "This is the bug fix in the development branch" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature2 in the development branch."
echo "This is gadbad code" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature3 in the development branch."
echo "This feature will gadbad everything from now." >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature4 in the development branch."
Step 4 - Revert Changes:
Restore the
version01.txt
file to a previous version where the content is "This is the bug fix in the development branch."Use
git revert
orgit reset
based on your preference and understanding.
# Revert using git revert
git log # Find the commit hash of the previous version you want to revert to
git revert <commit_hash>
# Reset using git reset
git log # Find the commit hash of the previous version you want to reset to
git reset --hard <commit_hash>
Day 11 Task: Advance Git & GitHub for DevOps Engineers: Part-2
Git Stash:
git stash
saves changes temporarily without committing.Use
git stash pop
to apply stashed changes later.git stash list
shows the list of stashed changes.git stash drop
deletes a stash.git stash clear
deletes all stashes.
Cherry-pick:
git cherry-pick
applies specific commits to another branch.Create two branches and make commits on each.
Cherry-pick the desired commits.
Resolving Conflicts:
git status
checks for conflicting files.git diff
shows differences in conflicting versions.Manually edit conflicting files.
git add
marks resolved files.
Task-01: Git Stash and Applying Changes
Create a new branch: Start by creating a new branch named "feature-branch" using
git checkout -b feature-branch
.Make some changes: Modify the necessary files in the new branch.
Stash your changes: Use
git stash
to save the changes without committing them.Switch to a different branch: Now, switch to a different branch, for example, "development" using
git checkout development
.Commit changes: In the "development" branch, make some changes and commit them using
git commit -m "Your commit message"
.Apply stashed changes: Return to the "feature-branch" using
git checkout feature-branch
, then usegit stash pop
to apply the stashed changes on top of the new commits.
Task-01: Git Stash and Applying Changes
# Task-01: Git Stash and Applying Changes
# Create a new branch and switch to it
git checkout -b feature-branch
# Make changes to the necessary files
# ...
# Stash your changes
git stash
# Switch to a different branch (e.g., development)
git checkout development
# Make some changes in the development branch and commit them
# ...
# Apply stashed changes on top of the new commits
git checkout feature-branch
git stash pop
Task-02: Cherry-pick and Rebase
Add lines to "version01.txt" for feature2.1 and commit.
Add more lines for feature2.2 and commit.
Finalize feature2 and commit.
# Task-02: Cherry-pick and Rebase
# Switch to the development branch
git checkout development
# Modify the version01.txt file as described in the task
# ...
# Commit changes for feature2.1
git add version01.txt
git commit -m "Added feature2.1 in development branch"
# Add more lines for feature2.2
# ...
# Commit changes for feature2.2
git add version01.txt
git commit -m "Added feature2.2 in development branch"
# Add final lines for feature2 completion
# ...
# Commit changes for feature2 completion
git add version01.txt
git commit -m "Feature2 completed"
Task-03: Cherry-pick in the Production Branch
Switch to "production" branch.
Cherry-pick commit "Added feature2.2 in development branch".
Add more lines after cherry-picked commit.
Commit changes for the optimized feature.
# Task-03: Cherry-pick in the Production Branch
# Switch to the production branch
git checkout production
# Cherry-pick the commit "Added feature2.2 in development branch"
git cherry-pick <commit_hash> # Replace <commit_hash> with the actual commit hash
# Add more lines after the cherry-picked commit
# ...
# Commit changes for the optimized feature
git add version01.txt
git commit -m "Optimized the feature"