Introduction
Today’s task focuses on advanced Git and GitHub functionalities, essential for any DevOps engineer. We'll delve into Git branching, revert and reset commands, as well as rebase and merge strategies. These are crucial for efficient version control and collaborative development.
Understanding Git Branching
Branches in Git allow you to work on features or fixes in isolation. Each repository starts with a default branch, often called "master" or "main", but you can create multiple branches to manage different tasks.
Git Revert and Reset
Git Revert: Creates a new commit that undoes the changes of a previous commit, preserving history.
Git Reset: Moves the current branch to a specific commit, altering the history and potentially losing changes.
Git Rebase and Merge
Git Rebase: Integrates changes from one branch into another, modifying commit history for a cleaner project history.
Git Merge: Combines branches, preserving the commit history from both branches.
Tasks
Task 1: Feature Development with Branches
Create a Branch and Add a Feature:
Create
version01.txt
inDevops/Git/
with "This is the first feature of our application".Create and switch to a new branch:
git checkout -b dev
Add and commit changes:
git add Devops/Git/version01.txt git commit -m "Added new feature"
Push Changes to GitHub:
Push the branch to GitHub:
git push origin dev
Add More Features with Separate Commits:
Add more lines and commit after each change:
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt git commit -am "Added feature2 in development branch" echo "This is gadbad code" >> Devops/Git/version01.txt git commit -am "Added feature3 in development branch" echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt git commit -am "Added feature4 in development branch"
Restore the File to a Previous Version:
Revert the file to an earlier state:
git revert HEAD~2
Task 2: Working with Branches
Demonstrate Branches:
- Create multiple branches and take screenshots of the branch structure.
Merge Changes into Master:
Merge the dev branch into master:
git checkout master git merge dev
Practice Rebase:
Rebase the dev branch onto master and observe the differences:
git rebase master
Conclusion
Mastering Git commands like branching, revert, reset, rebase, and merge is vital for efficient version control. These skills allow for better collaboration and code management, which are crucial for any DevOps engineer. Continue practicing these tasks to enhance your proficiency in Git and GitHub.