Day 10 Task: Advance Git amp; GitHub for DevOps Engineers.
Muhamad Kamran
DevOps Engineer|AWS DevOps Engineer |Linux OS| Git and Github| Docker|Jenkins CI|CD Pipelines | Kubernetes |Python | Azure | Shell Scripting |Ex.NOC & IT Support | CCNA | CCNP | MTCNA | Recommended
Day 10 Task: Advance Git amp; GitHub for DevOps Engineers.
Git Branching
Use a branch to isolate development work without affecting other branches in the repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.
Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
Git Revert and Reset
Two commonly used tools that git users will encounter are those of git reset and git revert . The benefit of both of these commands is that you can use them to remove or edit changes you’ve made in the code in previous commits.
Git Rebase and Merge
What Is Git Rebase?
Git rebase is a command that lets users integrate changes from one branch to another, and the logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs.
What Is Git Merge?
Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact.
The merge wording can be confusing because we have two methods of merging branches, and one of those ways is actually called “merge,” even though both procedures do essentially the same thing.
Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from?master, [hint try?git checkout -b dev], swithch to?dev?branch ( Make sure your commit message will reflect as "Added new feature").?
To accomplish this task, you can follow these steps:
1.??? Navigate to the directory where your Git repository is located using the command line.
2.??? Make sure you are on the master branch by using the following command:
git checkout master
3.??? Create a new branch named dev and switch to it:
git checkout -b dev
4.??? Create the text file version01.txt inside the Devops/Git/ directory and add the specified content:
echo "This is the first feature of our application" > Devops/Git/version01.txt
5.??? Add the new file to the staging area:
git add version01.txt
6.??? Commit the changes with the commit message "Added new feature":
git commit -m "Added new feature"
Now, you have successfully added a text file called version01.txt with the specified content inside the Devops/Git/ directory. The changes are committed to the dev branch with the commit message "Added new feature."
Here are the steps to add new commits to the dev branch with the specified content and then restore the file to a previous version:
1.??? Add new commits in the dev branch:
# Add the first line to the file
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
# Add and commit the changes with message "Added feature2 in development branch"
git add Devops/Git/version01.txt
git commit -m "Added feature2 in development branch"
# Add the second line to the file
echo "This is gadbad code" >> Devops/Git/version01.txt
# Add and commit the changes with message "Added feature3 in development branch"
git add Devops/Git/version01.txt
git commit -m "Added feature3 in development branch"
# Add the third line to the file
echo "This feature will gadbad everything from now." >> Devops/Git/version01.txt
# Add and commit the changes with message "Added feature4 in development branch"
git add Devops/Git/version01.txt
git commit -m "Added feature4 in development branch"
?
领英推荐
2.??? Restore the file to a previous version:
To restore the file to a previous version, you can use git reset:
# Reset the last commit, which is "Added feature4 in development branch"
git reset HEAD~1
# This will unstage the changes but keep the modifications in the working directory
# Check the status to make sure the changes are unstaged
git status
# Now, if you want to completely discard the last commit and revert the changes in the working directory
git checkout Devops/Git/version01.txt
Now, the file Devops/Git/version01.txt is restored to the state of the commit "Added feature3 in development branch," which contains the content "This is gadbad code."
?
Task 2:
Here's a step-by-step guide:
Demonstrate the Concept of Branches:
Create and Switch to dev Branch:
git checkout -b dev
Make Changes in dev Branch:
# Make some changes to your files
echo "Some changes in dev branch" >> Devops/Git/version01.txt
Commit the Changes in dev Branch:
git add Devops/Git/version01.txt
git commit -m "Made changes in dev branch"
Create Another Branch (feature) and Make Changes:
git checkout -b feature
# Make some changes
echo "Some changes in dev branch" >> Devops/Git/version01.txt
Commit Changes in feature Branch:
git add version01.txt
git commit -m "Made changes in feature branch"
Merge dev Branch into master:
git checkout master
git merge dev
Practice git rebase:
git checkout feature
git rebase dev
Resolve any conflicts if they occur.
git checkout master git merge feature
git branch -d feature
Note: