Raw
1 git-branch(1)
2 =============
3
4 NAME
5 ----
6 git-branch - List, create, or delete branches
7
8 SYNOPSIS
9 --------
10 [synopsis]
11 git branch [--color[=<when>] | --no-color] [--show-current]
12 [-v [--abbrev=<n> | --no-abbrev]]
13 [--column[=<options>] | --no-column] [--sort=<key>]
14 [--merged [<commit>]] [--no-merged [<commit>]]
15 [--contains [<commit>]] [--no-contains [<commit>]]
16 [(--forked <branch>)...]
17 [--points-at <object>] [--format=<format>]
18 [(-r|--remotes) | (-a|--all)]
19 [--list] [<pattern>...]
20 git branch [--track[=(direct|inherit)] | --no-track] [-f]
21 [--recurse-submodules] <branch-name> [<start-point>]
22 git branch (--set-upstream-to=<upstream>|-u <upstream>) [<branch-name>]
23 git branch --unset-upstream [<branch-name>]
24 git branch (-m|-M) [<old-branch>] <new-branch>
25 git branch (-c|-C) [<old-branch>] <new-branch>
26 git branch (-d|-D) [-r] <branch-name>...
27 git branch --edit-description [<branch-name>]
28 git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]
29
30 DESCRIPTION
31 -----------
32
33 If `--list` is given, or if there are no non-option arguments, existing
34 branches are listed; the current branch will be highlighted in green and
35 marked with an asterisk. Any branches checked out in linked worktrees will
36 be highlighted in cyan and marked with a plus sign. Option `-r` causes the
37 remote-tracking branches to be listed,
38 and option `-a` shows both local and remote branches.
39
40 If a `<pattern>`
41 is given, it is used as a shell wildcard to restrict the output to
42 matching branches. If multiple patterns are given, a branch is shown if
43 it matches any of the patterns.
44
45 Note that when providing a
46 `<pattern>`, you must use `--list`; otherwise the command may be interpreted
47 as branch creation.
48
49 With `--contains`, shows only the branches that contain the named commit
50 (in other words, the branches whose tip commits are descendants of the
51 named commit), `--no-contains` inverts it. With `--merged`, only branches
52 merged into the named commit (i.e. the branches whose tip commits are
53 reachable from the named commit) will be listed. With `--no-merged` only
54 branches not merged into the named commit will be listed. If the _<commit>_
55 argument is missing it defaults to `HEAD` (i.e. the tip of the current
56 branch). With `--forked`, only branches whose configured upstream matches
57 the given branch or pattern will be listed.
58
59 The command's second form creates a new branch head named _<branch-name>_
60 which points to the current `HEAD`, or _<start-point>_ if given. As a
61 special case, for _<start-point>_, you may use `<rev-A>...<rev-B>` as a
62 shortcut for the merge base of _<rev-A>_ and _<rev-B>_ if there is exactly
63 one merge base. You can leave out at most one of _<rev-A>_ and _<rev-B>_,
64 in which case it defaults to `HEAD`.
65
66 Note that this will create the new branch, but it will not switch the
67 working tree to it; use `git switch <new-branch>` to switch to the
68 new branch.
69
70 When a local branch is started off a remote-tracking branch, Git sets up the
71 branch (specifically the `branch.<name>.remote` and `branch.<name>.merge`
72 configuration entries) so that `git pull` will appropriately merge from
73 the remote-tracking branch. This behavior may be changed via the global
74 `branch.autoSetupMerge` configuration flag. That setting can be
75 overridden by using the `--track` and `--no-track` options, and
76 changed later using `git branch --set-upstream-to`.
77
78 With a `-m` or `-M` option, _<old-branch>_ will be renamed to _<new-branch>_.
79 If _<old-branch>_ had a corresponding reflog, it is renamed to match
80 _<new-branch>_, and a reflog entry is created to remember the branch
81 renaming. If _<new-branch>_ exists, `-M` must be used to force the rename
82 to happen.
83
84 The `-c` and `-C` options have the exact same semantics as `-m` and
85 `-M`, except instead of the branch being renamed, it will be copied to a
86 new name, along with its config and reflog.
87
88 With a `-d` or `-D` option, _<branch-name>_ will be deleted. You may
89 specify more than one branch for deletion. If the branch currently
90 has a reflog then the reflog will also be deleted.
91
92 Use `-r` together with `-d` to delete remote-tracking branches. Note, that it
93 only makes sense to delete remote-tracking branches if they no longer exist
94 in the remote repository or if `git fetch` was configured not to fetch
95 them again. See also the `prune` subcommand of linkgit:git-remote[1] for a
96 way to clean up all obsolete remote-tracking branches.
97
98
99 OPTIONS
100 -------
101 `-d`::
102 `--delete`::
103 Delete a branch. The branch must be fully merged in its
104 upstream branch, or in `HEAD` if no upstream was set with
105 `--track` or `--set-upstream-to`.
106
107 `-D`::
108 Shortcut for `--delete --force`.
109
110 `--create-reflog`::
111 Create the branch's reflog. This activates recording of
112 all changes made to the branch ref, enabling use of date
113 based sha1 expressions such as `<branch-name>@{yesterday}`.
114 Note that in non-bare repositories, reflogs are usually
115 enabled by default by the `core.logAllRefUpdates` config option.
116 The negated form `--no-create-reflog` only overrides an earlier
117 `--create-reflog`, but currently does not negate the setting of
118 `core.logAllRefUpdates`.
119
120 `-f`::
121 `--force`::
122 Reset _<branch-name>_ to _<start-point>_, even if _<branch-name>_ exists
123 already. Without `-f`, `git branch` refuses to change an existing branch.
124 In combination with `-d` (or `--delete`), allow deleting the
125 branch irrespective of its merged status, or whether it even
126 points to a valid commit. In combination with
127 `-m` (or `--move`), allow renaming the branch even if the new
128 branch name already exists, the same applies for `-c` (or `--copy`).
129 +
130 Note that `git branch -f <branch-name> [<start-point>]`, even with `-f`,
131 refuses to change an existing branch _<branch-name>_ that is checked out
132 in another worktree linked to the same repository.
133
134 `-m`::
135 `--move`::
136 Move/rename a branch, together with its config and reflog.
137
138 `-M`::
139 Shortcut for `--move --force`.
140
141 `-c`::
142 `--copy`::
143 Copy a branch, together with its config and reflog.
144
145 `-C`::
146 Shortcut for `--copy --force`.
147
148 `--color[=<when>]`::
149 Color branches to highlight current, local, and
150 remote-tracking branches.
151 The value must be `always` (the default), `never`, or `auto`.
152
153 `--no-color`::
154 Turn off branch colors, even when the configuration file gives the
155 default to color output.
156 Same as `--color=never`.
157
158 `-i`::
159 `--ignore-case`::
160 Sorting and filtering branches are case insensitive.
161
162 `--omit-empty`::
163 Do not print a newline after formatted refs where the format expands
164 to the empty string.
165
166 `--column[=<options>]`::
167 `--no-column`::
168 Display branch listing in columns. See configuration variable
169 `column.branch` for option syntax. `--column` and `--no-column`
170 without options are equivalent to `always` and `never` respectively.
171 +
172 This option is only applicable in non-verbose mode.
173
174 `--sort=<key>`::
175 Sort based on _<key>_. Prefix `-` to sort in descending
176 order of the value. You may use the `--sort=<key>` option
177 multiple times, in which case the last key becomes the primary
178 key. The keys supported are the same as those in linkgit:git-for-each-ref[1].
179 Sort order defaults to the value configured for the
180 `branch.sort` variable if it exists, or to sorting based on the
181 full refname (including `refs/...` prefix). This lists
182 detached `HEAD` (if present) first, then local branches and
183 finally remote-tracking branches. See linkgit:git-config[1].
184
185 `-r`::
186 `--remotes`::
187 List or delete (if used with `-d`) the remote-tracking branches.
188 Combine with `--list` to match the optional pattern(s).
189
190 `-a`::
191 `--all`::
192 List both remote-tracking branches and local branches.
193 Combine with `--list` to match optional pattern(s).
194
195 `-l`::
196 `--list`::
197 List branches. With optional `<pattern>...`, e.g. `git
198 branch --list 'maint-*'`, list only the branches that match
199 the pattern(s).
200
201 `--show-current`::
202 Print the name of the current branch. In detached `HEAD` state,
203 nothing is printed.
204
205 `--delete-merged <pattern>`::
206 Delete local branches whose configured upstream matches
207 _<pattern>_, but only when their tip is reachable from that
208 upstream. In other words, the work on the branch has already
209 landed on the upstream it tracks, so the local copy is no longer
210 needed. _<pattern>_ may name a ref, a remote (using the branch its
211 `HEAD` points at), or a shell-style glob. The option can be
212 repeated to widen the upstream match.
213 Optional _<branch-pattern>_ arguments limit which local branches
214 are considered, e.g. `git branch --delete-merged 'origin/*'
215 'topic-*'`.
216 +
217 A branch is not deleted when:
218 +
219 --
220 * its configured upstream ref no longer exists,
221 * it is checked out in any worktree,
222 * pushing it to the remote configured by
223 `branch.<name>.remote` would update its upstream, so it cannot be
224 distinguished from a branch that just looks fully merged right
225 after a pull; this is determined by the remote's configured push and
226 fetch refspecs,
227 * it is the local upstream of a branch that is not being deleted, or
228 * `branch.<name>.deleteMerged` is set to `false`.
229 --
230 +
231 When such a local upstream branch has its own upstream deleted by the
232 same operation, its upstream configuration is cleared.
233 +
234 A branch whose work has not yet been merged into its upstream is
235 silently skipped. Delete it with `git branch -D` if you want to
236 remove it anyway.
237
238 `--dry-run`::
239 With `--delete-merged`, print which branches would be
240 deleted and exit without touching any ref. Useful for
241 sanity-checking a wide pattern like `'origin/*'` before
242 committing to the deletion.
243
244 `-v`::
245 `-vv`::
246 `--verbose`::
247 When in list mode,
248 show sha1 and commit subject line for each head, along with
249 relationship to upstream branch (if any). If given twice, print
250 the path of the linked worktree (if any) and the name of the upstream
251 branch, as well (see also `git remote show <remote>`). Note that the
252 current worktree's `HEAD` will not have its path printed (it will always
253 be your current directory).
254
255 `-q`::
256 `--quiet`::
257 Be more quiet when creating or deleting a branch, suppressing
258 non-error messages.
259
260 `--abbrev=<n>`::
261 In the verbose listing that show the commit object name,
262 show the shortest prefix that is at least _<n>_ hexdigits
263 long that uniquely refers the object.
264 The default value is 7 and can be overridden by the `core.abbrev`
265 config option.
266
267 `--no-abbrev`::
268 Display the full sha1s in the output listing rather than abbreviating them.
269
270 `-t`::
271 `--track[=(direct|inherit)]`::
272 When creating a new branch, set up `branch.<name>.remote` and
273 `branch.<name>.merge` configuration entries to set "upstream" tracking
274 configuration for the new branch. This
275 configuration will tell git to show the relationship between the
276 two branches in `git status` and `git branch -v`. Furthermore,
277 it directs `git pull` without arguments to pull from the
278 upstream when the new branch is checked out.
279 +
280 The exact upstream branch is chosen depending on the optional argument:
281 `-t`, `--track`, or `--track=direct` means to use the start-point branch
282 itself as the upstream; `--track=inherit` means to copy the upstream
283 configuration of the start-point branch.
284 +
285 The `branch.autoSetupMerge` configuration variable specifies how `git switch`,
286 `git checkout` and `git branch` should behave when neither `--track` nor
287 `--no-track` are specified:
288 +
289 The default option, `true`, behaves as though `--track=direct`
290 were given whenever the start-point is a remote-tracking branch.
291 `false` behaves as if `--no-track` were given. `always` behaves as though
292 `--track=direct` were given. `inherit` behaves as though `--track=inherit`
293 were given. `simple` behaves as though `--track=direct` were given only when
294 the _<start-point>_ is a remote-tracking branch and the new branch has the same
295 name as the remote branch.
296 +
297 See linkgit:git-pull[1] and linkgit:git-config[1] for additional discussion on
298 how the `branch.<name>.remote` and `branch.<name>.merge` options are used.
299
300 `--no-track`::
301 Do not set up "upstream" configuration, even if the
302 `branch.autoSetupMerge` configuration variable is set.
303
304 `--recurse-submodules`::
305 THIS OPTION IS EXPERIMENTAL! Cause the current command to
306 recurse into submodules if `submodule.propagateBranches` is
307 enabled. See `submodule.propagateBranches` in
308 linkgit:git-config[1]. Currently, only branch creation is
309 supported.
310 +
311 When used in branch creation, a new branch _<branch-name>_ will be created
312 in the superproject and all of the submodules in the superproject's
313 _<start-point>_. In submodules, the branch will point to the submodule
314 commit in the superproject's _<start-point>_ but the branch's tracking
315 information will be set up based on the submodule's branches and remotes
316 e.g. `git branch --recurse-submodules topic origin/main` will create the
317 submodule branch "topic" that points to the submodule commit in the
318 superproject's "origin/main", but tracks the submodule's "origin/main".
319
320 `--set-upstream`::
321 As this option had confusing syntax, it is no longer supported.
322 Please use `--track` or `--set-upstream-to` instead.
323
324 `-u <upstream>`::
325 `--set-upstream-to=<upstream>`::
326 Set up _<branch-name>_'s tracking information so _<upstream>_ is
327 considered _<branch-name>_'s upstream branch. If no _<branch-name>_
328 is specified, then it defaults to the current branch.
329
330 `--unset-upstream`::
331 Remove the upstream information for _<branch-name>_. If no branch
332 is specified it defaults to the current branch.
333
334 `--edit-description`::
335 Open an editor and edit the text to explain what the branch is
336 for, to be used by various other commands (e.g. `format-patch`,
337 `request-pull`, and `merge` (if enabled)). Multi-line explanations
338 may be used.
339
340 `--contains [<commit>]`::
341 Only list branches which contain _<commit>_ (`HEAD`
342 if not specified). Implies `--list`.
343
344 `--no-contains [<commit>]`::
345 Only list branches which don't contain _<commit>_
346 (`HEAD` if not specified). Implies `--list`.
347
348 `--merged [<commit>]`::
349 Only list branches whose tips are reachable from
350 _<commit>_ (`HEAD` if not specified). Implies `--list`.
351
352 `--no-merged [<commit>]`::
353 Only list branches whose tips are not reachable from
354 _<commit>_ (`HEAD` if not specified). Implies `--list`.
355
356 `--forked <branch>`::
357 Only list branches whose configured upstream matches
358 _<branch>_. The argument can be a ref (e.g. `origin/main`,
359 `master`), a remote name like `origin` for the branch its
360 `origin/HEAD` points at, or a shell-style glob (e.g.
361 `'origin/*'`). The option can be repeated to widen the
362 filter. Implies `--list`.
363
364 `--points-at <object>`::
365 Only list branches of _<object>_.
366
367 `--format <format>`::
368 A string that interpolates `%(fieldname)` from a branch ref being shown
369 and the object it points at. _<format>_ is the same as
370 that of linkgit:git-for-each-ref[1].
371
372 _<branch-name>_::
373 The name of the branch to create or delete.
374 The new branch name must pass all checks defined by
375 linkgit:git-check-ref-format[1]. Some of these checks
376 may restrict the characters allowed in a branch name.
377
378 _<start-point>_::
379 The new branch head will point to this commit. It may be
380 given as a branch name, a commit-id, or a tag. If this
381 option is omitted, the current `HEAD` will be used instead.
382
383 _<old-branch>_::
384 The name of an existing branch. If this option is omitted,
385 the name of the current branch will be used instead.
386
387 _<new-branch>_::
388 The new name for an existing branch. The same restrictions as for
389 _<branch-name>_ apply.
390
391 CONFIGURATION
392 -------------
393 `pager.branch` is only respected when listing branches, i.e., when
394 `--list` is used or implied. The default is to use a pager.
395 See linkgit:git-config[1].
396
397 include::includes/cmd-config-section-rest.adoc[]
398
399 include::config/branch.adoc[]
400
401 EXAMPLES
402 --------
403
404 Start development from a known tag::
405 +
406 ------------
407 $ git clone git://git.kernel.org/pub/scm/.../linux-2.6 my2.6
408 $ cd my2.6
409 $ git branch my2.6.14 v2.6.14 <1>
410 $ git switch my2.6.14
411 ------------
412 +
413 <1> This step and the next one could be combined into a single step with
414 "checkout -b my2.6.14 v2.6.14".
415
416 Delete an unneeded branch::
417 +
418 ------------
419 $ git clone git://git.kernel.org/.../git.git my.git
420 $ cd my.git
421 $ git branch -d -r origin/todo origin/html origin/man <1>
422 $ git branch -D test <2>
423 ------------
424 +
425 <1> Delete the remote-tracking branches "todo", "html" and "man". The next
426 `git fetch` or `git pull` will create them again unless you configure them not to.
427 See linkgit:git-fetch[1].
428 <2> Delete the "test" branch even if the "master" branch (or whichever branch
429 is currently checked out) does not have all commits from the test branch.
430
431 Listing branches from a specific remote::
432 +
433 ------------
434 $ git branch -r -l '<remote>/<pattern>' <1>
435 $ git for-each-ref 'refs/remotes/<remote>/<pattern>' <2>
436 ------------
437 +
438 <1> Using `-a` would conflate _<remote>_ with any local branches you happen to
439 have been prefixed with the same _<remote>_ pattern.
440 <2> `for-each-ref` can take a wide range of options. See linkgit:git-for-each-ref[1]
441
442 Patterns will normally need quoting.
443
444 NOTES
445 -----
446
447 If you are creating a branch that you want to switch to immediately,
448 it is easier to use the `git switch` command with its `-c` option to
449 do the same thing with a single command.
450
451 The options `--contains`, `--no-contains`, `--merged` and `--no-merged`
452 serve four related but different purposes:
453
454 - `--contains <commit>` is used to find all branches which will need
455 special attention if _<commit>_ were to be rebased or amended, since those
456 branches contain the specified _<commit>_.
457
458 - `--no-contains <commit>` is the inverse of that, i.e. branches that don't
459 contain the specified _<commit>_.
460
461 - `--merged` is used to find all branches which can be safely deleted,
462 since those branches are fully contained by `HEAD`.
463
464 - `--no-merged` is used to find branches which are candidates for merging
465 into `HEAD`, since those branches are not fully contained by `HEAD`.
466
467 include::ref-reachability-filters.adoc[]
468
469 SEE ALSO
470 --------
471 linkgit:git-check-ref-format[1],
472 linkgit:git-fetch[1],
473 linkgit:git-remote[1],
474 link:user-manual.html#what-is-a-branch["Understanding history: What is
475 a branch?"] in the Git User's Manual.
476
477 GIT
478 ---
479 Part of the linkgit:git[1] suite