Git Branches and Merging: Work on Ideas Safely

Git and GitHub Essentials: Version Control for Beginners · Git Basics · 6 min read · Updated

A branch and a merge

  1. The main branch holds the stable version of the project.
  2. You create a new branch to try an idea without touching main.
  3. You commit your work on the branch in small steps. Main stays unchanged.
  4. When the idea works, you merge the branch into main. If it fails, you simply delete the branch.

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 branch lists branches, and the * marks the current one.
  • git switch -c contact-page creates the branch contact-page and moves to it. Older tutorials use git checkout -b, which does the same job.
  • git switch main moves back to main.
  • git merge contact-page brings 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-page deletes 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

  1. Start from an up-to-date main.
  2. Create a branch with a clear name, such as fix-login-typo or add-contact-page.
  3. Commit your work on the branch in small steps.
  4. Switch back to main and merge the branch, or open a pull request on GitHub (lesson 7).
  5. 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-link is better than asha-work.
  • Many teams add a prefix such as feature/ or fix/. Follow your team's rule.
  • Keep main for stable work that others can rely on.

Your task

  1. Create a practice repo with a file and one commit. Create a branch feature-one, add a file and commit it.
  2. Switch back to main and check that the new file is not there. Merge the branch. Is it a fast-forward?
  3. 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.
  4. Run git log --oneline --graph --all and draw the result on paper.

How to check it worked

  • The new file is missing on main before the merge and present after it.
  • Your graph shows two lines joining in a merge commit for the second branch.
  • git branch lists only main after you delete the merged branches.

Frequently asked questions

What is a fast-forward merge?

When the target branch has no new commits, Git just moves its label forward to the other branch. No extra merge commit is made.

Is it safe to delete a branch after merging?

Yes. The commits are now part of the target branch. Git also refuses to delete an unmerged branch with -d, so you cannot lose work by accident.