Advance Git & GitHub for DevOps Engineers.
Git Branching :
Think of branching like creating separate workspaces within a project. This lets developers work on new features, fix problems, or try out new ideas without messing up the main project. Each branch is like a separate path for working on different tasks at the same time, and later, these changes can be added back into the main project.
A branch is used to keep different tasks separate from each other within the project. Each project has one main branch and can have many other branches. You can combine the work from one branch into another using something called a pull request.
In short, branches make it easy to work on new features, fix bugs, or try out new ideas in a safe and separate part of your project.
Git Revert & Reset :
git reset and git revert are two handy tools that Git users often use. Both of these commands are powerful because they can help you remove or modify changes you’ve made in your code in previous commits.
Git Revert:
The git revert command is used when you want to make a new commit that undoes the changes made by a specific commit or a series of commits.
When you use git revert, it doesn’t actually delete the original commit from the history. What it does instead is create a new commit that is the opposite of the changes made in the commit(s) you’re targeting.
So, git revert is a safe way to undo changes. It’s especially useful when you want to keep your history intact and avoid changing the commit history.
Git Reset :
git reset is a command that lets you change the current branch to a different commit. This means that the branch will look like it did at that commit.
Unlike git revert, git reset does not make new commits. Instead, it changes the commit history by “hiding” the commits that came after the reset point. These hidden commits are not shown in the log, but they are still in the repository for a while (usually 30 days) until Git deletes them.
git reset is a command that you should use carefully because it can make you lose some of your commit history.
git reset --soft is a way of using git reset that changes the current branch to a different commit but keeps the changes in your working directory and staging area.
git reset --hard is a way of using git reset that changes the current branch to a different commit and throws away all the changes in your working directory and staging area that happened after the reset point.
Git Rebase and Merge :
What is Rebase?
Git rebase is a way of changing one branch to include the changes from another branch, and the logs are changed after the process is done. Git rebase was made to solve some problems with merging, especially about logs.
With rebase, you can add the changes from one branch to another, and make your branch start from a different point. This helps in avoiding complicated merge commits and makes a more straight and easy-to-see history.
What is Git Merge?
Git merge is a way of joining two Git branches and keeping the logs of commits on branches as they are.
The merge word can be confusing because we have two ways of joining branches, and one of those ways is also called “merge,” even though both ways do almost the same thing.
What’s the Difference Between Merge and Rebase?
The main difference between git merge and git rebase is that git merge is a way of combining changes from one branch (source branch) into another branch (target branch) whereas git rebase is a way of moving the changes from one branch onto another branch.
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 master branch, (Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commit command]
Prevent Errors: I misunderstood the assignment and unintentionally completed work on the development branch.
Now: switch to dev
branch [hint try git checkout -b dev
] 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
Commit this with message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with message “ Added feature3 in development branch
3rd line>> This feature will gadbad everything from now.
Commit with message “ Added feature4 in development branch
Task 2:
Demonstrate the concept of branches with 2 or more branches with screenshot.
add some changes to
dev
branch and merge that branch inmaster
as a practice try git rebase too, see what difference you get.
I hope you enjoy the blog post!
If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. I’ll be more than happy to help!