If you've ever opened Android Studio, cloned a sample project from GitHub, or tried to contribute to an open-source app, you've bumped into branches. They sound mysterious, but the core idea is simple: a branch is just a label that follows a line of commits. According to initialcommit.com, that label is really a "ref" that always points to the tip of whatever chain of work you're on right now.
Most of the confusion we see from readers who are new to coding comes from the vocabulary: HEAD, origin, main, master, remote tracking. The good news is you don't need to learn it all at once. This guide walks through the everyday Git branch commands you'll actually use, with the Android dev workflow in mind.
Quick take:
- A Git branch is a lightweight label that points to a specific commit, not a separate copy of your project.
- Every repo with at least one commit has a default branch, historically called master and now commonly renamed to main.
- HEAD is Git's pointer to whatever commit is currently checked out in your working directory.
- You create branches with `git branch <name>`, switch with `git checkout <name>`, or do both with `git checkout -b <name>`.
- Local branches sync to remotes like GitHub through `git push` and `git pull`, which set up remote tracking branches automatically.
What a Branch Actually Is
Here's the mental model worth locking in. When you make a commit, Git stores a snapshot and gives it an ID. The very first commit in a repo has no parent; every commit after that references the one before it. A branch is just the name of the latest commit in a line of work. Move forward, and Git quietly re-points the label. That's it.
Because branches are cheap, the docs note you're encouraged to make them often. That same idea is why tools like GitHub let you spin up a branch in two clicks from the web UI.
The Commands You'll Use Every Day
For most Android projects, you'll live in about six commands.
| Task | Command |
|---|---|
| Create a branch | `git branch <name>` |
| Switch to a branch | `git checkout <name>` |
| Create and switch in one go | `git checkout -b <name>` |
| See where you are | `git status` |
| List branches | `git branch` or `git branch -a` |
| Push to remote | `git push -u origin <name>` |
The `-u` flag, short for `–set-upstream`, tells Git "this local branch tracks the remote one." After the first push, plain `git push` works.
To see what branch you're on right now, `git status` does the job and prints a line like "On branch main" along with whether you're ahead or behind. Add `-a` to `git branch` and you also see the remote tracking branches that Git created for you automatically.
The Shift From master to main
If you've been away from Git for a couple of years, you'll notice many repos now use `main` instead of `master`. That's a deliberate naming change pushed by major hosts to use more inclusive defaults. The reference confirms that today, a fresh repo can use either name, but `main` is the common modern choice.
To rename your local default branch, run `git branch -m master main`. To push that rename upstream, the cleanest sequence is:
- `git push -u origin main` to publish the renamed branch.
- `git push origin –delete master` to remove the old one on the remote.
If that second command refuses, the default branch is still set to master in your GitHub or BitBucket settings. Flip the default in the repo's settings page first, then re-run the delete.
Our Analysis
Here's what we think matters. For most Android users reading this site, branches are a tool you'll meet through Android Studio's VCS menu or through a tutorial project you cloned. You probably don't need to memorize the internals. You do need to understand the workflow: each feature or experiment lives on its own branch, you push to a remote so your work is backed up, and you merge back when it's ready.
The trade-off is straightforward. Branches protect you — your half-finished feature won't break the main build. But they also pile up. Old feature branches, stale experiments, and renamed defaults tend to clutter the list after a year. A quick monthly cleanup saves headaches.
Watch for these next: more Android tooling is moving to trunk-based development with short-lived branches, and GitHub continues to push `main` as the new default. If you maintain sample apps or share open-source code, expect to be nudged toward the new naming over time.
A Worked Example: Say You're Forking an Android Sample
Imagine you forked the nowinandroid sample on GitHub and want to add a dark-mode toggle. Here's how the branching looks in practice.
You clone your fork, open it in Android Studio, and confirm you're on `main` with `git status`. Then you run `git checkout -b feature/dark-mode-toggle` to create a new branch and switch to it in one step. You make your changes, commit them, and push with `git push -u origin feature/dark-mode-toggle`. From there, you open a pull request on GitHub. Reviewers see only your toggle, not your unfinished experiments from last week.
That's the whole branch story, really: isolate, push, request, merge.
What to Watch Next
If you only learn one thing this week, make it `git checkout -b`. It will carry you through 90% of the work you do as a hobbyist Android developer. After that, get comfortable renaming master to main, and don't be surprised when your favorite projects default to it without asking. Branches aren't a feature to fear — they're a scratchpad you get for free.
Key takeaways
- A branch is a label pointing to a commit, not a duplicate of your project.
- Use `git checkout -b ` to create and switch in one step.
- Rename `master` to `main` with `git branch -m master main`, then update your remote default.
- The `-u` flag on `git push` links your local branch to its remote counterpart so future pushes are one word.
- Clean up old branches monthly; they pile up faster than you think.
Frequently asked questions
What's the difference between a local branch and a remote tracking branch?
A local branch is the label you work on in your own repo. A remote tracking branch is a read-only reference Git creates locally to mirror the state of a branch on a remote like GitHub. You never merge into them directly — `git pull` and `git push` handle that for you.
Do I need to use `main` instead of `master`?
Not strictly. Older repos and tools still default to `master`. New repos on GitHub default to `main`, and most teams have moved over. Renaming is cosmetic; the tool works the same either way.
How do I see all the branches in a repo?
Run `git branch` for local branches only, or `git branch -a` to include remote tracking branches. For a more visual view, your Git client or GitHub's branches page lists them too.
Can I delete a branch and lose my work?
You can only delete a branch safely if its commits have been merged into another branch or pushed to a remote. Git will warn you before deleting unmerged work, and you can force it with `-D` if you're sure — but that's where accidental data loss happens.
What does HEAD mean in Git?
HEAD is Git's pointer to whatever commit is currently checked out in your working directory. When you switch branches, HEAD moves with you. Detached HEAD is the rare state where HEAD points directly at a commit instead of a branch label.