Advance Git & GitHub for DevOps Engineers
#90 Days of DevOps Challenge - Day 10
Git Branching
A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes. So, it is complex to merge the unstable code with the main code base and also facilitates you to clean up your future history before merging with the main branch.
To create a new branch:-
git branch <name-of-new-branch>
Similarly, to delete a branch use the command
git branch –D <branch name>
To switch to the new branch, we type the command
git checkout <branch-name>
Git Revert and Reset
These two commands are used in Git to undo changes to a project code and history, but in different ways.
Git Revert
Git revert
is similar to git reset
, but the approach is slightly different. Instead of removing all the commits in its way, the revert ONLY undoes a single commit by taking you back to the staged files before the commit.
So, instead of removing a commit, git revert
inverts the changes introduced by the original commit by creating a new commit with the underlying inverse content. This is a safe way to revoke a commit because it prevents you from losing your history.
It is important to know that the revert will have no effect unless the commit hash or reference is specified.
This command helps you in reverting a commit, to a previous version
git revert <commit-id>
<commit-id> can be obtained from the output of git log
Git Reset
The git reset
command is used to undo the changes in your working directory and get back to a specific commit while discarding all the commits made after that one.
For instance, imagine you made ten commits. Using git reset on the first commit will remove all nine commits, taking you back to the first commit stage. Before using git reset
, it is important to consider the type of changes you plan to make. You can use multiple options along with git reset
, but these are the main ones. Each one is used depending on a specific situation: git reset --soft
, git reset --mixed
, and git reset --hard
Command:-
git reset <commit_ID>
Git Rebase and Merge
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.
Command:-
git rebase <source branch>
Git Merge
If you want to apply changes from one branch to another branch, you can use the merge command Should be used on remote branches, since history does not change Creates a new commit, which is a merger of the two branches.
Imagine, you have a Master branch and a Feature A branch. The developer has finished his/her work in the feature A branch and wants to merge his work in the master. If he is using git merge, a new commit will be created, which will have the changes of Feature A and Master branch combined. Any new commits to the Feature branch will be isolated from the master branch
Command:-
git merge <source-branch-name>
If you need to see the graph
git log --graph --pretty=oneline
Tasks:-
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
switch todev
branch ( Make sure your commit message will reflect as "Added new feature")
- version01.txt should reflect at local repo first followed by Remote repo for review.
Add a new commit in dev
branch after adding the 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 the development branch
Commit this with the message “ Added feature2 in 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
- Restore the file to a previous version where the content should be “This is the bug fix in the development branch”
Task 2:-
- Demonstrate the concept of branches with 2 or more branches with a screenshot.
In the organization if you are working on any changes to the code then you are not using the master branch directly so you have to create another branch, for example, you created a staging branch and then you change whatever is needed and if the code is stable and working fine it stage branch then you can merge it to master branch after approval
- add some changes to
dev
branch and merge that branch inmaster
- as a practice try git rebase too, see what difference you get.
Thank you for reading!! I hope you find this article helpful!!
if any query or if any correction to be done in this blog please let me know.
Happy Learning!!
Saikat Mukherjee