Day10: Devops Journey

Day10: Devops Journey

Advance Git & GitHub Part-1.

Git Branching:

  • In Git, a branch is a new/separate version of the main repository.

  • Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes. This makes it harder for unstable code to get merged into the main code base, and it gives you the chance to clean up your future's history before merging it into the main branch.

  • In the normal scenario, we have a default branch ("master" or "main" ) and then we have other sub branches called the feature branch.

    The Feature branch are used to push code that contains changes from the local repository to the remote repository without disturbing the line of development i.e. the default branch.

Naming Convention of Branches:

A branch can be named on the purpose they solve. Here's a table explaining the ideal naming convention that should be followed:

Branch NamePurpose It solves
Feature BranchFor changes like adding features or updating a feature.
Bugfix branchFor commits containing Bug Fixes
Release branchFor Newer Version
Hotfix branchFor critical issues or bugs in a production environment

Commands:

#git branch - To check the current branch

#git branch branch_name - To create new branch

#git checkout -b stag - To Checkout and Create a branch at the same time

#git branch -a - To list all branches

#git checkout -d branch_name. - To delete branch

Git Revert and Reset:

What is Git Revert?

  • It is the command that is used for undo changes to a repository's commit history.

  • revert is the command we use when we want to take a previous commit and add it as a new commit, keeping the log intact.

  • Git Reverts creates a new commit in which the changes made in the previous commit are reverted or Undone. By doing this, it preserves the commit history and users can access to the past commit if needed.

    NOTE: It does not delete the commit.

  • #git revert commit_id

Lab:

Create some files:

#touch test1.txt test2.txt mistake.txt

#git add test1.txt , #git add test2.txt , #git add mistake.txt

#git commit -m "feat: added test1"

#git commit -m "feat: added test2"

#git commit -m "feat: added mistake"

#git log --oneline

Now, we will revert the commit mistake. Copy commit-id from log.

#git revert fd482ff

Here one editor will open, press control+x.

Same you can check in log. When you #ls command, mistake file is listing.

What is Git Reset?

  • reset is the command we use when we want to move the repository back to a previous commit, discarding any changes made after that commit.

  • Then there's git reset. It's like taking a step back in time. You tell Git you want to go back to how things were at a certain point, and it helps you get there. Just be careful with this one; it's powerful, and you don't want to accidentally erase something important!

  • This is a similar command as compared to git revert but the catch here is that it creates a commit undoing the changes and DELETES the commit which needs to be undone.

    There are two types of Resetting:

    • --soft : It resets the commit but still we can have the access to the commit in the git history.

    • --hard : It resets the commit and deletes it. This way we don't have any access to the history and the commit cannot be accessed.

#git reset commit_id

#git reset --soft commit_id

#git reset --hard commit_id.

Git Rebase and Merge

What is git Rebase?

  • Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualised in the context of a feature branching workflow. The general process can be visualised as the following:

  • Git rebase

From the above we can conclude, rebasing is changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit.

Git accomplishes this by creating new commits and applying them to the specified base.

What is Git Merge?

Git merge is a command used to merge two or more branches into the Main or Default branch.

Say we have a new branch feature that is based off the main branch. We now want to merge this feature branch into main.

Calling the git merge command will merge the specified branch feature into the main branch.

Commands:

#git merge