Programming and IT

Git branches

A Git branch is a movable pointer to a commit, not a copy of your files. That is why creating one costs nothing, and the git branch command is what you use to list, create, rename and delete branches.

Updated
In this article

A branch is a pointer#

Inside the repository a branch is stored as a single line holding a commit hash. When you make a new commit, the pointer of the current branch moves to it — and only that pointer. A separate pointer called HEAD remembers which branch you are on right now.

Everything else follows from this. Creating a branch is instant, no matter how many files the project has. Ten branches do not take ten times the space. And deleting a branch does not delete commits: only the name disappears, the commits stay as long as something still refers to them.

See what you have#

git branch
  feature/login
* main
  hotfix-404

The asterisk marks the current branch. Useful variations of the listing:

Command What it shows
git branch -v the latest commit of every branch on one line
git branch -vv the same plus the tracked branch on the server
git branch -a both local and remote branches
git branch -r remote branches only (origin/…)
git branch --show-current just the current name — handy in scripts
git branch --merged branches already merged into the current one: candidates for deletion

Create, switch, delete#

git branch feature/export

The branch exists now, but you are still where you were. Usually you want to move to it straight away:

git switch -c feature/export
Switched to a new branch 'feature/export'

The switch and restore commands appeared in Git 2.23 to split up the job of the old checkout: it could both change branches and restore files, which made it easy to use in the wrong way. The old form git checkout -b feature/export still works — see git checkout for the details.

Deleting:

git branch -d feature/export
Deleted branch feature/export (was 9d31c07).

Lowercase -d refuses to delete a branch whose work has not been merged anywhere — error: the branch 'feature/export' is not fully merged. That is a safeguard, not a bug: make sure you really do not need those commits and repeat with uppercase -D. This does not touch the branch on the server; you delete that one with a push — see git push.

Detached HEAD#

If you switch not to a branch but to a specific commit or tag, Git says:

Note: switching to '4f1c2ab'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them...

Nothing bad has happened: you are simply looking at an older state of the project, and HEAD points straight at a commit instead of at a branch. There is only one danger — commits you make in this state do not belong to any branch, and after you switch away they will have no name.

How you get out depends on what you did. If you made no commits, go back to the branch you were on:

git switch -

If you made commits and you need them, give them a name right where you are:

git switch -c experiment-sort

If you already left and the commits were left without a name, find their hash in git reflog and create a branch from it: git branch rescue 3c9f011.

Renaming and keeping the list tidy#

git branch -m old-name new-name

Without the first name, the current branch gets renamed — this is how people change master to main in an old repository. After a rename the branch on the server keeps its old name, so you have to push the new one and delete the old one there.

Every couple of weeks it pays to clean up the list: git branch --merged shows merged branches, and git fetch --prune removes remote branches from your local list once they no longer exist on the server. Keep names descriptive — feature/export-csv, fix/login-500 — and the branch list reads like a to-do list rather than a jumble of letters. The other everyday commands are collected in the Git commands cheat sheet.

Step-by-step plan

  1. Check where you aregit status or git branch --show-current before any switch.
  2. Create a branch from the right placegit switch -c name; give a starting point if needed: git switch -c name main.
  3. Work and commitCommits move only this branch's pointer; main stays where it was.
  4. Merge the workGo back to main and merge the branch; after that git branch --merged lists it.
  5. Delete the finished namegit branch -d name locally and git push origin --delete name on the server.

Start learning this in your own space

The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.

Start the plan

Check yourself

1.Which command creates a branch called feature and switches to it at once?

2.Git said “You are in 'detached HEAD' state” and you have already made commits. What keeps them?

3.Which command lists the branches already merged into the current one?

Sources

Was this helpful?

More articles

Programming and IT git merge Merging brings work from one branch into another. git merge takes the branch you name and joins its history with the current one — sometimes by just moving a pointer, sometimes by creating a separate commit, and sometimes by stopping and asking for help. Programming and IT git checkout Historically `git checkout` does several unrelated things at once: it moves your working copy to another branch, creates a branch and pulls a single file out of a commit. Because it was so overloaded, Git 2.23 added two separate commands — `switch` and `restore`. Here are both sides: the old way and its modern replacement. Programming and IT git clone git clone takes an entire repository from a server — with all its history, branches and tags — and puts a copy into a new folder on your computer. It is how you start working on any existing project, yours or someone else's. Programming and IT git push Until you run git push, your commits exist only on your own disk. The command sends them to a remote repository and moves the branch pointer there — which is also why it is the command that most often refuses, when someone else got their work in first. Programming and IT How to undo a commit The question "how do I undo a commit" actually hides four different ones: fix the last commit, take it back and redo it, erase several, or cancel an old commit in the middle of history. The answer depends above all on one thing — whether anyone besides you has seen that commit. Programming and IT git pull git pull brings other people's commits from a remote repository and immediately merges them into your current branch. It is two operations in a row, and almost every problem with pull comes from the second one — how it joins the two histories.

More solutions