Skip to main content

Command Palette

Search for a command to run...

Day 10 : A Comprehensive Guide to Git and GitHub: Branching, Reverting, Resetting, and More๐Ÿš€๐Ÿš€

Day#10 of 90 Days Of Devops Challenge

Published
โ€ข6 min read
Day 10 : A Comprehensive Guide to Git and GitHub: Branching, Reverting, Resetting, and More๐Ÿš€๐Ÿš€
V

Experienced QA professional with a passion for manual and automation testing. Proficient in DevOps practices, ensuring seamless integration and continuous delivery. Dedicated to ensuring top-notch software quality and efficiency. Eager to contribute my skills to Hashnode's vibrant tech community. Let's collaborate and create exceptional experiences!

Introduction

๐Ÿ‘‹ Hey there! Ever heard of Git and GitHub? They're like superheroes for developers ๐Ÿฆธโ€โ™‚๏ธ, making teamwork super smooth and code management a piece of cake ๐Ÿฐ. In this blog, we're going to explore some basic ideas about Git and GitHub. We'll chat about cool stuff like branches, going back in time ๐Ÿ•ฐ๏ธ, fixing things, combining code, and even some magic called rebasing. By the time we're done, you'll be a pro at these tricks, ready to tackle all sorts of adventures in your coding journey! ๐Ÿš€

๐ŸŒฟ Git Branching for Beginners

Imagine you're painting a picture, but you want to try out two different ideas at the same time without messing up the original. Git branching is like having two separate canvases where you can work on different things without mixing them up.

When using Git branches, developers can create these separate spaces to add new features or fix bugs without affecting the main painting. Each branch is like a snapshot of the artwork that you can edit without worrying about the original version.

This makes it super simple to organize and keep track of changes while collaborating with others. It's like having a magical cloning spell for your project! ๐Ÿช„๐Ÿ–Œ๏ธ

๐ŸŒฑ Creating a New Branch and Saving Changes:

  1. ๐ŸŒฟ Start by creating a new branch called "dev" from the main starting point.

  2. ๐Ÿ“ Put a new file named "version01.txt" inside a special place called "Devops/Git/". Inside this file, write "This is the first special part of our program."

  3. โœ”๏ธ Now, tell the system that you've done something important by confirming your changes with a message that makes sense.

  4. ๐Ÿš€ Share your changes with others by sending them to a faraway place where everyone can see and talk about them. This is like showing your work on a big screen for review.

๐Ÿ”„Git Revert and Reset

Here's a detailed comparison of Git Revert and Git Reset in a tabular form with emojis:

Git Revert ๐Ÿ”„Git Reset ๐Ÿ”„
Purpose๐Ÿ›‘ Undo changes by creating a new commit that undoes a previous commit's changes.๐Ÿ”™ Discard commits and move the branch pointer to a specific commit.
Commit History๐Ÿ“œ Commit history remains intact, and the undoing commit is added as a new commit.๐Ÿ”ช Commit history can be rewritten, and specific commits can be removed.
Usage๐Ÿ“‹ Used when you want to keep a clear history and collaborate with others without altering the commit history.โš ๏ธ Used for local changes and when you're certain about discarding previous commits. Not recommended for shared branches.
Undoing Multiple Commits๐Ÿ“š Can be used to undo multiple commits by specifying a range of commits.๐Ÿ”„ Can be used to move the branch pointer to any commit, effectively discarding commits after that point.
Safety๐Ÿ›ก๏ธ Safer option, as it doesn't alter the existing commit history and is suitable for public repositories.โš ๏ธ Riskier option, as it rewrites history and may lead to confusion when collaborating with others.
Workflow๐Ÿšถ Reverting commits maintains a linear history, which helps understand changes over time.๐Ÿ”€ Resetting can create a more complex branch history, potentially making it harder to track changes.
Commands๐Ÿ“ git revert <commit>๐Ÿ” git reset --<mode> <commit>
Common Use Cases๐Ÿž Undoing specific changes or fixing bugs while keeping a clean history.๐Ÿšซ Discarding local commits before they are pushed, starting over, or fixing local mistakes.

Remember, both commands have their place in different scenarios, so choose the one that best suits your needs and the context of your project! ๐Ÿค“

๐ŸงฉGit Rebase and Merge

Here's a detailed comparison of Git Rebase and Git Merge in a tabular form with emojis:

AspectGit RebaseGit Merge
๐Ÿ”„ PurposeIncorporates changes from one branch into another while maintaining a linear commit history.Combines changes from one branch into another while preserving the original branch's history.
๐Ÿ“ Commit HistoryCreates a cleaner, more linear history by replaying commits on top of the target branch.Preserves the commit history of both branches, resulting in a more branching and merging-like history.
๐Ÿงฉ Branch StructureCan make the branch structure appear more straightforward and easier to follow.Results in a more branching-like structure that shows the separate development paths.
๐Ÿ“Œ Base CommitCommits are based on the latest commit of the target branch, making integration smoother.Creates a new commit with two parent commits, preserving the divergence point.
โณ Time ComplexityThis can lead to more conflicts and requires careful handling.Generally results in fewer conflicts but may clutter the history.
๐Ÿ› ๏ธ Interactive ModeAllows interactive editing of commits during the rebase process.Doesn't offer an interactive mode.
๐Ÿšง Use CasesIdeal for feature branches and topic branches where a linear history is preferred.Suited for integrating larger and more complex features or bug fixes from one branch to another.
๐ŸŒŸ AdvantagesProduces a cleaner, more logical commit history.Preserves the entire development path and can provide a clearer view of individual branch changes.
โšก DisadvantagesCan be complex and lead to conflicts if not handled properly.This may create a more cluttered history, especially when dealing with frequent merges.
๐ŸŒ CollaborationRequires more communication and coordination among team members due to history rewriting.Easier to understand for distributed teams and doesn't require as much coordination.
๐Ÿš€ ExecutionCommands like git rebase or interactive tools initiate the process.Commands like git merge bring changes from one branch into another.

Remember, both Git Rebase and Git Merge have their use cases and advantages. The choice depends on your team's preferences, the project's needs, and your desired commit history structure. Feel free to pick the one that suits your development workflow best! ๐ŸŒˆ

๐Ÿ“Task 1: Adding and Modifying Commits

โœ”Add more content to the file version01.txt and commit the changes as requested:

โœ…Restore the file to a previous version:

Using git revert

Using git reset

Note: Be cautious when using git reset as it rewrites history. It's generally safer to use git revert.

โœ…Push the changes from your local dev branch to the remote repository:

๐Ÿ“ Task 2: Exploring Branches and Collaboration

โœ…Create multiple branches, make changes

โœ…Add changes to the dev branch and merge it into master

โœ…Try out git rebase

๐ŸŽ‰Conclusion

Understanding Git branching, reverting, resetting, merging, and rebasing is essential for effective collaboration and version control in software development. By mastering these concepts, you'll be better equipped to manage projects, work on different features simultaneously, and maintain a clean commit history. With Git and GitHub at your fingertips, you have the tools to streamline your development workflow and enhance code quality.

Happy git-ing!๐Ÿš€๐Ÿš€

Thank you for taking the time to read this blog. I hope you found the information helpful and insightful. So please keep yourself updated with my latest insights and articles on DevOps ๐Ÿš€ by following me on :

Hashnode: vishaltoyou.hashcode.dev

LinkedIn: linkedin.com/in/vishalphadnis

So, Stay in the loop and stay ahead in the world of DevOps!

Happy learning ๐Ÿš€๐Ÿ˜Š๐Ÿง๐Ÿ“ฆ๐Ÿ”ง๐Ÿ› ๏ธ๐Ÿ’ป๐Ÿ’ผ๐Ÿ”๐Ÿ“ˆ๐Ÿš€๐ŸŒŸ๐Ÿ“Š๐Ÿ“š