Day 10 - 90 DaysOfDevOps - Advanced Git & GitHub

Day 10 - 90 DaysOfDevOps - Advanced Git & GitHub

ยท

4 min read

๐Ÿ”– What is Git Branching?

Branching is the feature of the Git Version Control System, which allows developers to create a separate line from which they can work independently and then add their work to the main branch. Branching enables multiple team members to work on different features, bug fixes, or experiments simultaneously without interfering with each other's work. Each branch in Git represents an independent line of development, and changes made on one branch do not affect other branches until they are merged.

  1. Create a Branch: -

    To create a branch we can use the below command.

     git checkout -b dev
    
  2. Switch to a Branch: -

    once the branch is created, to work on that branch we need to switch to it, which is done by the below command.

     git chekout dev
    
  3. Committing Changes: -

    Once the code is developed it is present on the local machine which is referred to as Untracked. Then "git add " command allows to transfer the code from Untracked to Staged. By using the "git commit " command, that code will transfer from Staged to Tracked.


๐Ÿ”– What is Git Revert and Reset?

Git Revert: -

In Git, the git revert command is used to create a new commit that undoes the changes made in a previous commit. It allows you to "revert" the changes introduced by a specific commit without modifying the commit history. This is different from other commands like git reset, which can alter the commit history.

When you use git revert, a new commit is created that applies the inverse of the changes from the specified commit. This is useful when you want to undo a particular commit or a series of commits while preserving the commit history.

git revert <hash>

here hash is the special code that is obtained by the "git log" command.

Git reset: -

In Git, "git reset" is a command used to manipulate the Git commit history by moving the HEAD and branch references to a specific commit. It allows you to undo or modify commits, staging, and the working directory. There are three primary options for the git reset command: "soft," "mixed," and "hard."

  1. Soft Reset:
git reset --soft <commit-hash>

A soft reset moves the HEAD to the specified commit without changing the staging index or working directory. This means that all changes made after the specified commit will remain staged (index) and uncommitted. It's useful when you want to edit or recommit the changes from the previous commit.

  1. Mixed Reset (Default Behavior):
git reset --mixed <commit-hash>

A mixed reset (default behavior) moves the HEAD to the specified commit and updates the staging index but does not modify the working directory. Changes that were committed after the specified commit will be unstaged, allowing you to rework them before creating a new commit.

  1. Hard Reset:
git reset --hard <commit-hash>

A hard reset moves the HEAD to the specified commit and updates the staging index and working directory. It effectively discards all changes made after the specified commit, reverting your working directory to the state of the specified commit. Be cautious when using a hard reset, as it permanently removes all changes that have not been committed.


๐Ÿ”– What Is Git Rebase?

Git rebase is a powerful and commonly used command in Git that allows you to integrate changes from one branch into another by applying a series of commits in a more streamlined way. It essentially allows you to modify the commit history of a branch, making it appear as if the changes were made on top of the latest version of another branch.

The basic idea behind git rebase is to take the changes from one branch and apply them on top of another branch. This is different from merging, which creates a new commit that combines the changes from both branches. When you rebase a branch onto another, you rewrite the commit history of the rebased branch to include the new changes on top of the target branch's history.

The syntax for git rebase is as follows:

git rebase <target-branch>

Here, <target-branch> is the branch onto which you want to apply the changes from the current branch. To rebase, you need to be on the branch that you want to rebase (the one with the commits you want to move).


๐Ÿ”– What Is Git Merge?

Git merge is a command used to combine changes from one branch into another branch. It is a fundamental operation in Git for integrating work done in separate branches and bringing those changes together. The merge command allows you to merge the commit history of one branch into another, creating a new commit that incorporates the changes from both branches.

The basic syntax for git merge is as follows:

git merge <source-branch>

Here, <source-branch> is the branch that you want to merge into the current branch. The current branch is the branch you are on when you run the git merge command. After a successful merge, the changes from the source branch are combined with the current branch, and a new commit is created to record the merge.


ย