interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-branch.adoc

479 lines · 18175 bytes Raw Blame History
1git-branch(1)
2=============
3
4NAME
5----
6git-branch - List, create, or delete branches
7
8SYNOPSIS
9--------
10[synopsis]
11git 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>...]
20git branch [--track[=(direct|inherit)] | --no-track] [-f]
21 [--recurse-submodules] <branch-name> [<start-point>]
22git branch (--set-upstream-to=<upstream>|-u <upstream>) [<branch-name>]
23git branch --unset-upstream [<branch-name>]
24git branch (-m|-M) [<old-branch>] <new-branch>
25git branch (-c|-C) [<old-branch>] <new-branch>
26git branch (-d|-D) [-r] <branch-name>...
27git branch --edit-description [<branch-name>]
28git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]
29
30DESCRIPTION
31-----------
32
33If `--list` is given, or if there are no non-option arguments, existing
34branches are listed; the current branch will be highlighted in green and
35marked with an asterisk. Any branches checked out in linked worktrees will
36be highlighted in cyan and marked with a plus sign. Option `-r` causes the
37remote-tracking branches to be listed,
38and option `-a` shows both local and remote branches.
39
40If a `<pattern>`
41is given, it is used as a shell wildcard to restrict the output to
42matching branches. If multiple patterns are given, a branch is shown if
43it matches any of the patterns.
44
45Note that when providing a
46`<pattern>`, you must use `--list`; otherwise the command may be interpreted
47as branch creation.
48
49With `--contains`, shows only the branches that contain the named commit
50(in other words, the branches whose tip commits are descendants of the
51named commit), `--no-contains` inverts it. With `--merged`, only branches
52merged into the named commit (i.e. the branches whose tip commits are
53reachable from the named commit) will be listed. With `--no-merged` only
54branches not merged into the named commit will be listed. If the _<commit>_
55argument is missing it defaults to `HEAD` (i.e. the tip of the current
56branch). With `--forked`, only branches whose configured upstream matches
57the given branch or pattern will be listed.
58
59The command's second form creates a new branch head named _<branch-name>_
60which points to the current `HEAD`, or _<start-point>_ if given. As a
61special case, for _<start-point>_, you may use `<rev-A>...<rev-B>` as a
62shortcut for the merge base of _<rev-A>_ and _<rev-B>_ if there is exactly
63one merge base. You can leave out at most one of _<rev-A>_ and _<rev-B>_,
64in which case it defaults to `HEAD`.
65
66Note that this will create the new branch, but it will not switch the
67working tree to it; use `git switch <new-branch>` to switch to the
68new branch.
69
70When a local branch is started off a remote-tracking branch, Git sets up the
71branch (specifically the `branch.<name>.remote` and `branch.<name>.merge`
72configuration entries) so that `git pull` will appropriately merge from
73the remote-tracking branch. This behavior may be changed via the global
74`branch.autoSetupMerge` configuration flag. That setting can be
75overridden by using the `--track` and `--no-track` options, and
76changed later using `git branch --set-upstream-to`.
77
78With a `-m` or `-M` option, _<old-branch>_ will be renamed to _<new-branch>_.
79If _<old-branch>_ had a corresponding reflog, it is renamed to match
80_<new-branch>_, and a reflog entry is created to remember the branch
81renaming. If _<new-branch>_ exists, `-M` must be used to force the rename
82to happen.
83
84The `-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
86new name, along with its config and reflog.
87
88With a `-d` or `-D` option, _<branch-name>_ will be deleted. You may
89specify more than one branch for deletion. If the branch currently
90has a reflog then the reflog will also be deleted.
91
92Use `-r` together with `-d` to delete remote-tracking branches. Note, that it
93only makes sense to delete remote-tracking branches if they no longer exist
94in the remote repository or if `git fetch` was configured not to fetch
95them again. See also the `prune` subcommand of linkgit:git-remote[1] for a
96way to clean up all obsolete remote-tracking branches.
97
98
99OPTIONS
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+
130Note that `git branch -f <branch-name> [<start-point>]`, even with `-f`,
131refuses to change an existing branch _<branch-name>_ that is checked out
132in 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+
172This 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+
217A 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+
231When such a local upstream branch has its own upstream deleted by the
232same operation, its upstream configuration is cleared.
233+
234A branch whose work has not yet been merged into its upstream is
235silently skipped. Delete it with `git branch -D` if you want to
236remove 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+
280The exact upstream branch is chosen depending on the optional argument:
281`-t`, `--track`, or `--track=direct` means to use the start-point branch
282itself as the upstream; `--track=inherit` means to copy the upstream
283configuration of the start-point branch.
284+
285The `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+
289The default option, `true`, behaves as though `--track=direct`
290were 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`
293were given. `simple` behaves as though `--track=direct` were given only when
294the _<start-point>_ is a remote-tracking branch and the new branch has the same
295name as the remote branch.
296+
297See linkgit:git-pull[1] and linkgit:git-config[1] for additional discussion on
298how 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+
311When used in branch creation, a new branch _<branch-name>_ will be created
312in the superproject and all of the submodules in the superproject's
313_<start-point>_. In submodules, the branch will point to the submodule
314commit in the superproject's _<start-point>_ but the branch's tracking
315information will be set up based on the submodule's branches and remotes
316e.g. `git branch --recurse-submodules topic origin/main` will create the
317submodule branch "topic" that points to the submodule commit in the
318superproject'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
391CONFIGURATION
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.
395See linkgit:git-config[1].
396
397include::includes/cmd-config-section-rest.adoc[]
398
399include::config/branch.adoc[]
400
401EXAMPLES
402--------
403
404Start 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
416Delete 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
431Listing 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
442Patterns will normally need quoting.
443
444NOTES
445-----
446
447If you are creating a branch that you want to switch to immediately,
448it is easier to use the `git switch` command with its `-c` option to
449do the same thing with a single command.
450
451The options `--contains`, `--no-contains`, `--merged` and `--no-merged`
452serve 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
467include::ref-reachability-filters.adoc[]
468
469SEE ALSO
470--------
471linkgit:git-check-ref-format[1],
472linkgit:git-fetch[1],
473linkgit:git-remote[1],
474link:user-manual.html#what-is-a-branch["Understanding history: What is
475a branch?"] in the Git User's Manual.
476
477GIT
478---
479Part of the linkgit:git[1] suite