Advance Git & GitHub for DevOps Part-I

Suraj barikSuraj barik
4 min read

Git Branching🌿

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

Use a branch to isolate development work without affecting other branches in the repository. Each repository has one default branch and can have multiple other branches. Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.

Branch Command

🌿 git branch --list: List all branches in the repository.

🌱 git branch <branch name>: Create a new branch with the given name.

🌳 git checkout <branch name>: Switch to the specified branch.

🌲 git checkout -b <branch name>: Create a new branch and switch to it in one command.

🍂 git merge: Merge the changes from the specified branch into the current branch.

Git Revert◀️ and Reset🔄

Two commonly used tools that git users will encounter are of git reset and git revert. The benefit of both of these commands is that you can use them to remove or edit changes you’ve made in the code in previous commits.

Revert◀️:

The revert command helps you undo an existing commit.

It does not delete any data in this process instead Rather git creates a new commit with the included files reverted to their previous state. So, your version control history moves forward while the state of your file moves backward.

git revert <commit-ID>

Git reset 🔄:

The git reset command is used to reset the state of the repository to a specific point in the commit history. This can be useful for undoing changes, reverting to a previous commit.
Syntax: git reset <commit-ID>

Inspecting History:

📜 git log: View the commit history.

📃 git log --oneline: View a condensed version of the commit history.

🗺️ git log --graph: Display the commit history as a graph.

📌 git show: Display the details of a specific commit.

Git Rebase and Merge

Git Rebase and Git Merge are both Git commands used to integrate changes from one branch into another, but they have different approaches and effects on the commit history.

Git Merge:

Git merge is used to merge changes from one branch to another branch. When you want to merge changes from a feature branch into a main branch, you can use git merge to combine the changes.

Usage:

 git merge <branch>

The <branch> parameter represents the branch from which you want to merge the changes.

Example:

git merge feture

git squash:

git squashing combines multiple commits into a single commit based on your commit history. With the help of squashing you can clean your branch history and can maintain an organized commit timeline. It is used before pulling requests or merging feature branches.

git merge feature-branch --squash

git rebase:

Git rebase integrates the changes from one branch to another. Rebasing is the process of combining or moving a sequence of commits on top of a new base commit. Git rebase is the linear process of merging.

git rebase <branch name>
git rebase feature

This command will take the commits from the current branch and reapply them on top of the main branch.

Advantages:

  • Linear history: Rebase allows you to create a linear commit history by incorporating the changes from one branch into another.

  • Cleaner history: It avoids unnecessary merge commits, resulting in a cleaner commit history.

Assignment

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 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.

Add a new commit in 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”

  • 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

Restore the file to a previous version where the content should be “This is the bug fix in the development branch” [Hint use git revert or reset according to your knowledge]

Let's revert

git revert 6db60b5
vim version01.txt
# Removed Changes

COPY

git add .
git commit -m "This is the bug fix in development branch"
cat version01.txt

Add some changes to dev branch and merge that branch in Main

Thank you for reading this article. Happy Learning !!!😊

0
Subscribe to my newsletter

Read articles from Suraj barik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Suraj barik
Suraj barik

I'm Suraj Barik Aspiring DevOps Engineer with Hands-on experience in Automating,Shell Scripting, Supporting in AWS, management, CI/CD, and DevOps processes.