GIT & VERSION CONTROL · REFERENCE
Branches and Merging
Develop one change separately, then combine its history with the main line of work.
At a glance
| Term or command | What it means | When to use it |
|---|---|---|
| Branch | A movable name pointing to a line of commits. | Keep an in-progress change separate from another line of work. |
| git switch -c NAME | Creates a branch and switches to it. | Begin a focused change from the current commit. |
| git switch main | Checks out the main branch. | Return after committing or otherwise preserving your current work. |
| git branch | Lists local branches, marking the current one. | Check where your next commit will go. |
| git merge NAME | Integrates the named branch into the current branch. | Combine reviewed work locally. |
| Fast-forward | Advances a branch pointer when histories have not diverged. | A merge may finish this way without creating a merge commit. |
Commands are examples, not an interactive terminal. Run them in your own learning repository after checking the prerequisites. Output, paths, commit IDs, and wording can vary. Host-specific instructions are labeled.
What a branch changes
A branch is not a separate project folder. In one working tree, switching branches updates the tracked files to the selected version when Git can do so safely. Commit or stash unfinished work first if switching might overwrite or mix it.
A──B main
└──C update-heading
Both branches share A and B. C belongs to the new line of work until it is integrated.
Create a branch for a heading update
Prerequisite: your local project has commits, main exists, and status is clean. Run inside that project.
Commands
git status
git switch -c update-heading
git branch
Example output
Switched to a new branch 'update-heading'
main
* update-heading
The asterisk marks your current branch. Edit and save index.html in your editor next. New commits will advance update-heading, while main stays where it was.
Record the change on its branch
git diff
git add index.html
git diff --staged
git commit -m "Clarify homepage heading"
The staging and commit workflow is unchanged. Branches organize history; they do not replace status, review, or testing.
Merge into main locally
Assume the branch’s edit is committed, status is clean, and main has no additional commits. This is the local-only route; a team may instead require a pull request.
Commands
git switch main
git merge update-heading
git log --oneline --graph --all
Example output
Updating b2c3d4e..d4e5f6a
Fast-forward
Merge integrates into the branch you have currently checked out: here that is main. If both branches advanced, Git may create a merge commit or ask you to resolve a conflict. A local merge does not update a remote until pushed.
When you use a hosted review instead
Push the feature branch and open a review request on the host. GitHub calls it a pull request; GitLab calls it a merge request. After the host merges it, switch to main locally and pull the result. Avoid mixing this route with an independent local merge unless you understand the combined history.
Sources and further reading
Study questions
Explain it in your own words, then open the answer.
Does a branch create a new project folder?
No. It is a named reference within repository history.
Which branch receives git merge update-heading?
The currently checked-out branch.
Does switching branches upload anything?
No. Switching is local.
When can a merge fast-forward?
When the current branch can advance along the other branch’s history without divergent commits.
Should I choose a local merge and a hosted review for the same change blindly?
No. Follow the project’s workflow and synchronize the result.