Day 10 #90DaysOfDevOps Challenge

Advanced Git Concepts

Git and GitHub - Advanced concept

⚡Git Branching

The branch is nothing but a pointer to a commit. Branches help you to isolate your work from others or isolate environments.

The default branch is Master.

Commands-

Create new branch

git branch <name>

Switch to a branch

git checkout <branch name>

Create and switch to a new branch

git checkout -b <name>

Delete a branch

git branch -D <name>

⚡Git Revert and Reset

Both commands are used to undo changes, but differently.

⭐Git Revert

It removes the changes from the specified commit and adds a new commit to the log history.

For example, if you have 5 commits and realized that the 5th commit was not appropriate and want to set the head to the previous commit but you do not want to alter the history. Then you can use the revert command which will add a 6th commit after removing the changes committed in the 5th commit.

Go for this option if you want to undo changes on a public branch, for safety.

No alt text provided for this image

Command and available options-

git revert <commit ID>

--no-edit - It does not change the commit message for the commit you are reverting. The new commit will have the same commit message, with 'revert' added to it.

--no-commit - It will not automatically create a new commit to record the revert. Instead, it will leave the changes unstaged so you can review them and decide whether to commit the revert manually.

Git Reset

Go back to a previous commit by deleting the other commits in between. It alters the log history. This option is better when undoing changes on a private branch.

For example, if you made 5 commits and realized that there was some sensitive data in the last 2 commits and you have to go back to commit 3 in a way that the last 2 commits should be removed from the history. You can use the reset command.

Command and available options-

--soft - commits will be deleted but changes will remain in the staged area (green).

--mix - commits will be deleted and changes will remain in the unstaged area (red).

--hard - commits and changes both will be deleted completely.

Note-

If you do not specify any flag then by default --mix is used.

You should be careful while using --hard as it performs a permanent delete of changes.

git reset --soft <Id of the commit to which we want to reset our head>

When you delete the commits with this command in the local repo, the push fails because the remote repo has those commits. So, in this case, you have to do a force push.

git push --force origin <branch>

No alt text provided for this image

⚡Git Rebase and Merge

⭐Git Merge:

Git merge is one of the merging techniques in git, in which the logs of commits on branches are intact.

Example - If you have a project with 3 commits (1,2,3) on the master branch and the feature branch has 2 commits A and B. If you perform a git merge operation then commits A and B will be merged as commit 4 onto the master branch, which will show that commits A and B have come from the feature branch.

No alt text provided for this image

Advantages-

  • The logs help in understanding what changes were merged when and from which branch. The history is very clear.

  • It is easy to find mistakes and resolve them.

Disadvantages-

  • Gives a clumsy log/history.

  • Not very user-friendly.

⭐Git Rebase:

Git Rebase is similar to git merge, but the logs are modified after merging. Git rebase was introduced to overcome the limitation of merging, i.e., to make logs of repository history look linear.

Example - If you have a project with 3 commits (1,2,3) on the master branch and the feature branch has 2 commits A and B. If you perform a git rebase operation then the commits A and B will be rebased onto the master branch as commit 4 and 5 and there will be no logs of the feature branch. You will only see 5 commits 1,2,3,4,5.

No alt text provided for this image

Advantages-

  • The logs are linear.

  • It’s easy to move through the project.

Disadvantages-

  • You cannot track, when and how the commits were merged on the target branch.

No alt text provided for this image


⚡Tasks:

⭐Task1-

Step 1 - 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 dev branch coming from the master. switch to the dev branch ( Make sure your commit message will reflect as "Added new feature").

No alt text provided for this image

No alt text provided for this image

Step 2 - Version01.txt should reflect at the local repo first followed by the Remote repo for review.

My remote repo does not have the dev branch.

No alt text provided for this image

No alt text provided for this image

No alt text provided for this image

Step 3 - 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”

No alt text provided for this image

  • 2nd line>> This is gadbad code.

  • Commit this with the message “ Added feature3 in the development branch."

No alt text provided for this image

  • 3rd line>> This feature will gadbad everything from now.

  • Commit with a message “ Added feature4 in the development branch.

No alt text provided for this image

Restore the file to a previous version where the content should be “This is the bug fix in the development branch”

\> Using the git reset command - this will remove the last two commits where we added the 2nd and 3rd line, and will move the changes to the unstaged area.

No alt text provided for this image

If we do a git push then we can see the content only has - “This is the bug fix in the development branch”

No alt text provided for this image

\> To remove the commit as well as the changes from the workspace, we can use

git reset --hard <commit ID>

⭐Task2-

  1. Demonstrate the concept of branches.

No alt text provided for this image

2. Demonstrate the concept of Merge.

No alt text provided for this image

\> I added one new commit to the master branch.

No alt text provided for this image

\> Added 2 new commits to the dev branch.

No alt text provided for this image

\> Now merging Dev into master.

No alt text provided for this image

3. Demonstrate the concept of Rebase.

\> In the above demo I used the merge command to add the changes from dev to master.

\> Now I will use rebase and we will see the difference in the history.

No alt text provided for this image

The dev branch commits linearly added to the master branch and the commit IDs have changed because the parent commit has changed.


Thank you for reading! 📘