In this blog, we will discuss about advanced Git topics like git revert and reset, git rebase, and git merge.
Git Branching
Git branching is a feature in the Git version control system that allows you to create multiple branches of code in a single repository. A single repository will have one default branch and can have multiple branches.
Branches are independent lines of development that can be used to work on new features or bug fixes without affecting the main codebase.
To create a new branch, the following command can be used:
git branch <branchname>
To change branches, the following command is used:
git checkout <branchname>
Git branching allows multiple people to work on different features or parts of a project without interfering with each other's work. It also provides a way to test changes without affecting the main codebase until they are ready to be merged.
Git revert and Git reset
Git revert and git reset are two important Git commands that are used to undo changes made to a repository. However, they have different purposes and use cases.
git revert
git revert
is used to create a new commit that undoes the changes made in a previous commit. It undoes a commit while preserving the commit history. The changes are undone in a new commit, and the original commit remains in the commit history. This is useful when you want to undo a commit, and also retain the history of the commit.
Syntax:
git revert <commit_ID>
git reset
git reset
is used to move the branch pointer to a different commit, henceforth erasing commits from the commit history. It allows you to undo commits and "reset" the repository to a previous state. 'git reset' is not recommended, and any unmonitored usage can turn dangerous as it can remove all the previous history.
Syntax:
git reset <commit_ID>
Git Rebase and Git Merge
git rebase
git rebase
is a Git command that allows you to modify the commit history of a branch. It's used to apply a series of commits from one branch onto another branch and can be used to combine, re-order or edit commits.
git merge
git merge
is a Git command that allows you to combine changes from different branches into a single branch. When you merge branches, Git creates a new commit that incorporates the changes from the merged branch into the current branch.
Tasks
Task 1
Problem Statement: Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside. This should be in a branch coming from the master switch to the dev branch
version01.txt should reflect at the local repo first followed by the Remote repo for review.
Add a new commit in the 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
Problem Statement:
Demonstrate the concept of branches with 2 or more branches with a screenshot.
Let's say I am working on a project that has a main branch, which contains the stable version of the code. I want to modify the code but don't want to disturb the stability of the code. In this case, I will create a new branch called dev.
git branch dev
A new dev branch is created, but I am still not working on the dev branch. to switch from main to dev:
git checkout dev
Now we can add files and commit them to this branch. The main branch will be untouched. But if I want to merge the dev and main branch, then I will have to switch to the main branch and utilize the git merge command.
git checkout main
git merge dev
To view all branches you can use the code:
git branch --all
Add some changes to the dev branch and merge that branch in the main.
Image: Before Merging
Image: After merging
Try git rebase too, and see what difference you get.
To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.
#Day10 #90DaysofDevops