Guides · git
How to create and switch branches in git
By Lalla Asmaa Alaoui, founder of Mel · Updated
Run git switch -c my-branch to create a new branch and move onto it in one step. To move to a branch that already exists, run git switch my-branch.
A branch is a separate line of work. You can try an idea, or let an AI agent work on a task, without touching main until you are ready. git switch arrived in git 2.23. On older versions, use git checkout -b my-branch instead; it does the same thing. The commands are identical on Mac, Linux and Windows.
Steps
- See where you are.
git branchThe branch marked with * is the one you are on. Add -a to include branches on the remote too.
- Start from an up-to-date main. A new branch starts from wherever you are now, so update first.
git switch main
git pullYour project may call its main branch master or something else. git branch shows the name.
- Create the branch and switch to it.
git switch -c fix-login-timeoutPick a short name that says what the work is. Use hyphens, not spaces. You can also start a branch from a specific branch without switching first:
git switch -c fix-login-timeout main- Switch between branches.
git switch main
git switch fix-login-timeoutgit switch - jumps back to the branch you were on before. If you have uncommitted changes that would clash with the other branch, git refuses to switch. Commit them, or put them aside with git stash and bring them back later with git stash pop.
- Push the branch so others can see it.
git push -u origin fix-login-timeout-u links your local branch to the remote one, so later you can type just git push and git pull.
- Switch to a branch a teammate pushed.
git fetch
git switch their-branchIf a remote branch of that name exists and you do not have it locally, git creates a local copy that tracks it.
- Delete a branch when you are done.
git branch -d fix-login-timeout-d refuses if the branch has work that is not merged. Use -D to delete it anyway. To delete the remote copy: git push origin --delete fix-login-timeout.
Two branches at once: worktrees
Switching branches changes the files in your folder. If you want two branches checked out side by side, for example to run two tasks at the same time, use a worktree:
git worktree add ../myapp-fix-login -b fix-login-timeoutThis creates a second folder with its own branch, sharing the same repository. git worktree list shows them, and git worktree remove ../myapp-fix-login cleans one up. On Windows, the same commands work; ..\myapp-fix-login also works as a path in Command Prompt.
Why a branch is the safe start for agent work
When an AI agent works on a task, give it its own branch. If the result is good, you merge it. If not, you switch back to main and delete the branch, and nothing on main ever changed. It also makes the agent's work easy to review: git diff main...HEAD shows everything the branch added.
Doing it with Mel
Mel runs your own shell, so every command above works at the prompt. Above the prompt, the git chip shows your current branch and a live count of changed files. Click it to open a branch picker and switch without typing.
You can also ask the agent: press ctrl+enter (cmd+enter on macOS) and write "create a branch called fix-login-timeout from main and switch to it". With Mel's approvals, the git commands appear on an approval card before they run.
For parallel work, Mel can create worktrees for you. Track your repo once from the + menu ("Track this repo"), then choose + → Worktree for that repo. Mel creates a checkout on a new branch, so you can run one agent per worktree tab and keep their changes apart.
FAQ
What is the difference between git switch and git checkout? git switch only changes branches. git checkout does that and also restores files, which made it confusing. Both work; git switch needs git 2.23 or newer.
How do I create a branch without switching to it? Run git branch my-branch. You stay on your current branch.
How do I rename a branch? Switch to it and run git branch -m new-name. If it was pushed, push the new name and delete the old one on the remote.
Why will git not let me switch branches? You have uncommitted changes that would be overwritten. Commit them, or run git stash, switch, and run git stash pop when you come back.
Should an AI agent work on its own branch? Yes. A separate branch, or a separate worktree, keeps the agent's changes away from main until you have reviewed them.
- How to run several AI agents at once
- How to fix a merge conflict in git
- How to undo the last git commit
- What is an agentic development environment?
Facts checked on Oct 25, 2026.
Git is a trademark of its owner. Mel is not affiliated with or endorsed by Git.
Mel – the agentic terminal · openmel.dev. A multi-agent, multi-model AI terminal for Mac, Windows, Linux and VS Code. Free with your own API key.

Lalla is a hands-on tech founder, agentic infrastructure architect and the vision behind Mel.
Follow on LinkedIn