Explore foundational concepts and best practices of branching and merging strategies in Git, including workflows, conflict resolution, and branch types. Perfect for beginners seeking to understand how to manage collaborative development with Git's core features.
In Git, what is the primary purpose of creating branches during software development?
Explanation: Creating branches allows developers to make changes for new features or bug fixes in isolation from the main codebase, ensuring stability. Deleting unused files is not a function of branching, nor does branching directly relate to file transfer speed. The number of commands required does not decrease due to branching, as each branch often requires its own operations.
Suppose two developers work on separate branches and want to bring their changes together. Which Git operation should they use?
Explanation: The 'git merge' command combines changes from different branches into one, allowing collaborative development. 'git remove' deletes files, not merges branches. 'git pull-request' is not a standard git operation, and 'git duplicate' does not exist in Git for merging purposes.
Which branching strategy suggests creating a new branch for each new feature or fix being developed?
Explanation: Feature branching involves creating a separate branch for every new feature or fix, which makes parallel development and later integration easier. The single-branch workflow uses only the main branch for all changes. Linear commit flow refers to a development process without branching, and detached HEAD state is a situation where HEAD points to a commit, not a branch.
If a branch can be merged into another without any divergent changes, what type of merge occurs by default in Git?
Explanation: A fast-forward merge occurs when there are no divergent changes in target and source branches; Git simply moves the pointer forward. Squash merge combines all changes in a branch into a single commit. Octopus merge is for merging more than two branches, and reverse merge is not a standard term in Git.
What must a developer do when a merge conflict occurs during a Git merge?
Explanation: Merge conflicts require manual intervention; the developer must edit the affected files, resolve differences, and complete the merge. Shutting down the computer or ignoring the conflict will not solve the problem, and deleting the repository is unnecessary and counterproductive.
In most Git repositories, what is the typical role of the main (or master) branch?
Explanation: The main branch generally holds the stable, production-ready code that is deployed or released. Experimental changes are made on separate branches, binary files can exist anywhere but are not the focus of the main branch, and the main branch can be merged into others as needed.
If a developer wants to place their feature branch changes on top of the latest main branch commits, which Git operation can achieve this?
Explanation: The 'git rebase' command reruns commits from the feature branch onto the latest base branch, creating a linear history. 'git remove' deletes files, 'git stash' temporarily shelves changes, and 'git restore' reverts files back to a previous state, none of which relate to rebasing.
Which branch name is considered descriptive and appropriate when adding a login form to an app?
Explanation: Naming branches like 'feature/login-form' makes it clear what change is being made. Names such as 'asdf123', 'temp', and 'newbranch' are unclear and unhelpful for understanding the branch purpose.
When a critical bug is found in production, which type of branch is typically created to address it quickly?
Explanation: A hotfix branch is created for urgent bugs in production, allowing the fix to be prioritized and later merged back into main. A develop branch is used for ongoing changes, a release branch readies for deployment, and fork branch is not a standard Git term.
Why does Git sometimes create a separate merge commit when merging branches?
Explanation: A merge commit is created when histories have diverged, capturing the union of changes and maintaining a clear project history. It does not delete branches, reduce repository size, or obscure changes from others.