Junior
How to merge two different branches in Git?
sobes.tech AI
Answer from AI
There are two main methods: merge and rebase.
-
Merge
-
Creates a new merge commit that combines changes from both branches.
-
Preserves the change history in chronological order.
-
Can lead to a "noisier" history with many merge commits.
-
Process:
# Switch to the branch you want to merge into (e.g., master) git checkout master # Merge the feature_branch into the current branch git merge feature_branch
-
-
Rebase
-
"Rewrites" history by moving commits of one branch onto another.
-
Creates a more linear and clean history.
-
Can cause issues when working in a team if the branch has already been published.
-
Process:
# Switch to the branch you want to rebase (e.g., feature_branch) git checkout feature_branch # Rebase the feature_branch onto master git rebase master # Resolve conflicts if any, then continue: # git add <conflicted_files> # git rebase --continue # After a successful rebase, switch to master and do a fast-forward merge git checkout master git merge feature_branch
-
Choosing between merge and rebase depends on team preferences and repository complexity. Merge is suitable for preserving exact history, while rebase is for creating a cleaner, linear history.