On this page
- What is a branch in Git, really?
- How do you list the branches you have?
- How do you create and switch branches?
- How do you work on a branch someone else pushed?
- How do you merge a finished branch?
- How do you handle merge conflicts?
- How do you share a branch and delete it afterwards?
- How do you work on two branches at once on one server?
- Which branching layout fits a small project?
- Questions
What is a branch in Git, really?
A branch is nothing more than a movable name that points at one commit. When you commit, the current branch name moves forward to the new commit; the other branches stay where they were. HEAD is the marker for the branch you are on right now.
Because a branch is only a small pointer file, creating one is instant even in a repository with years of history. That makes it cheap to give every fix, experiment or feature its own line of work, keep main deployable, and merge only what is ready. On a server, a common pattern is a main branch that production checks out and a staging branch that a test copy follows. You need Git configured first, as described in install and configure Git.
How do you list the branches you have?
git branch lists local branches and stars the current one; add flags to see remote branches and the last commit on each.
git branch # local branches
git branch -r # remote-tracking branches such as origin/main
git branch -a # both
git branch -vv # last commit plus the upstream each branch tracks
git branch --show-currentRemote-tracking branches like origin/main are read-only bookmarks that record where the remote was at your last fetch. Run git fetch to refresh them; with fetch.prune enabled, branches deleted on the remote also disappear from this list.
How do you create and switch branches?
Use git switch, the dedicated command for changing branches since Git 2.23. It replaces the branch half of the older and overloaded git checkout.
git switch -c feature-login # create from the current commit and switch
git switch -c hotfix-1.4 v1.4.0 # create from a tag or any commit
git switch main # go back
git switch - # jump to the previous branchUncommitted edits travel with you when you switch, as long as they do not clash with the target branch. If they would be overwritten, Git refuses; commit them first, or park them with git stash as shown in the undo guide. To rename the branch you are on, use git branch -m new-name.
How do you work on a branch someone else pushed?
Fetch, then switch to the branch by its short name; Git spots the matching origin branch and creates a local branch that tracks it.
git fetch origin
git switch feature-payments
git status # "Your branch is up to date with origin/feature-payments"From then on, git pull and git push on that branch talk to its remote twin. If two remotes have a branch with the same name, be explicit: git switch -c feature-payments --track upstream/feature-payments.
How do you merge a finished branch?
Switch to the branch that should receive the work, usually main, update it, and merge the feature branch into it.
git switch main
git pull --ff-only
git merge feature-loginTwo outcomes are possible:
- Fast-forward:
mainhad no new commits since the branch started, so Git simply moves themainpointer forward. No extra commit is created. - Three-way merge: both sides moved, so Git writes a merge commit with two parents that joins them.
Some teams want a merge commit every time, so the history shows where a feature began and ended: git merge --no-ff feature-login. Others want only straight lines: git merge --ff-only feature-login, which fails instead of merging when a fast-forward is not possible. git merge --squash feature-login followed by git commit folds the whole branch into one new commit.
How do you handle merge conflicts?
When both branches changed the same lines, Git pauses the merge, marks the files and waits for you to pick the final content.
git status # "both modified" entries
git diff # shows the conflicting hunks
git checkout --ours path/file # keep the version from main
git checkout --theirs path/file # or keep the version from the branch
git add path/file
git commitFor most conflicts you open the file, keep the right lines from each side and delete the <<<<<<<, ======= and >>>>>>> markers. Setting git config --global merge.conflictStyle zdiff3 also shows the original text from the common ancestor, which makes the intended change much easier to see. git merge --abort cancels everything and returns to where you were.
How do you share a branch and delete it afterwards?
Push a new branch with -u so it gets an upstream, and delete it in both places once its work is merged.
git push -u origin feature-login
# after the merge
git branch -d feature-login # refuses if not merged
git push origin --delete feature-login
git fetch --prune-d protects you: it only deletes a branch whose commits are already reachable from your current branch or its upstream. -D deletes regardless, which throws away unmerged commits (they stay recoverable through the reflog for a while). To find branches that are safe to clean up, list the merged ones: git branch --merged main.
How do you work on two branches at once on one server?
Use git worktree: it checks out a second branch into another folder that shares the same repository data, so you avoid a full second clone.
git worktree add ../app-hotfix hotfix-1.4
cd ../app-hotfix # work, commit, push as usual
cd ../app
git worktree list
git worktree remove ../app-hotfixThis suits a VPS that runs a production checkout and a staging checkout side by side, or when you need to test a fix without disturbing the build that is already running. A branch can only be checked out in one worktree at a time.
Which branching layout fits a small project?
Keep it simple: one long-lived main that always deploys, plus short-lived branches named for their purpose, such as fix-session-timeout or feature-invoices. Merge them within days, not months, so conflicts stay small.
Add a staging branch only when you really run a separate test copy. Protecting main from direct pushes is a setting on your Git host; on a self-hosted bare repository you can enforce it with an update hook, as shown in the deploy hooks guide. When you want history without merge bubbles, the rebase guide shows how to replay a branch on top of main before merging.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Create, switch, merge and delete branches
Is git checkout still valid for branches?
Yes, git checkout branch and git checkout -b new still work and will keep working. git switch and git restore simply split its two jobs into clearer commands.
What is a detached HEAD?
It means you checked out a commit or tag rather than a branch. You can look around and even commit, but new commits belong to no branch. Run git switch -c new-branch to keep them.
How do I see which commits a branch adds?
Run git log --oneline main..feature-login. It lists the commits on the feature branch that main does not have yet; git diff main...feature-login shows the combined change.
I deleted a branch with -D by mistake. Can I get it back?
Usually. Find the last commit in git reflog and recreate the branch with git branch feature-login COMMIT. Unreachable commits are pruned only after the reflog expiry, 30 days by default.
Does the default branch have to be called main?
No. Git uses the name in init.defaultBranch for new repositories. Older projects often use master; rename with git branch -m master main and update the default on your Git host.