Advanced Git and GitHub for DevOps: Git Branching, Merging, and Collaboration
Git is a powerful tool for Source Code management that can help DevOps teams manage and collaborate on code.
This blog will cover advanced Git features like branching, merging, and collaboration. We'll begin with the basics of Git branching, different branching strategies to manage your codebase, how to merge code changes and resolve conflicts when multiple developers are working on the same code and Importance of collaboration.
What is Git branching?
Git branching is a feature in the Git version control system that allows to create separate code bases, or "branches," that can be worked on independently from the main codebase. This enables developers to experiment with new features or approaches without affecting the main codebase
Each branch contains complete code copy of its parent branch. This makes it possible to work on multiple aspects of a project at the same time and to experiment with new ideas or approaches without impacting the main codebase.
Common Branching Strategies
There are several common branching strategies to manage code effectively.
Feature Branches: Feature branches are used to develop new features or functionality. Work on it independently, then merge it back into the master codebase when complete.
Release Branches: Release branches are created when a new version of a software product is ready to be released. The release branch is used to fix any last-minute issues or bugs before the product is released to the public.
Hotfix Branches: Hotfix branches are used to fix critical issues or bugs in a live production environment. A hotfix branch is created, the issue is fixed, then the hotfix branch is merged back into the master codebase.
Branching Conventions
When using Git branching, it's important to follow consistent conventions for branch naming and commit messages. This helps to ensure that branches are easy to understand and manage. Some common conventions include:
Branch Naming: Branch names should be descriptive and indicate the purpose of the branch. For example, a feature branch could be named "feature/add-new-login-page."
Commit Messages: Commit messages should be concise but informative. They should explain what changes were made and why, and should be written in the present tense. For example, "Add new login page" would be an appropriate commit message.
Let's Do Some Hands-On
Create Branches and Switch over to different branches.
Push chnages from local branch to remote branch.
Do your work on one of the feature branch and commit it.
Push that branch to remote repository.
Pull changes from remote branch to local branch.
Make changes into remote branch "feature/login-page".
Pull that changes to local branch "feature/login-page".
Advanced Git Branching Techniques/Commands
We will learn some of the most commonly used Advanced Git commands, including git revert
, git reset
, git cherry-pick
, git merge
, git rebase
, git stash
, and git squash
.
git revert
: This command is used to undo a commit by creating a new commit that reverses the changes made in the original commit. It's useful for rolling back changes while keeping a record of the previous state of the codebase.$ git revert [ commit ID ]
git reset
: This command allows you to reset the state of your repository to a previous commit. It can be used to discard changes made in the most recent commit or to reset the entire branch to a previous state.$ git reset [ commit ID ]
git cherry-pick
: This command allows you to apply a specific commit from one branch to another. It's useful when you want to include a particular change from one branch into another branch without merging the entire branch.$ git cherry-pick [ commit ID ]
git merge
: This command is used to combine changes from one branch into another branch. It creates a new commit that includes the changes from both branches.$ git merge [ branch name ]
git rebase
: This command is used to apply the changes from one branch onto another branch. It's useful for keeping the commit history clean and organized. It allows you to integrate changes from one branch onto another by moving the commits from one branch onto the tip of another branch.$ git rebase [ branch name ]
git squash
: Is not a separate Git command, but rather a technique for combining multiple commits into a single-larger commit. This can be done using thegit rebase
command with the--interactive
or-i
option. It's useful for cleaning up a branch's commit history and making it easier to understand.$ git rebase -i HEAD~N
----->'N' integer to consider number of commits.git stash
: This command is used to save changes that are not yet ready to be committed. It allows you to switch to another branch or work on another task without committing incomplete changes.Simply, You can store your changes to hidden space for some time
$ git stash
-----------------> put your current not commited work into stash$ git stash list
-----------> displays stash jobs list$ git stash apply stash@{0}
---> get back you incomplete job to working directory$ git stash clear
-----------> remove all stash jobs permanently from the stash$ git stash pop
-----------> opens recent stash to working directory and removes from stash$ git stash drop
-----------> removes the most recent stash job from the stash without applying it to the working directory.
By mastering these commands can help to work efficiently and effectively on software development projects and ensure that their codebase is organized, clean, and functioning correctly.
GitHub Collaboration
GitHub is a popular platform for collaborating with other developers on software projects.
Let's learn to use GitHub for collaboration:
Create a repository: Start by creating a new repository on GitHub. This will serve as the central location for the project's codebase. You can also fork an existing repository if you want to make changes to it.
Add collaborators: Invite other developers to collaborate on the project by adding them as collaborators to the repository. Collaborators can be given different levels of access, such as read-only or read-write access.
>> https://github.com/$USERNAME/$REPO-NAME/settings/access
+Add People
Create branches: Create branches for new features or changes to the codebase. This allows developers to work on their changes independently from the main codebase.
Set up branch protection rules: To enforce quality control and prevent unauthorized changes, set up branch protection rules for the main branch(es) of the repository. This can include requiring pull request reviews, requiring status checks to pass, and preventing force pushes.
>> https://github.com/$USERNAME/$REPO-NAME/settings/branches
+Add branch protection rule
Fork a repository and contribute changes: To contribute changes to an existing repository, you can fork the repository and make changes in your own fork. Once you've made changes, create a pull request to submit your changes back to the original repository.
Add reviewers and manage feedback: When creating a pull request, you can add reviewers who will provide feedback on your changes. You can also manage feedback from other developers by responding to comments and making changes based on their suggestions.
-
Resolve merge conflicts: If there are conflicts between the codebase in your branch and the codebase in the main branch, you will need to resolve the conflicts before your changes can be merged.
Overall, GitHub provides a powerful platform for collaboration on software projects. By setting up branch protection rules, forking repositories, and managing feedback from other developers, you can work with team and ensure the quality of the codebase.
Summary:
In this article, we learned about Git and GitHub features like branching, merging, and collaboration, Common branching strategies include feature, release, and hotfix, conventions for branch naming and commit messages. Advanced Git commands like revert, reset, cherry-pick, merge, rebase, stash, and squash can help to work efficiently. GitHub platform for collaboration, allowing developers to create and share repositories, manage pull requests, and review code changes.
I hope you learnt something valuable today with me!
Stay tuned for my next blog on "Containerization Tools". I will keep sharing my learnings and knowledge here with you.
Let's learn together! I appreciate any comments or suggestions you may have to improve my blog contents.
Thank you,
Chaitannyaa Gaikwad