The labs on this site need JavaScript to run. The same content is written out below.
Learn › Architecture and operations › Collaboration
Merge and rebase
Three ways to combine two branches
Why it exists — If several people combine the changes they made separately by copying files, they lose track of who changed what and overwrite each other's edits. Git's merge and rebase combine the changes of two branches automatically and ask a person only about the parts where the same place was edited, and they leave the combined result in the commit history.
Where it's used today — When you combine a Pull Request on GitHub, you choose one of a merge commit, squash, or rebase, and each team decides which shape to leave in the history. You use rebase to move your own working branch on top of the latest main, and merge to combine a branch that is already shared with other people. If you do not want to keep the small commits made during the work, you use squash to leave one commit per feature.
While you work on the feature branch, new commits also appear on main. Whether you combine the two with merge or with rebase changes the shape of the commit graph, and if both changed the same line, you get a merge conflict.
Checked against real execution · Checked by creating a temporary repository with git 2.55
Concepts in this lab
- Merge and rebase
- Two ways to combine the changes of two branches. A merge joins both histories as they are (if the histories have diverged, a merge commit is created, and if they have not, only the branch pointer moves forward). A rebase moves your commits to after the latest point of the target branch and applies them again, which makes the history a single line. A rebase re-creates existing commits as new commits, so do not use it on commits that you have already shared with others.
- Merge conflict
- A situation in which two branches changed the same part of the same file in different ways, so Git cannot combine them automatically. A person looks at the parts marked as conflicting, decides which side to keep or how to combine the two, edits the file, and then completes the merge.
- Git fetch, pull, and push
- Why it exists — A central repository system like SVN puts a commit on the server right away, so you cannot commit without a network, and others can take the commit as soon as you make it. Git first keeps the commit in the repository on your own computer, and lets people decide when to exchange with the remote repository using fetch, pull, and push.
- Where it's used today — At the start of work, you get the team's latest changes with pull, and when the work is finished, you push and then open a Pull Request. To see the differences before merging, you fetch and then check with git log or git diff.
- The fetch command downloads new changes from the remote repository but does not apply them to your working branch. The pull command does a fetch and then also merges the downloaded changes into the current branch. The push command uploads your local commits to the remote repository.
- Git-flow and GitHub Flow
- Why it exists — Without branch rules, people create branches by their own standards, and it becomes unclear which branch is what is deployed now and where to branch an urgent fix from. Git-flow and GitHub Flow decide in advance the kinds of branches and the direction of merging, so the team works in the same order.
- Strategies for managing branches. Git-flow keeps a branch for releases and a branch that integrates development, and it manages features, release preparation, and urgent fixes on their own separate branches. It suits products that release versions on a fixed schedule. GitHub Flow is a simple approach in which you create a short-lived working branch from a single base branch, merge it after review, and deploy right away. It suits services that deploy at any time.
- Code review and pull requests
- Why it exists — If everyone pushes changes straight to the base branch, others use that code without knowing what changed, and mistakes show up only after they reach production. A pull request adds a step before merging in which you see the changes on one screen, exchange opinions in comments, and check the automated test results.
- Where it's used today — On GitHub and GitLab (where it is called a Merge Request), you protect the base branch so that only changes with review approval can be merged, and sometimes a CODEOWNERS file automatically sets who reviews each path. So that reviews do not drag on, split the work so that one pull request is a small change of about one feature.
- A pull request (PR) is a request to merge the changes on a working branch into the base branch. Code review is the process in which other developers examine the changes before they are merged. It finds defects early, keeps the code consistent, and shares knowledge within the team.
Try these
- Default settings — merge. A merge commit that joins the two lines is created.
- Merging after a rebase — The commits of feature are moved and attached to the end of main. The result is a single line.
- When main has no new commits — main just moves forward, with no merge commit (fast-forward).
- When both changed the same line — A merge conflict occurs. A person has to choose.
- Rebasing a branch that is already pushed — It now differs from the commits on the remote, so you have to push by force.
- squash — Several commits of feature are squashed into one and added to main.
Running with the default settings
- This is the starting state. The feature branch split off at commit B and added two commits, D and E. In the meantime, commit C, pushed by someone else, appeared on main.
- A new commit M that combines the contents of C and E is created. M is a merge commit with two parents (C and E). The existing commits D and E stay as they are, and the graph keeps a record of when the lines split and when they were joined.
Result of this run
- merge does not touch existing commits and adds one merge commit. The history stays as it really happened, but the graph gets complicated when there are many branches.
Code
git switch main
git merge feature
Assumptions of this model
This is an example of a small repository with five commits. It assumes that a conflict occurs in only one place in one file. The behavior of each command follows the explanations in the official Git documentation (git-merge, git-rebase). Teams usually decide on a merge method (merge commit, squash, or rebase) and stick to it. Rather than one being correct, it is a choice of what shape to leave the history in. It was checked by creating a temporary repository with git 2.55 in our test run. merge created a merge commit with two parents and kept the hashes of the existing commits, and when main had no new commits, it was a fast-forward. After rebase, the hashes of D and E changed and the history became a single line without a merge commit; for a branch that was already pushed, a normal push was rejected and a force push was needed. The squash commit had one parent, and git did not recognize that feature had been merged. The conflict markers were also exactly as shown on the screen.
Previous lab: Distributed tracing