A branch is a separate line of work. You make one when you want to try a new feature or fix a bug without touching the stable version. If the idea works, you merge it back. If it does not, you throw the branch away, and nothing else is harmed. Branches are cheap in Git: creating one takes a moment, so professionals use them for almost everything. This lesson uses a tiny website project and a real recorded session.
$ git branch
* main
$ git switch -c contact-page
Switched to a new branch 'contact-page'
$ git add contact.txt && git commit -m "Add contact page"
[contact-page c102ae8] Add contact page
1 file changed, 1 insertion(+)
create mode 100644 contact.txt
$ git switch main
Switched to branch 'main'
$ ls
index.txt
$ git merge contact-page
Updating ec7feed..c102ae8
Fast-forward
contact.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 contact.txt
$ ls
contact.txt
index.txt
$ git branch -d contact-page
Deleted branch contact-page (was c102ae8).
$ git switch -c about-page
Switched to a new branch 'about-page'
$ git add about.txt && git commit -m "Add about page"
[about-page 38e1b3d] Add about page
1 file changed, 1 insertion(+)
create mode 100644 about.txt
$ git switch main
Switched to branch 'main'
$ git add index.txt && git commit -m "Add welcome line"
[main f1b5255] Add welcome line
1 file changed, 1 insertion(+)
$ git merge --no-edit about-page
Merge made by the 'ort' strategy.
about.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 about.txt
$ git log --oneline --graph --all
* 8c59a4f Merge branch 'about-page'
|\
| * 38e1b3d Add about page
* | f1b5255 Add welcome line
|/
* c102ae8 Add contact page
* ec7feed Add home page
$ git branch -d about-page
Deleted branch about-page (was 38e1b3d).
Understanding branches
A branch is just a movable label that points to a commit. The branch you are on is the one that moves forward when you commit. A special pointer called HEAD shows which branch you are currently on. When you switch branches, Git changes the files in your folder to match that branch, so contact.txt was missing on main before the merge and appeared after it.
The commands
git branchlists branches, and the*marks the current one.git switch -c contact-pagecreates the branchcontact-pageand moves to it. Older tutorials usegit checkout -b, which does the same job.git switch mainmoves back tomain.git merge contact-pagebrings the work of that branch into the one you are on. Always switch to the branch that should receive the changes first.git branch -d contact-pagedeletes a branch that is already merged. Git refuses if it is not merged, to protect your work.
Two kinds of merge
In the first merge, main had not changed since we created contact-page, so Git simply moved the main label forward. The output says Fast-forward. There is no extra commit, and the history stays a straight line.
In the second case, both main and about-page had new commits. Git combined them with a new merge commit, and said Merge made by the 'ort' strategy. The graph makes this visible.
* 8c59a4f Merge branch 'about-page'
|\
| * 38e1b3d Add about page
* | f1b5255 Add welcome line
|/
* c102ae8 Add contact page
* ec7feed Add home page
Read the graph from the bottom up. Two lines run in parallel, and then join at the merge commit on top. git log --oneline --graph --all gives you this picture, and it is a great way to understand where you are.
A good branch workflow
- Start from an up-to-date
main. - Create a branch with a clear name, such as
fix-login-typooradd-contact-page. - Commit your work on the branch in small steps.
- Switch back to
mainand merge the branch, or open a pull request on GitHub (lesson 7). - Delete the branch when it is merged.
Before you switch branches
If you have uncommitted changes, Git may refuse to switch, or it may carry them along. The cleanest habit is to commit, or to put the work aside with git stash (lesson 9), before switching. Run git status first. It takes two seconds and avoids a lot of confusion.
Naming branches
- Use lowercase words with hyphens:
add-search-box. - Describe the purpose, and not yourself:
fix-footer-linkis better thanasha-work. - Many teams add a prefix such as
feature/orfix/. Follow your team's rule. - Keep
mainfor stable work that others can rely on.
Your task
- Create a practice repo with a file and one commit. Create a branch
feature-one, add a file and commit it. - Switch back to
mainand check that the new file is not there. Merge the branch. Is it a fast-forward? - Create a second branch, commit on it. Then switch to
main, make a different commit, and merge the second branch to see a merge commit. - Run
git log --oneline --graph --alland draw the result on paper.
How to check it worked
- The new file is missing on
mainbefore the merge and present after it. - Your graph shows two lines joining in a merge commit for the second branch.
git branchlists onlymainafter you delete the merged branches.