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

Sidharth ShuklaSidharth Shukla
3 min read

Git branching

Git branching is a powerful feature of Git that allows you to create parallel lines of development for your project. This is useful for a variety of tasks, such as:

  • Developing new features without affecting the main codebase.

  • Fixing bugs without affecting the latest release.

  • Experimenting with new ideas without disrupting the current workflow.

Branches are lightweight and fast to create, so you can easily create new branches whenever you need to. Once you're finished working on a branch, you can merge it back into the main codebase.

some tips for using Git branching effectively:

  • Create a branch for every new feature or bug fix.

  • Give your branches descriptive names.

  • Merge your branches back into the main codebase often.

  • Use a branching strategy that works for your team.

Git revert and Reset

Git Revert and Reset are both commands used to undo changes in Git. However, they work in different ways and have different use cases.

Git Revert

  • Creates a new commit that reverses the changes made by the specified commit.

  • Does not rewrite history.

  • Does not affect the working directory.

  • Is safe to use on public branches.

git revert <commit_hash>

Git Reset

  • Moves the HEAD pointer back to the specified commit.

  • Rewrites history.

  • Affects the working directory.

  • Should only be used on private branches.

git reset <commit_hash>

When to use each command

  • Use git revert to undo a commit that you made by mistake.

  • Use git reset to undo changes that you made to the working directory.

Git commit and Git rebase

Git merge and rebase are both used to integrate changes from one branch into another. However, they work in different ways and have different use cases.

Git Merge

  • Creates a new commit that combines the changes from two branches.

  • Preserves the entire history of both branches.

  • Can result in a messy history with merge commits.

git merge <branch_name>

Git Rebase

  • Moves the commits from one branch onto the top of another branch.

  • Rewrites history.

  • Creates a cleaner history with a linear commit chain.

git rebase <branch_name>

When to use each command

  • Use git merge, when you want to preserve the entire history of both branches.

  • Use git rebase, when you want to create a cleaner history with a linear commit chain.

Example of branching concept

  1. Here their is only one branch which is master.

  1. Now i m creating two additional branch which is feature and dev branch.

  1. Now i will make some changes in dev branch and then merge it to master branch.

  2. This is how git merge will work.

  3. Now its a time to do experiment with git rebase.

  4. First i will do switch to feature branch then make a commit then rebase it to master branch.

    1. Here conflict is arrises due to rebase/merge in same line in same code base, it will become more clear when you see this.

  1. For this kind of situatution generally we have to manually resolve the conflict in code base the use these command.
git rebase --continue

  1. This will complete git rebase

Conclusion

we understand some advance concept of git and perform some experiments based on Git.

0
Subscribe to my newsletter

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

Written by

Sidharth Shukla
Sidharth Shukla

I am full stack web developer, currently i am learning devops and cloud technology.