DevNet Associate (DEVASC) v1.0Software Development and DesignMedium
A development team is using Git for version control. A developer has made several local commits on a feature branch and now needs to integrate these changes into the main branch, ensuring a clean, linear history without creating a merge commit. Which Git command sequence achieves this goal?
- Agit checkout main; git pull; git merge feature-branch
- Bgit checkout feature-branch; git merge main
- Cgit checkout feature-branch; git rebase main
- Dgit checkout main; git rebase feature-branch
Show answer & explanationAnswer & explanation
Correct answer: C. git checkout feature-branch; git rebase main
To integrate changes from a feature branch into main with a linear history (no merge commit), the feature branch should be rebased onto main. This rewrites the feature branch's history to appear as if its commits were made on top of the latest main branch commits.
Why the other options are wrong
- A. This merges the feature branch into main, creating a merge commit and not preserving a linear history.
- B. This merges main into the feature branch, which is not the goal of integrating feature into main, and would create a merge commit.
- D. This would attempt to rebase the main branch onto the feature branch, which is generally not recommended for shared branches and doesn't achieve the desired integration of feature into main.
Git Rebase
Git rebase is a command that integrates changes from one branch onto another by moving or combining a sequence of commits to a new base commit, resulting in a linear project history.
- Rewrites commit history
- Creates a linear history (avoids merge commits)
- Moves a branch's commits to a new base
- Can be dangerous on shared branches if not handled carefully
Memory trick: Rebase makes history neat, like stacking blocks in a straight line.