Git Branching
A branch is a version of the repository that diverges from the main working project. A Git project can have more than one branch. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes. We can use a branch to isolate development work without affecting other branches in the repository. You can merge a branch into another branch using a pull request.
Git Reset and Revert
Two commonly used commands that git users use when they want to remove or edit changes that have been made in the code in previous commits.
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.
Let's say we have made ten commits. Using git reset on the first commit will remove all nine commits, taking us back to the first commit stage.
There are multiple options available along with git reset. Each one is used depending on a specific situation: git reset --soft, git reset --hard and git reset --mixed.
git reset --soft:
The --soft aims to change the HEAD (where the last commit is in your local machine) reference to a specific commit.
git reset --soft <commit-ID> moves back to the head with the <commit-ID>
git reset --mixed:
This is the default argument for git reset. Running this command has two impacts: (1) uncommit all the changes and (2) unstage them.
git reset --hard
When using the hard reset on a specific commit, it forces the HEAD to get back to that commit and deletes everything else after that.
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.
When to use Git Revert vs. Reset
Git Reset
With git reset, you can go back to the previous commits, but can’t create a new commit.
This option is better when undoing changes on a private branch.
Git Revert
Using git revert, you can go back to previous states while creating a new commit.
Go for this option if you want to undo changes on a public branch, for safety.
What Is 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.
What Is Git Merge?
Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact.
The merge wording can be confusing because we have two methods of merging branches, and one of those ways is called “merge,” even though both procedures do essentially the same thing.
Task 1
1.Add a text file called version01.txt inside the DevOps repository with “This is the first feature of our application” written inside. This should be in a branch coming from master
, [hint try git checkout -b dev
], switch to dev
branch ( Make sure your commit message will reflect as "Added new feature").
version01.txt should reflect at the local repo first followed by the Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
2.Add a new commit in dev
branch after adding the below-mentioned content in Devops/version01.txt.While writing the file make sure you write these lines
Doing changes in the file version01.txt
- 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".
Check “git log” and then restore the file to a previous version using "git reset" command 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, add some changes to the dev
branch and merge that branch in the master.
As a practice try git rebase too, and see what difference you get.
The default branch is 'Master'. The file created in the workspace will be visible in any of the branch workspaces until you commit. Once you commit, then that file belongs to that particular branch.
We created a new branch, modified the files, committed a message, and pushed the branch.
We have successfully rebased to the master branch.