Git Course 0%

Branch management

Beginner IntelliJ UI ≈ 9 min

What you will learn

  • Every branch operation and where it lives
  • What Smart Checkout does, and why it is not magic
  • How to compare a branch with the one you are on

After this lesson you can

  • I can run a branch's whole life from the IDE

Why this matters

Branching is the operation you do most often after committing, and IntelliJ's branch menu offers more than any other tool in this course. Knowing what each entry does, especially Smart Checkout, means the extra options help rather than confuse.

The VCS widget

The window header shows the current branch. Clicking it opens the branch popup, with local branches, remote branches, recent ones, and favourites marked with a star.

Entry Does
New Branch Creates from the current branch and switches to it
New Branch from Selected Creates from another branch, local or remote
Checkout Switches to the selected branch
Checkout and Rebase onto Current Switches, then rebases it onto where you were
Compare with Current Shows the commits and files that differ
Show Diff with Working Tree Compares that branch with your files right now
Rename Renames a local branch only
Delete Deletes a local branch, with an undo link in the notification
Push / Update On a specific branch, without switching to it

The fetch icon in the widget refreshes the remote branch list, which matters before checking out a branch a colleague has just pushed.

Smart Checkout

Switching branches with uncommitted changes is where Git refuses if the changes touch files that differ (lesson 6.3). IntelliJ offers Smart Checkout, which:

  1. Shelves your uncommitted changes.
  2. Switches to the other branch.
  3. Unshelves them there.

It is convenient and worth understanding rather than accepting blindly. Two consequences: your changes are now on a different branch, which is occasionally not what you wanted; and if the unshelve conflicts, you get a conflict on a branch you have just arrived on. The alternatives IntelliJ offers in the same dialog are Force Checkout, which discards the changes, and Don't Checkout.

The habit that avoids the question: commit or shelve before switching.

Publishing and tracking

A new branch is local until pushed. Git → Push (⇧⌘K) publishes it and sets the upstream, so afterwards the widget shows incoming and outgoing counts for it.

The branch popup marks tracked branches, and Update on a branch fetches and fast-forwards it without switching, which is a genuinely useful trick for keeping main current while you work elsewhere.

Comparing branches

Compare with Current opens a two-pane view: the commits that are on one branch and not the other, in both directions, and the file diffs for any commit you select.

This answers "has my branch got the latest main?" and "what would this merge bring in?" without merging anything, which is worth doing before every merge request.

Merging and rebasing

Task Where
Merge another branch into this one Branch popup → the branch → Merge into Current
Rebase this branch onto another Branch popup → the branch → Rebase Current onto Selected
Abort either The banner in the Git tool window, or Git → Abort

Conflicts open the merge dialog (lesson 16.7), which is the part IntelliJ does better than anything else here.

How to do it

Terminal
$ git switch -c docs/12-windows-install-steps
$ git switch main
$ git push -u origin docs/12-windows-install-steps
$ git branch -d docs/12-windows-install-steps
$ git log --oneline main..docs/12-windows-install-steps

The last one is Compare with Current in command form.

Common mistakes

  • Accepting Smart Checkout without knowing it moves your changes to the other branch.
  • Choosing Force Checkout, which discards them.
  • Renaming a branch and expecting the remote to follow. Rename is local; the remote branch keeps its name.
  • Not fetching first, so a colleague's new branch is not in the list.
  • Deleting an unmerged branch past the warning (lesson 12.9).

Try it yourself

Goal: use the branch popup for a whole branch life, including Smart Checkout.

  1. Create docs/intellij-branch-demo with New Branch.
  2. Make a change and leave it uncommitted, then switch to main and accept Smart Checkout.
  3. Look at the Shelf tab during the switch, then switch back and confirm your change returned.
  4. Commit, push with ⇧⌘K, and read the commit list in the dialog.
  5. Use Compare with Current from main to see what your branch would bring in, then merge and delete it.

Expected result: a branch created, moved between, published, compared, merged and deleted, with one observation of what Smart Checkout does behind the scenes.

Show solution

Step 3 is the point. Smart Checkout is a shelve, a checkout and an unshelve, and seeing the shelf entry appear turns it from magic into three steps you could have done yourself. That matters the day the unshelve conflicts, because then you know exactly where your work is.

Check yourself

1. What does Smart Checkout do when you switch branches with uncommitted changes?
2. Rename on a branch in the VCS widget affects:
3. Which action answers "what would merging this branch bring in?" without merging?

Key terms

Branch main Stash Push