#Day10:- 90 Days DevOps Challenge@Advance Git & GitHub for DevOps Engineers: Part-1
Enhancing Collaboration in DevOps through Advanced Git & GitHub
🌿Git Branching:
🌿 Branches are essential in Git to isolate development work without affecting other parts of the repository. Each repository has one default branch, but you can create multiple other branches. They act as isolated environments where you can develop new features, fix bugs, or safely experiment with new ideas. When you're ready, you can merge a branch into another branch using a pull request, ensuring seamless integration of your changes. 🚀🌱
🔀Git Revert and Reset:
🔙 Git Revert: "Git revert" is a command used to undo specific commits by creating a new commit that reverses the changes introduced in the original commit. It allows you to safely remove unwanted changes from the version history while preserving the commit history.
🔙 Git Reset: "Git reset" is a command used to move the HEAD (current branch pointer) to a specified commit, effectively resetting the branch to that state. It can be used to undo commits, discard changes, or unstaged files from the staging area. There are different modes of reset, including soft, mixed, and hard, each with varying degrees of impact on the working directory and staged changes. Care should be taken when using "git reset" as it can rewrite history and potentially lead to data loss.
🌐Git Rebase and Merge⛓️:
🔄 Git Rebase: "Git rebase" is a command used to incorporate changes from one branch into another by moving or combining commits. It replays the changes of the current branch on top of another branch, creating a linear history. This process can help keep the commit history clean and make it easier to manage changes.
🔀 Git Merge: "Git merge" is a command used to combine changes from one branch into another. It integrates the commit history of one branch with another, creating a new merge commit that combines the changes from both branches. The merge commit joins the histories, and it is often used to integrate feature branches back into the main branch.
🥏GIT Rebase Vs GIT Merge:- Git rebase changes the commit history by replaying commits on top of another branch, creating a linear history, while git merge combines changes from one branch into another, preserving separate commit paths.
📝Task 1:
:- Add a text file called version01.txt inside the devops90days/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master.
SOLUTION:- Create an file in devops90days/Git/ with name version.txt with touch
command (touch version.txt).
and write "This is first feature of our application” in the file.
:-You need to add the untracked file with git add .
:- After a git add . you need to commit the file with git commit -m "Added
New Feature"
:- Now push this file in your git repository with git push command which is git push -u origin main
(here, main is your branch name).
:- In the next step you need to create a new branch and also switch to that branch in same time. You can do this thing with help of git checkout -b dev
(Here, dev is your new branch name).
:- Now you have created the new branch whose name is dev. You can check it with the help of git branch
command.
📝TASK 2:
:- Add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines
1st line>> This is the bug fix in development branch
SOLUTION :- add "This is the bug fix in development branch" in version.txt with echo > version01.txt This is the bug fix in development branch.
-
Commit this with message “ Added feature2 in development branch”
-
Now add, 2nd line>> "This is gadbad code" with
echo >> version01.txt This is gadbad code
.
- Now again Commit this with the message “ Added feature3 in the development branch".
Now add the 3rd line>> This feature will gadbad everything from now.
- Now, Commit with the message “ Added feature4 in development branch"
: - 📝Restore the file to a previous version where the content should be “This is the bug fix in the development branch”
(With the "git reset HEAD~3"
You can revert the required no.of commits by adding HEAD~1 for 1 commit back, HEAD~2 for 2 commit back and so on.)
In the above image you can check i have reset the file with 3 commit back with above git reset command.
📝TASK 3:
🤳In task 3 we will go to learn more about git branches.
#Currently, we have 2 branches 1 is the main and another one is the dev branch.
#You can check the branches with "git branch
" command.
Now add some changes to
dev
branch and merge that branch inmaster
:- I have made the folder file in the dev branch and after that, i switched to main branch and run the
git merge dev
command for merging the dev branch into a main branch. Don't forget to run git add . and git commits after any change and before the merge.
🌿Git Merge:
Merging integrates changes from one branch into another, preserving separate commit paths.
It creates a new merge commit that combines both branch histories.
Merged branches retain their individual commit histories, and the merge commit shows the integration point.
Example: Suppose you have a main branch and a feature branch "feature-branch". After working on the feature branch, you want to merge it into the main branch.
# Switch to the main branch
git checkout main
# Merge the feature-branch into the main branch
git merge feature-branch
🌿Git Rebase:
Rebasing moves the commits from one branch to another, creating a linear history.
It replays the commits of the current branch on top of another branch.
The commit history appears as if the work was done sequentially on the main branch.
Example: Suppose you have a main branch and a feature branch "feature-branch". After working on the feature-branch, you want to rebase it onto the main branch.
# Switch to the feature-branch
git checkout feature-branch
# Rebase the feature-branch onto the main branch
git rebase main
In summary, git merge preserves separate commit paths, while git rebase creates a linear history. Choosing between the two depends on your workflow and how you want to manage the commit history.
⚰️Conclusion
🚀 As a DevOps engineer, this blog covered Git merge, rebase, branching, revert, and reset. Git merge combines changes while preserving paths 🔀, rebase creates a linear history 🔄, and branching isolates work in separate environments 🌳. Git revert undoes commits, while reset moves HEAD to a specific commit 🔄. Mastering these Git techniques streamlines workflows, fosters collaboration, and delivers high-quality software efficiently 🎉.