6. Git Tools
3.3 Git Branching - Branch Management
Now that you’ve created, merged, and deleted some branches, let’s look at some branch-management tools that will come in handy when you begin using branches all the time.
git branch command does more than just create and delete branches. If you run it with no arguments, you get a simple listing of your current branches:
$ git branch iss53 * master testing
* character that prefixes the
master branch: it indicates the branch that you currently have checked out. This means that if you commit at this point, the
master branch will be moved forward with your new work. To see the last commit on each branch, you can run
git branch –v:
Another useful option to figure out what state your branches are in is to filter this list to branches that you have or have not yet merged into the branch you’re currently on. The useful
--no-merged options have been available in Git since version 1.5.6 for this purpose. To see which branches are already merged into the branch you’re on, you can run
git branch --merged:
$ git branch --merged iss53 * master
Because you already merged in
iss53 earlier, you see it in your list. Branches on this list without the
* in front of them are generally fine to delete with
git branch -d; you’ve already incorporated their work into another branch, so you’re not going to lose anything.
To see all the branches that contain work you haven’t yet merged in, you can run
git branch --no-merged:
$ git branch --no-merged testing
This shows your other branch. Because it contains work that isn’t merged in yet, trying to delete it with
git branch -d will fail:
$ git branch -d testing error: The branch 'testing' is not an ancestor of your current HEAD. If you are sure you want to delete it, run 'git branch -D testing'.
If you really do want to delete the branch and lose that work, you can force it with
-D, as the helpful message points out.