What is branching in Git?
In Git, branching means creating a separate copy of the codebase that allows developers to work on different tasks simultaneously without affecting each other's work. Each branch can be modified independently and later combined back into the main codebase.
Commands Used:
git branch <branch_name>: to create branch
git checkout <branch_name>: to switch to it
git checkout -b <branch_name>: to create a new branch and switch to it
git merge <branch_name>: to combine changes from another branch to the current one
Example:
What are Git Revert and Git Reset?
git revert
and git reset
are both Git commands used to undo changes made to a codebase, but they work differently.
git revert
creates a new commit that undoes the changes made in a previous commit while leaving a record of the undo in the commit history. It's used when the changes have already been pushed to a shared repository or when you want to preserve the commit history.
On the other hand, git reset
removes the changes made in a previous commit by resetting the codebase to a previous commit. It's used when the changes are still local and have not been pushed to a shared repository. git reset
is a more destructive command and should be used with caution, as it erases the commit history of the reset commits.
In summary, git revert
undoes changes by creating a new commit, while git reset
erases changes by resetting the codebase to a previous commit.
Example:
Let's say you have a repository with the following commit history:
A -> B -> C -> D (HEAD)
And you want to undo the changes made in commit C
. You can use git revert
as follows:
git revert C
This will create a new commit E
that undoes the changes made in C
. The commit history will now look like this:
A -> B -> C -> D -> E (HEAD)
Note that C
still exists in the commit history, but the changes made in C
have been undone in E
.
Here's an example code of git reset
:
Let's say you have the same commit history as before:
A -> B -> C -> D (HEAD)
And you want to undo the changes made in commit C
. You can use git reset
as follows:
git reset --hard B
This will reset the codebase to commit B
and remove the changes made in C
and D
. The commit history will now look like this:
A -> B (HEAD)
Note that commits C
and D
have been removed from the commit history. Also, the --hard
option ensures that the changes made in C and D are completely erased from the codebase.
What Is Git Rebase?
Rebase is a Git tool that helps integrate changes from one branch into another. It involves moving a sequence of commits onto a new base commit, creating a linear history. This process is known as Git rebase.
What Is Git Merge?
Merging in Git means bringing the separate paths of code development together. By using the git merge
command, you can combine different branches of code development into a single branch. It's important to remember that these merge commands will affect the current branch you're working on.
Commands used: git merge branc_name
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
, version01.txt should reflect at local repo first followed by Remote repo for review.
Adding the file version01.txt:
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
Adding 2nd line: This is the gadbad code and committing it in the dev branch
Adding 3rd line: This feature will gadbad everything from now.
Restore the file to a previous version where the content should be “This is the bug fix in the development branch”
Note: You can also use git revert for the same