14 | Viewing Branches |
You need to be able to see what branches your repository has in it so you can switch between them. You can use a visualization tool such as gitk[13] or GitX.[14] You can use git branch to get the same information, however.
You can view local, remote, or all branches depending on which parameters you pass to git branch. Calling git branch by itself shows you your local branches. You can add either the -r parameter or the -a parameter to view only the remote branches or all the branches, respectively.
Your current branch always has an asterisk before it in the output from git branch. It’s colored green if you turned on color output (see Task 2, Configuring Git). Likewise, remote branches are colored red if colors are on.
One gotcha with remote branches is that the output from git branch -a shows their name with a remotes/ prefix. git branch -r doesn’t. You can use either name with commands that require a branch name.
It’s useful to see what branches have or have not been merged into the current branch. You can see that by using the --merged and --no-merged parameters.
It’s also useful to be able to find out which branches contain a particular commit. For example, you can track which branches contain a commit that has a known bug in it by using the --contains parameter.
What To Do... |
| prompt> git branch |
| master |
| new |
| newer |
| * newest |
| prompt> git branch -r |
| origin/master |
| prompt> git branch -a |
| master |
| new |
| newer |
| * newest |
| remotes/origin/master |
| prompt> git branch --merged |
| prompt> git branch --no-merged |
| prompt> git branch --contains <commit id> |
Task 13, Creating and Switching Branches
Task 15, Merging Commits Between Branches
18.117.93.0