Day 9 - Deep Dive in Git & GitHub for DevOps Engineers

Hey there! 👋
I'm Tushar Ranjan, a DevOps Engineer passionate. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps.
🛠️ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space.
🌐 Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!
Branching in Git
In Git, branching is a powerful feature that allows developers to work on multiple versions of a project simultaneously. Each repository starts with a default branch, typically named main or master. This default branch represents the main line of development.
Creating and Switching Branches
To create a new branch in Git and switch to it, you use the following commands:
git checkout -b <branch_name>
This command creates a new branch named <branch_name> and switches your working directory to that branch. If you want to switch to an existing branch, you simply use:
git checkout <branch_name>
To see a list of all branches in your repository, including remote branches, use:
git branch -a
You don't need to clone the repository again to create branches; these commands work directly within your cloned repository.
Creating Branches on GitHub
On GitHub, you can also create branches directly through the web interface:
Click on "View all branches" in your repository.
Click on "New branch".
Provide a branch name and create the new branch.
Once you have your branch set up, you can add new changes to files, commit them, and push them to the remote repository from your branch.
Merging Branches
To merge changes from your branch back into the stable branch (e.g., main), you typically create a pull request (PR):
Create a pull request on GitHub, where you can review the changes and request reviews from other developers.
After approval, the merge button becomes available, allowing you to merge your changes into the stable branch.
You can also merge branches using the git merge command:
git checkout main
git merge <your_branch_name>
Handling Merge Conflicts
Merge conflicts occur when Git is unable to automatically merge changes from different branches. This usually happens when multiple developers modify the same line of a file. To resolve merge conflicts:
Git will mark the conflicting lines in your files.
Manually edit the files to resolve conflicts.
After resolving conflicts, add the resolved files (
git add) and commit the changes.
Reverting Changes
To revert changes to a specific commit, use the git revert command:
git revert <commit_id>
This command creates a new commit that undoes the changes introduced by the specified commit.
Git Reset
Git reset allows you to move the HEAD pointer to a specific commit and optionally modify the index and working directory. There are three common modes of reset:
Soft Reset: Moves
HEADto the specified commit, keeps changes in the index.Mixed Reset: Moves
HEADto the specified commit, keeps changes in the working directory.Hard Reset: Moves
HEADto the specified commit, discards all changes in the index and working directory.
Here's how you can use these reset options:
git reset --soft <commit_id>
git reset --mixed <commit_id>
git reset --hard <commit_id>
Conclusion
Understanding branching in Git is crucial for managing complex development workflows. Branches allow teams to collaborate effectively, experiment with new features, and maintain stable codebases. By mastering Git branching strategies and commands like merge, revert, and reset, developers can navigate projects with confidence and efficiency.
~ Tushar Ranjan🙂




