DevNet Associate (DEVASC) v1.0Software Development and DesignMedium
A development team is using Git for version control. They have a `main` branch and several feature branches. A developer has finished working on `feature-X` and needs to integrate their changes into `main`. The `main` branch has received several new commits since `feature-X` was branched off. The team wants to maintain a clean, linear history on `main` without introducing merge commits for every feature. Which Git operation should the developer use to achieve this?
- Agit reset --hard
- Bgit cherry-pick
- Cgit rebase main
- Dgit merge --no-ff
Show answer & explanationAnswer & explanation
Correct answer: C. git rebase main
`git rebase main` on the `feature-X` branch will move the entire `feature-X` branch to begin at the tip of the `main` branch, effectively rewriting the project history by creating new commits for each commit in `feature-X`. This results in a linear history when `feature-X` is then fast-forward merged into `main`.
Why the other options are wrong
- A. `git reset --hard` discards local changes and resets the branch to a specific commit, which is a destructive operation and not for integrating features.
- B. `git cherry-pick` applies individual commits from one branch to another, which is not suitable for integrating an entire feature branch.
- D. `git merge --no-ff` would create a merge commit, explicitly violating the requirement for a linear history without merge commits.
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. It rewrites project history, creating a linear sequence of commits.
- Used to maintain a linear project history.
- Rewrites commit history by creating new commit IDs.
- Should generally not be used on public, shared branches.
- Often followed by a fast-forward merge (or squashed merge) into the target branch.
Memory trick: Rebase makes history clean, like a fresh start.