What is Git Branching, Revert, Reset, Rebase, and Merge

Day 10 Task: Advance Git & GitHub for DevOps Engineers.

Saurabh Dahibhate ♾️☁️
5 min readMar 9, 2023

Hello everyone I am back with a new DevOps task.

advance-git-for-devops-engineers

Git Branching

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. You can merge a branch into another branch using a pull request.

Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.

Git Revert and Reset

Two commonly used tools that git users will encounter are those 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.

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 actually called “merge,” even though both procedures do essentially the same thing.

01. Add a text file called version01.txt inside the DevOps/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master, swithch to dev branch ( Make sure your commit message will reflect as "Added new feature").

Here are the steps to add a text file called version01.txt inside the Devops/Git/ directory with "This is first feature of our application" written inside. This should be in a branch coming from master and you should switch to the dev branch with a commit message reflecting "Added new feature":

  1. Open a terminal or command prompt.
  2. Navigate to the directory where you want to create the Git repository.
  3. Run the command git init. This will initialize a new Git repository in the current directory.
  4. Run the command git checkout -b dev. This will create a new branch called dev and switch to it.
  5. Create a new file called version01.txt in the Devops/Git/ directory. You can do this by running the command touch Devops/Git/version01.txt.
  6. Open the version01.txt file in a text editor.
  7. Write “This is first feature of our application” inside the file.
  8. Save the file and close the text editor.
  9. Add the file to the staging area by running the command git add Devops/Git/version01.txt.
  10. Commit the changes to the repository with a commit message reflecting “Added new feature” by running the command git commit -m "Added new feature".
  11. Push the changes to the remote repository by running the command git push -u origin dev.

Now you have created a new branch called dev and added a new file called version01.txt with the message "This is first feature of our application" inside it. The changes have been committed to the repository with a commit message reflecting "Added new feature”. The changes have also been pushed to the remote repository.

Add new commit in dev branch after adding 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 development branch
  • Commit this with message “ Added feature2 in development branch”
  1. Run the command git checkout dev. This will switch to the dev branch.
  2. Open the version01.txt file in a text editor.
  3. Add the content “This is the bug fix in development branch” as the first line in the file.
  4. Save the file and close the text editor.
  5. Add the changes to the staging area by running the command git add Devops/Git/version01.txt.
  6. Commit the changes to the repository with a commit message reflecting “Added feature2 in development branch” by running the command git commit -m "Added feature2 in development branch".
  7. Push the changes to the remote repository by running the command git push origin dev.
  • 2nd line>> This is gadbad code
  • Commit this with message “ Added feature3 in development branch
  • 3rd line>> This feature will gadbad everything from now.
  • Commit with message “ Added feature4 in development branch

Do steps 03 and 06 with change in content of text and message while commiting the changes.

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

To restore the version01.txt file to a previous version where the content is "This is the bug fix in development branch", you can use the git checkout command followed by the commit hash or branch name of the version you want to restore. Here are the steps:

  1. Open a terminal or command prompt.
  2. Navigate to the directory where your Git repository is located.
  3. Run the command git log --oneline. This will display a list of commits in the repository with their commit hashes and commit messages.
  4. Identify the commit hash or branch name of the version you want to restore. Look for the commit message “Added feature2 in development branch” or any other identifying information.
  5. Run the command git checkout <commit hash or branch name> Devops/Git/version01.txt. Replace <commit hash or branch name> with the hash or name of the version you want to restore.
  6. Verify that the version01.txt file has been restored to the correct version by opening it in a text editor and checking that the content is "This is the bug fix in development branch".

That’s all about todays task of DevOps journey.

Thankyou for reading.

If you liked this story then click on clap do follow for more interesting and helpful stories.

— — — — — — — — #keepLearning_DevOpsCloud — — — — — — — —

advance-git-for-devops

--

--

Saurabh Dahibhate ♾️☁️

- ⭐Passionate Web Developer and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.