Understanding_of_branching_merging_pull_requests.md
November 28, 2023 · View on GitHub
Version Control with Git: Understanding of Branching, Merging, Pull Requests
Branching in Git
Branching is a core concept in Git that allows developers to diverge from the main line of development and continue to work independently without affecting the main line. Each branch represents an independent line of development.
Key Points
-
Creation: A new branch is created using
git branch [branch-name]. It's a lightweight process as Git only creates a new pointer, it doesn’t duplicate the entire codebase. -
Usage: Branches are typically used for developing new features, fixing bugs, or experimenting. This ensures that the main branch (often
mainormaster) always contains production-quality code. -
Switching Branches: Use
git checkout [branch-name]to switch between branches.
Merging in Git
Merging is the process of integrating changes from one branch into another. It’s a critical part of the Git workflow, particularly when a feature or fix in a development branch is ready to be integrated into the main branch.
Key Points
-
Fast-forward Merge: If there are no new commits on the base branch since the feature branch was created, Git performs a fast-forward merge.
-
Three-way Merge: If there have been changes on the base branch, Git performs a three-way merge, involving the two branches and their common ancestor.
-
Conflict Resolution: If there are conflicting changes, Git will pause the merge and ask the user to resolve the conflicts manually.
Pull Requests (PRs)
Pull requests are not a feature of Git itself but are a key feature of online Git repository hosting services like GitHub, GitLab, and Bitbucket. PRs are a mechanism to request that another developer (or group of developers) review and merge your branch into the main branch.
Key Points
-
Collaboration: PRs are a collaboration tool, allowing for code review, discussion, and additional commits before changes are merged.
-
Integration with CI/CD: Pull requests can be integrated with continuous integration/continuous deployment (CI/CD) systems to automatically test the changes.
-
Merging: After approval and passing any integrated tests, the PR can be merged into the target branch.
Example Workflow
-
Create a Branch: For a new feature or bug fix.
git checkout -b feature-branch -
Make Changes and Commit: Work on the feature and commit changes.
git commit -am "Add feature" -
Push to Remote Repository:
git push -u origin feature-branch -
Create a Pull Request: Via the hosting service's interface, create a PR for
feature-branchto be merged intomain. -
Review, Discuss, and CI Tests: Other developers review the code, discuss, and run automated tests.
-
Merge the PR: If everything is satisfactory, the PR is merged into
main. -
Delete the Feature Branch: Optionally, after merging.
Conclusion
Understanding branching, merging, and pull requests is crucial in a collaborative software development environment. These processes facilitate multiple developers working on the same codebase without conflict, ensure code quality through reviews, and maintain a stable codebase in the main branch.