Navigating Branch Management with Git Flow: Strategies for Success
Imagine Version Control and Team Collaboration in software development as being like a super-organized group project in a cooking class.
Version Control is like your recipe book: Imagine you're making a fancy cake. You start with a basic recipe, but as you go, you try different ingredients and techniques. Maybe you add some chocolate chips or try a new frosting. Version Control is like having a magic recipe book that remembers every single version of your cake recipe, from the plain vanilla start to the triple-chocolate, sprinkle-covered extravaganza it becomes. Made a mistake? No problem! The magic recipe book (your version control system like Git) lets you flip back to any previous version of your cake, so you can see where you went right (or wrong). It's a lifesaver when you're experimenting and need to backtrack.
Team Collaboration is like having a bunch of chefs working together: Now, imagine you're not baking this cake alone. You've got a team of fellow chefs, each with their own specialty. One is great at making dough, another is a frosting wizard, and someone else is a decoration guru. Team Collaboration tools (like GitHub, Trello, or Slack) are like your kitchen communication system. They let you all work on the cake at the same time without bumping into each other or accidentally adding salt twice. You can share updates ("I just finished the frosting!"), ask for help ("Who knows how to make a fondant unicorn?"), and make sure everyone is on the same page (literally and figuratively).
When you mix Version Control with Team Collaboration, it's like having a master plan for the cake, along with a top-notch team that communicates perfectly. Each chef can work on their part of the cake, check their work against the recipe book, and ensure everything comes together perfectly for the final masterpiece. It's a recipe (or should I say, software) for success.
Mastering Git and GitHub/GitLab
Before we get into live demos and live demonstration it is important to understand what is Git and Github and how they interact with each other. Think of Git and GitHub as the dynamic duo of software development, like Batman and Robin in the world of code!
How Git and GitHub Interact with Each Other:
?Git is like a superhero's toolkit: It's a version control system that helps you manage and track changes in your code. Imagine you're writing a story, and every time you make a change, you save a new version. Git does
that for your code. Each change is like a snapshot, capturing exactly how things looked at that moment. You can always go back to a previous version if you need to.
?GitHub is like the superhero's headquarters: It’s a place where you store your Git repositories (the whole collection of your snapshots) online. GitHub takes your local toolkit and puts it in the cloud, so you can access it from anywhere. It also adds superpowers like issue tracking, documentation, and collaboration tools.
?When you use Git and GitHub together, you get the best of both worlds: the ability to track and manage your code changes locally with Git, and then push those changes to GitHub for safekeeping and collaboration. You can share your code with others, contribute to open source projects, and collaborate on team projects, all while keeping track of every little change.
?For the purpose of demonstration the application we are working uses the “MERGE GIT WORKFLOW” which is explained in our next section.
?
Git Commands, Branching, and Workflows
Git Flow offers several features not included in the default Git setup, such as mechanisms for fixing production code, developing new features, and preparing for deployments.
Main/Master Branch: This is production code—plain and simple. A major advantage of Git Flow is that you never directly code on the master branch. Instead, all changes are introduced through merging or pull requests. This process guarantees that your code has been thoroughly reviewed and is ready for deployment.
?Hot Fixes: Need to address an issue in production? Simply start a hotfix branch and begin coding. When you're finished, a unique step occurs: a double merge—one into the master and another into the develop branch. This ensures that the bug you've resolved doesn't reappear.
?Develop Branch: This branch, similar to the master, is not directly committed to. It is initially a direct copy of the master branch when enabled. This setup is a one-time configuration, maintaining synchronization between the branches. The develop branch serves as an integration branch where merges from both hot-fix and feature branches occur. The advantage of this approach is that all code converges before deployment, saving time and reducing unnecessary stress.
Feature Branch: This is one of the standout features of the Git Flow pattern. It provides a staging area for your releases to production. Like hotfix branches, release branches also conclude with a double merge. This ensures that if any bugs or issues are discovered during a release, you can address them, and the fixes will automatically propagate to both your master and develop branches.
Daily Work on an Issue:
The described Git workflow is ideal for daily development activities, especially when working on specific issues or features. It ensures that your working environment is always synchronized with the latest updates from the main branches, either master or develop. This process is crucial when multiple developers are contributing to the same project, as it minimizes conflicts by regularly integrating changes. By starting each day with this workflow, developers can confidently make progress on their tasks, knowing that they are building on the most current and stable version of the project. This routine is particularly beneficial in a fast-paced development setting where code changes frequently and collaboration is key.
1.??? Switch to the master branch: git checkout master/develop
git checkout develop
2. Update your local master branch
git pull
3. Switch to your working branch: git checkout mybranch
git checkout mybranch
4. Merge changes from master into your branch: git merge master
git merge develop
Starting a New Issue:
When embarking on a new issue or feature in a project, it's crucial to follow a systematic approach using Git to ensure that your contributions are organized and do not interfere with the ongoing work of other team members. This scenario provides a structured workflow for initiating work on new issues. It begins with syncing the latest updates from the main development branches, either master or develop, which serves as a base for all new code changes. By creating a separate branch for each new issue, developers can focus on their tasks without disrupting the main codebase. This practice not only enhances collaboration among team members but also facilitates easier tracking and management of individual contributions, making it ideal for teams striving for efficiency and clarity in their development process.
1.??? Switch to the master branch: git checkout master/develop
领英推荐
git checkout develop
2.??? Update your local master branch: git pull
git pull
3.??? Create and switch to a new branch: git checkout -b newbranch name
git checkout -b mybranch
4.??? Push the new branch and set upstream tracking: git push --set-upstream origin newbranch name
git push –set-upstream origin mybranch
Making a Pull Request:
When you've completed your work on an issue and are ready to integrate your changes into the main project, making a pull request is the next critical step. This process begins by staging all modified files using git add, ensuring that only the intended changes are included in the submission. Following this, the changes are committed locally with a descriptive message using git commit. Once the local commits are ready, they are pushed to the remote repository using git push. The final step involves creating a pull request through the GitHub UI, which allows your team to review the changes. This review process is essential for maintaining code quality and ensuring that the new contributions align with the project’s direction and standards. This methodical approach not only helps in organizing and documenting changes but also fosters collaborative decision-making and feedback among team members.
1.??? Stage changed files: git add <file_name> for each changed file
2.??? Commit your changes: git commit
git commit -am “add new function”
3.??? Push your changes: git push
git push
4.??? Create a pull request in the GitHub UI.
Delete Local Branch:
After the successful completion and merging of a feature or issue, it’s often necessary to clean up your local and remote repositories by deleting the now obsolete branches. This process starts by removing the branch from your local machine using git branch -D mybranch, which forcibly deletes the specified branch. Subsequently, to ensure that the branch is also removed from the remote repository, you execute git push origin :mybranch. This command deletes the branch from the remote, preventing any further access or confusion. Lastly, to make sure your local branch list is up-to-date and synchronized with the remote, run git fetch -p. This command cleans up any stale references in your local Git repository. Deleting branches that are no longer needed helps in maintaining a clean, manageable, and organized repository, which is essential for ongoing project clarity and management.
1.??? Delete the branch locally
git branch -D mybranch
2.??? Delete the branch in the remote repository
git push origin :mybranch
3.??? Synchronize your branch list:
git fetch -p
Build Web Project and Commit with Comments:
1.??? Commit with a message and push:
git commit -am "#180 fix static analysis issues"
git push
Article References:
Camping Coder. (2018, April). How to use Git Flow. Retrieved from https://www.campingcoder.com/2018/04/how-to-use-git-flow/
Microsoft. (n.d.). Git branching guidance. Retrieved from https://learn.microsoft.com/en-us/azure/devops/repos/git/git-branching-guidance?view=azure-devops
Google Slides presentation]. Retrieved from https://docs.google.com/presentation/u/0/d/1hdr4bp6NqYSJCxwubZKOy7ZtYxLYHtbmtRsvRGFsifg/htmlpresent?pli=1
+17K | Software Delivery Manager | Public Speaker | Mentor | Blockchain | AI/ML | DEVOPS | SRE | Oracle DBA
5 个月strongly recommend : https://tech-tech.life/2024/10/03/5-reasons-why-we-boldly-switched-from-gitflow-to-trunk-based-development-tbd-a-developers-essential-guide/