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-push.adoc

811 lines · 32257 bytes Raw Blame History
1git-push(1)
2===========
3
4NAME
5----
6git-push - Update remote refs along with associated objects
7
8
9SYNOPSIS
10--------
11[synopsis]
12git push [--all | --branches | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
13 [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-q | --quiet] [-v | --verbose]
14 [-u | --set-upstream] [-o <string> | --push-option=<string>]
15 [--[no-]signed | --signed=(true|false|if-asked)]
16 [--force-with-lease[=<refname>[:<expect>]] [--force-if-includes]]
17 [--no-verify] [<repository> [<refspec>...]]
18
19DESCRIPTION
20-----------
21Updates one or more branches, tags, or other references in one or more
22remote repositories from your local repository, and sends all necessary
23data that isn't already on the remote.
24
25The simplest way to push is `git push <remote> <branch>`.
26`git push origin main` will push the local `main` branch to the `main`
27branch on the remote named `origin`.
28
29You can also push to multiple remotes at once by using a remote group.
30A remote group is a named list of remotes configured via `remotes.<name>`
31in your git config:
32
33 $ git config remotes.all-remotes "origin gitlab backup"
34
35Then `git push all-remotes` will push to `origin`, `gitlab`, and
36`backup` in turn, as if you had run `git push` against each one
37individually. Each remote is pushed independently using its own
38push mapping configuration. There is a `remotes.<group>` entry in
39the configuration file. (See linkgit:git-config[1]).
40
41The `<repository>` argument defaults to the upstream for the current
42branch, or `origin` if there's no configured upstream.
43
44To decide which branches, tags, or other refs to push, Git uses
45(in order of precedence):
46
471. The `<refspec>` argument(s) (for example `main` in `git push origin main`)
48 or the `--all`, `--mirror`, or `--tags` options
492. The `remote.<name>.push` configuration for the repository being pushed to
503. The `push.default` configuration. The default is `push.default=simple`,
51 which will push to a branch with the same name as the current branch.
52 See the <<CONFIGURATION,CONFIGURATION>> section below for more on `push.default`.
53
54`git push` may fail if you haven't set an upstream for the current branch,
55depending on what `push.default` is set to.
56See the <<UPSTREAM-BRANCHES,UPSTREAM BRANCHES>> section below for more
57on how to set and use upstreams.
58
59You can make interesting things happen to a repository
60every time you push into it, by setting up 'hooks' there. See
61documentation for linkgit:git-receive-pack[1].
62
63[[OPTIONS]]
64OPTIONS
65-------
66_<repository>_::
67 The "remote" repository that is the destination of a push
68 operation. This parameter can be either a URL
69 (see the section <<URLS,GIT URLS>> below), the name
70 of a remote (see the section <<REMOTES,REMOTES>> below),
71 or the name of a remote group
72 (see the section <<REMOTE-GROUPS,REMOTE GROUPS>> below).
73
74`<refspec>...`::
75 Specify what destination ref to update with what source object.
76+
77The format for a refspec is `[+]<src>[:<dst>]`, for example `main`,
78`main:other`, or `HEAD^:refs/heads/main`.
79+
80The _<src>_ is often the name of the local branch to push, but it can be
81any arbitrary "SHA-1 expression" (see linkgit:gitrevisions[7]).
82+
83The _<dst>_ determines what ref to update on the remote side. It must be the
84name of a branch, tag, or other ref, not an arbitrary expression.
85+
86The `+` is optional and does the same thing as `--force`.
87+
88You can write a refspec using the fully expanded form (for
89example `refs/heads/main:refs/heads/main`) which specifies the exact source
90and destination, or with a shorter form (for example `main` or
91`main:other`). Here are the rules for how refspecs are expanded,
92as well as various other special refspec forms:
93+
94 * _<src>_ without a `:<dst>` means to update the same ref as the
95 _<src>_, unless the `remote.<repository>.push` configuration specifies a
96 different _<dst>_. For example, if `main` is a branch, then the refspec
97 `main` expands to `main:refs/heads/main`.
98 * If _<dst>_ unambiguously refers to a ref on the <repository> remote,
99 then expand it to that ref. For example, if `v1.0` is a tag on the
100 remote, then `HEAD:v1.0` expands to `HEAD:refs/tags/v1.0`.
101 * If _<src>_ resolves to a ref starting with `refs/heads/` or `refs/tags/`,
102 then prepend that to <dst>. For example, if `main` is a branch, then
103 `main:other` expands to `main:refs/heads/other`
104 * The special refspec `:` (or `+:` to allow non-fast-forward updates)
105 directs Git to push "matching" branches: for every branch that exists on
106 the local side, the remote side is updated if a branch of the same name
107 already exists on the remote side.
108 * _<src>_ may contain a `*` to indicate a simple pattern match.
109 This works like a glob that matches any ref matching the pattern.
110 There must be only one `*` in both the `<src>` and `<dst>`.
111 It will map refs to the destination by replacing the * with the
112 contents matched from the source. For example, `refs/heads/*:refs/heads/*`
113 will push all branches.
114 * A refspec starting with `^` is a negative refspec.
115 This specifies refs to exclude. A ref will be considered to
116 match if it matches at least one positive refspec, and does not
117 match any negative refspec. Negative refspecs can be pattern refspecs.
118 They must only contain a _<src>_.
119 Fully spelled out hex object names are also not supported.
120 For example, `git push origin 'refs/heads/*' '^refs/heads/dev-*'`
121 will push all branches except for those starting with `dev-`
122 * If _<src>_ is empty, it deletes the _<dst>_ ref from the remote
123 repository. For example, `git push origin :dev` will
124 delete the `dev` branch.
125 * `tag <tag>` expands to `refs/tags/<tag>:refs/tags/<tag>`.
126 This is technically a special syntax for `git push` and not a refspec,
127 since in `git push origin tag v1.0` the arguments `tag` and `v1.0`
128 are separate.
129 * If the refspec can't be expanded unambiguously, error out
130 with an error indicating what was tried, and depending
131 on the `advice.pushUnqualifiedRefname` configuration (see
132 linkgit:git-config[1]) suggest what refs/ namespace you may have
133 wanted to push to.
134
135Not all updates are allowed: see
136<<PUSH_RULES,PUSH RULES>> below for the details.
137
138`--all`::
139`--branches`::
140 Push all branches (i.e. refs under `refs/heads/`); cannot be
141 used with other <refspec>.
142
143`--prune`::
144 Remove remote branches that don't have a local counterpart. For example
145 a remote branch `tmp` will be removed if a local branch with the same
146 name doesn't exist any more. This also respects refspecs, e.g.
147 `git push --prune remote refs/heads/*:refs/tmp/*` would
148 make sure that remote `refs/tmp/foo` will be removed if `refs/heads/foo`
149 doesn't exist.
150
151`--mirror`::
152 Instead of naming each ref to push, specifies that all
153 refs under `refs/` (which includes but is not
154 limited to `refs/heads/`, `refs/remotes/`, and `refs/tags/`)
155 be mirrored to the remote repository. Newly created local
156 refs will be pushed to the remote end, locally updated refs
157 will be force updated on the remote end, and deleted refs
158 will be removed from the remote end. This is the default
159 if the configuration option `remote.<remote>.mirror` is
160 set.
161
162`-n`::
163`--dry-run`::
164 Do everything except actually send the updates.
165
166`--porcelain`::
167 Produce machine-readable output. The output status line for each ref
168 will be tab-separated and sent to stdout instead of stderr. The full
169 symbolic names of the refs will be given.
170
171`-d`::
172`--delete`::
173 All listed refs are deleted from the remote repository. This is
174 the same as prefixing all refs with a colon.
175
176`--tags`::
177 All refs under `refs/tags` are pushed, in
178 addition to refspecs explicitly listed on the command
179 line.
180
181`--follow-tags`::
182 Push all the refs that would be pushed without this option,
183 and also push annotated tags in `refs/tags` that are missing
184 from the remote but are pointing at commit-ish that are
185 reachable from the refs being pushed. This can also be specified
186 with configuration variable `push.followTags`. For more
187 information, see `push.followTags` in linkgit:git-config[1].
188
189`--signed`::
190`--no-signed`::
191`--signed=(true|false|if-asked)`::
192 GPG-sign the push request to update refs on the receiving
193 side, to allow it to be checked by the hooks and/or be
194 logged. Possible values are:
195`false`;;
196`--no-signed`;;
197no signing will be attempted.
198`true`;;
199`--signed`;;
200the push will fail if the server does not support signed pushes.
201`if-asked`;;
202sign if and only if the server supports signed pushes. The push
203will also fail if the actual call to `gpg --sign` fails. See
204linkgit:git-receive-pack[1] for the details on the receiving end.
205
206`--atomic`::
207`--no-atomic`::
208 Use an atomic transaction on the remote side if available.
209 Either all refs are updated, or on error, no refs are updated.
210 If the server does not support atomic pushes the push will fail.
211
212`-o <option>`::
213`--push-option=<option>`::
214 Transmit the given string to the server, which passes them to
215 the pre-receive as well as the post-receive hook. The given string
216 must not contain a _NUL_ or _LF_ character.
217 When multiple `--push-option=<option>` are given, they are
218 all sent to the other side in the order listed on the
219 command line.
220 When no `--push-option=<option>` is given from the command
221 line, the values of configuration variable `push.pushOption`
222 are used instead.
223
224`--receive-pack=<git-receive-pack>`::
225`--exec=<git-receive-pack>`::
226 Path to the 'git-receive-pack' program on the remote
227 end. Sometimes useful when pushing to a remote
228 repository over ssh, and you do not have the program in
229 a directory on the default `$PATH`.
230
231`--force-with-lease`::
232`--no-force-with-lease`::
233`--force-with-lease=<refname>`::
234`--force-with-lease=<refname>:<expect>`::
235 Usually, `git push` refuses to update a remote ref that is
236 not an ancestor of the local ref used to overwrite it.
237+
238This option overrides this restriction if the current value of the
239remote ref is the expected value. `git push` fails otherwise.
240+
241Imagine that you have to rebase what you have already published.
242You will have to bypass the "must fast-forward" rule in order to
243replace the history you originally published with the rebased history.
244If somebody else built on top of your original history while you are
245rebasing, the tip of the branch at the remote may advance with their
246commit, and blindly pushing with `--force` will lose their work.
247+
248This option allows you to say that you expect the history you are
249updating is what you rebased and want to replace. If the remote ref
250still points at the commit you specified, you can be sure that no
251other people did anything to the ref. It is like taking a "lease" on
252the ref without explicitly locking it, and the remote ref is updated
253only if the "lease" is still valid.
254+
255`--force-with-lease` alone, without specifying the details, will protect
256all remote refs that are going to be updated by requiring their
257current value to be the same as the remote-tracking branch we have
258for them.
259+
260`--force-with-lease=<refname>`, without specifying the expected value, will
261protect _<refname>_ (alone), if it is going to be updated, by
262requiring its current value to be the same as the remote-tracking
263branch we have for it.
264+
265`--force-with-lease=<refname>:<expect>` will protect _<refname>_ (alone),
266if it is going to be updated, by requiring its current value to be
267the same as the specified value _<expect>_ (which is allowed to be
268different from the remote-tracking branch we have for the refname,
269or we do not even have to have such a remote-tracking branch when
270this form is used). If _<expect>_ is the empty string, then the named ref
271must not already exist.
272+
273Note that all forms other than `--force-with-lease=<refname>:<expect>`
274that specifies the expected current value of the ref explicitly are
275still experimental and their semantics may change as we gain experience
276with this feature.
277+
278`--no-force-with-lease` will cancel all the previous `--force-with-lease` on the
279command line.
280+
281A general note on safety: supplying this option without an expected
282value, i.e. as `--force-with-lease` or `--force-with-lease=<refname>`
283interacts very badly with anything that implicitly runs `git fetch` on
284the remote to be pushed to in the background, e.g. `git fetch origin`
285on your repository in a cronjob.
286+
287The protection it offers over `--force` is ensuring that subsequent
288changes your work wasn't based on aren't clobbered, but this is
289trivially defeated if some background process is updating refs in the
290background. We don't have anything except the remote tracking info to
291go by as a heuristic for refs you're expected to have seen & are
292willing to clobber.
293+
294If your editor or some other system is running `git fetch` in the
295background for you a way to mitigate this is to simply set up another
296remote:
297+
298----
299git remote add origin-push $(git config remote.origin.url)
300git fetch origin-push
301----
302+
303Now when the background process runs `git fetch origin` the references
304on `origin-push` won't be updated, and thus commands like:
305+
306----
307git push --force-with-lease origin-push
308----
309+
310Will fail unless you manually run `git fetch origin-push`. This method
311is of course entirely defeated by something that runs `git fetch
312--all`, in that case you'd need to either disable it or do something
313more tedious like:
314+
315----
316git fetch # update 'master' from remote
317git tag base master # mark our base point
318git rebase -i master # rewrite some commits
319git push --force-with-lease=master:base master:master
320----
321+
322I.e. create a `base` tag for versions of the upstream code that you've
323seen and are willing to overwrite, then rewrite history, and finally
324force push changes to `master` if the remote version is still at
325`base`, regardless of what your local `remotes/origin/master` has been
326updated to in the background.
327+
328Alternatively, specifying `--force-if-includes` as an ancillary option
329along with `--force-with-lease[=<refname>]` (i.e., without saying what
330exact commit the ref on the remote side must be pointing at, or which
331refs on the remote side are being protected) at the time of "push" will
332verify if updates from the remote-tracking refs that may have been
333implicitly updated in the background are integrated locally before
334allowing a forced update.
335
336`-f`::
337`--force`::
338 Usually, `git push` will refuse to update a branch that is not an
339 ancestor of the commit being pushed.
340+
341This flag disables that check, the other safety checks in
342<<PUSH_RULES,PUSH RULES>> below, and the checks in `--force-with-lease`.
343It can cause the remote repository to lose commits; use it with care.
344+
345Note that `--force` applies to all the refs that are pushed, hence
346using it with `push.default` set to `matching` or with multiple push
347destinations configured with `remote.<name>.push` may overwrite refs
348other than the current branch (including local refs that are
349strictly behind their remote counterpart). To force a push to only
350one branch, use a `+` in front of the refspec to push (e.g `git push
351origin +master` to force a push to the `master` branch). See the
352`<refspec>...` section above for details.
353
354`--force-if-includes`::
355`--no-force-if-includes`::
356 Force an update only if the tip of the remote-tracking ref
357 has been integrated locally.
358+
359This option enables a check that verifies if the tip of the
360remote-tracking ref is reachable from one of the "reflog" entries of
361the local branch based in it for a rewrite. The check ensures that any
362updates from the remote have been incorporated locally by rejecting the
363forced update if that is not the case.
364+
365If the option is passed without specifying `--force-with-lease`, or
366specified along with `--force-with-lease=<refname>:<expect>`, it is
367a "no-op".
368+
369Specifying `--no-force-if-includes` disables this behavior.
370
371`--repo=<repository>`::
372 This option is equivalent to the _<repository>_ argument. If both
373 are specified, the command-line argument takes precedence.
374
375`-u`::
376`--set-upstream`::
377 For every branch that is up to date or successfully pushed, add
378 upstream (tracking) reference, used by argument-less
379 linkgit:git-pull[1] and other commands. For more information,
380 see `branch.<name>.merge` in linkgit:git-config[1].
381
382`--thin`::
383`--no-thin`::
384 These options are passed to linkgit:git-send-pack[1]. A thin transfer
385 significantly reduces the amount of sent data when the sender and
386 receiver share many of the same objects in common. The default is
387 `--thin`.
388
389`-q`::
390`--quiet`::
391 Suppress all output, including the listing of updated refs,
392 unless an error occurs. Progress is not reported to the standard
393 error stream.
394
395`-v`::
396`--verbose`::
397 Run verbosely.
398
399`--progress`::
400 Progress status is reported on the standard error stream
401 by default when it is attached to a terminal, unless `-q`
402 is specified. This flag forces progress status even if the
403 standard error stream is not directed to a terminal.
404
405`--no-recurse-submodules`::
406`--recurse-submodules=(check|on-demand|only|no)`::
407 May be used to make sure all submodule commits used by the
408 revisions to be pushed are available on a remote-tracking branch.
409 Possible values are:
410`check`;;
411 Git will verify that all submodule commits that
412 changed in the revisions to be pushed are available on at least one
413 remote of the submodule. If any commits are missing the push will
414 be aborted and exit with non-zero status.
415`on-demand`;;
416 all submodules that changed in the revisions to be pushed will be
417 pushed. If `on-demand` was not able to push all necessary revisions it will
418 also be aborted and exit with non-zero status.
419`only`;;
420 all submodules will be pushed while the superproject is left
421 unpushed.
422`no`;;
423 override the `push.recurseSubmodules` configuration variable when no
424 submodule recursion is required. Similar to using `--no-recurse-submodules`.
425
426+
427When using `on-demand` or `only`, if a submodule has a
428`push.recurseSubmodules=(on-demand|only)` or `submodule.recurse` configuration,
429further recursion will occur. In this case, `only` is treated as `on-demand`.
430
431`--verify`::
432`--no-verify`::
433 Toggle the pre-push hook (see linkgit:githooks[5]). The
434 default is `--verify`, giving the hook a chance to prevent the
435 push. With `--no-verify`, the hook is bypassed completely.
436
437`-4`::
438`--ipv4`::
439 Use IPv4 addresses only, ignoring IPv6 addresses.
440
441`-6`::
442`--ipv6`::
443 Use IPv6 addresses only, ignoring IPv4 addresses.
444
445include::urls-remotes.adoc[]
446
447[[REMOTE-GROUPS]]
448REMOTE GROUPS
449-------------
450
451A remote group is a named list of remotes configured via `remotes.<name>`
452in your git config:
453
454 $ git config remotes.all-remotes "r1 r2 r3"
455
456When a group name is given as the `<repository>` argument, the push is
457performed to each member remote in turn. The defining principle is:
458
459 git push <options> all-remotes <args>
460
461is exactly equivalent to:
462
463 git push <options> r1 <args>
464 git push <options> r2 <args>
465 ...
466 git push <options> rN <args>
467
468where r1, r2, ..., rN are the members of `all-remotes`. No special
469behaviour is added or removed — the group is purely a shorthand for
470running the same push command against each member remote individually.
471
472When pushing to a group of more than one remote, Git spawns a separate
473`git push` subprocess for each member remote in sequence. Each subprocess
474receives the same flags and refspecs as the original invocation. This
475means that per-remote push mappings configured via `remote.<name>.push`
476and mirror mode (`remote.<name>.mirror`) are evaluated independently for
477each remote, and a mirror remote in the group cannot affect the push
478behaviour of other non-mirror remotes in the same group.
479
480The `--atomic` option is not supported for group pushes, because atomicity
481can only be guaranteed within a single transport connection to a single
482remote. Git will refuse the invocation with an error if `--atomic` is
483combined with a group name.
484
485If any member remote fails whether due to a push rejection (e.g. a
486non-fast-forward update, a server-side hook refusing a ref) or a connection
487error (e.g. the repository does not exist, authentication fails, or the
488network is unreachable), Git reports the error and continues pushing to
489the remaining remotes in the group. The overall exit code is non-zero if
490any member push fails.
491
492This means the user is responsible for ensuring that the sequence of
493individual pushes makes sense. If `git push r1`` would fail for a given
494set of options and arguments, then `git push all-remotes` will fail in
495the same way when it reaches r1. The group push does not do anything
496special to make a failing individual push succeed.
497
498OUTPUT
499------
500
501The output of "git push" depends on the transport method used; this
502section describes the output when pushing over the Git protocol (either
503locally or via ssh).
504
505The status of the push is output in tabular form, with each line
506representing the status of a single ref. Each line is of the form:
507
508-------------------------------
509 <flag> <summary> <from> -> <to> (<reason>)
510-------------------------------
511
512If `--porcelain` is used, then each line of the output is of the form:
513
514-------------------------------
515 <flag> \t <from>:<to> \t <summary> (<reason>)
516-------------------------------
517
518The status of up-to-date refs is shown only if `--porcelain` or `--verbose`
519option is used.
520
521_<flag>_::
522 A single character indicating the status of the ref:
523(space);; for a successfully pushed fast-forward;
524`+`;; for a successful forced update;
525`-`;; for a successfully deleted ref;
526`*`;; for a successfully pushed new ref;
527`!`;; for a ref that was rejected or failed to push; and
528`=`;; for a ref that was up to date and did not need pushing.
529
530_<summary>_::
531 For a successfully pushed ref, the summary shows the old and new
532 values of the ref in a form suitable for using as an argument to
533 `git log` (this is `<old>..<new>` in most cases, and
534 `<old>...<new>` for forced non-fast-forward updates).
535+
536For a failed update, more details are given:
537+
538--
539rejected::
540 Git did not try to send the ref at all, typically because it
541 is not a fast-forward and you did not force the update.
542
543remote rejected::
544 The remote end refused the update. Usually caused by a hook
545 on the remote side, or because the remote repository has one
546 of the following safety options in effect:
547 `receive.denyCurrentBranch` (for pushes to the checked out
548 branch), `receive.denyNonFastForwards` (for forced
549 non-fast-forward updates), `receive.denyDeletes` or
550 `receive.denyDeleteCurrent`. See linkgit:git-config[1].
551
552remote failure::
553 The remote end did not report the successful update of the ref,
554 perhaps because of a temporary error on the remote side, a
555 break in the network connection, or other transient error.
556--
557
558from::
559 The name of the local ref being pushed, minus its
560 `refs/<type>/` prefix. In the case of deletion, the
561 name of the local ref is omitted.
562
563to::
564 The name of the remote ref being updated, minus its
565 `refs/<type>/` prefix.
566
567reason::
568 A human-readable explanation. In the case of successfully pushed
569 refs, no explanation is needed. For a failed ref, the reason for
570 failure is described.
571
572[[PUSH_RULES]]
573PUSH RULES
574----------
575
576As a safety feature, the `git push` command only allows certain kinds of
577updates to prevent you from accidentally losing data on the remote.
578
579Because branches and tags are intended to be used differently, the
580safety rules for pushing to a branch are different from the rules
581for pushing to a tag. In the following rules "update" means any
582modifications except deletions and creations. Deletions and creations
583are always allowed, except when forbidden by configuration or hooks.
584
5851. If the push destination is a **branch** (`refs/heads/*`): only
586 fast-forward updates are allowed, which means the destination must be
587 an ancestor of the source commit. The source must be a commit.
5882. If the push destination is a **tag** (`refs/tags/*`): all updates will
589 be rejected. The source can be any object.
5903. If the push destination is not a branch or tag:
591 * If the source is a tree or blob object, any updates will be rejected
592 * If the source is a tag or commit object, any fast-forward update
593 is allowed, even in cases where what's being fast-forwarded is not a
594 commit, but a tag object which happens to point to a new commit which
595 is a fast-forward of the commit the last tag (or commit) it's
596 replacing. Replacing a tag with an entirely different tag is also
597 allowed, if it points to the same commit, as well as pushing a peeled
598 tag, i.e. pushing the commit that existing tag object points to, or a
599 new tag object which an existing commit points to.
600
601You can override these rules by passing `--force` or by adding the
602optional leading `+` to a refspec. The only exceptions are that no
603amount of forcing will make a branch accept a non-commit object,
604and forcing won't make the remote repository accept a push that it's
605configured to deny.
606
607Hooks and configuration can also override or amend these rules,
608see e.g. `receive.denyNonFastForwards` and `receive.denyDeletes`
609in linkgit:git-config[1] and `pre-receive` and `update` in
610linkgit:githooks[5].
611
612NOTE ABOUT FAST-FORWARDS
613------------------------
614
615When an update changes a branch (or more in general, a ref) that used to
616point at commit A to point at another commit B, it is called a
617fast-forward update if and only if B is a descendant of A.
618
619In a fast-forward update from A to B, the set of commits that the original
620commit A built on top of is a subset of the commits the new commit B
621builds on top of. Hence, it does not lose any history.
622
623In contrast, a non-fast-forward update will lose history. For example,
624suppose you and somebody else started at the same commit X, and you built
625a history leading to commit B while the other person built a history
626leading to commit A. The history looks like this:
627
628----------------
629
630 B
631 /
632 ---X---A
633
634----------------
635
636Further suppose that the other person already pushed changes leading to A
637back to the original repository from which you two obtained the original
638commit X.
639
640The push done by the other person updated the branch that used to point at
641commit X to point at commit A. It is a fast-forward.
642
643But if you try to push, you will attempt to update the branch (that
644now points at A) with commit B. This does _not_ fast-forward. If you did
645so, the changes introduced by commit A will be lost, because everybody
646will now start building on top of B.
647
648The command by default does not allow an update that is not a fast-forward
649to prevent such loss of history.
650
651If you do not want to lose your work (history from X to B) or the work by
652the other person (history from X to A), you would need to first fetch the
653history from the repository, create a history that contains changes done
654by both parties, and push the result back.
655
656You can perform "git pull", resolve potential conflicts, and "git push"
657the result. A "git pull" will create a merge commit C between commits A
658and B.
659
660----------------
661
662 B---C
663 / /
664 ---X---A
665
666----------------
667
668Updating A with the resulting merge commit will fast-forward and your
669push will be accepted.
670
671Alternatively, you can rebase your change between X and B on top of A,
672with `git pull --rebase`, and push the result back. The rebase will
673create a new commit D that builds the change between X and B on top of
674A.
675
676----------------
677
678 B D
679 / /
680 ---X---A
681
682----------------
683
684Again, updating A with this commit will fast-forward and your push will be
685accepted.
686
687There is another common situation where you may encounter non-fast-forward
688rejection when you try to push, and it is possible even when you are
689pushing into a repository nobody else pushes into. After you push commit
690A yourself (in the first picture in this section), replace it with `git
691commit --amend` to produce commit B, and you try to push it out, because
692forgot that you have pushed A out already. In such a case, and only if
693you are certain that nobody in the meantime fetched your earlier commit A
694(and started building on top of it), you can run `git push --force` to
695overwrite it. In other words, `git push --force` is a method reserved for
696a case where you do mean to lose history.
697
698
699EXAMPLES
700--------
701
702`git push`::
703 Works like `git push <remote>`, where <remote> is the
704 current branch's remote (or `origin`, if no remote is
705 configured for the current branch).
706
707`git push origin`::
708 Without additional configuration, pushes the current branch to
709 the configured upstream (`branch.<name>.merge` configuration
710 variable) if it has the same name as the current branch, and
711 errors out without pushing otherwise.
712+
713The default behavior of this command when no _<refspec>_ is given can be
714configured by setting the `push` option of the remote, or the `push.default`
715configuration variable.
716+
717For example, to default to pushing only the current branch to `origin`
718use `git config remote.origin.push HEAD`. Any valid _<refspec>_ (like
719the ones in the examples below) can be configured as the default for
720`git push origin`.
721
722`git push origin :`::
723 Push "matching" branches to `origin`. See
724 _<refspec>_ in the <<OPTIONS,OPTIONS>> section above for a
725 description of "matching" branches.
726
727`git push origin master`::
728 Find a ref that matches `master` in the source repository
729 (most likely, it would find `refs/heads/master`), and update
730 the same ref (e.g. `refs/heads/master`) in `origin` repository
731 with it. If `master` did not exist remotely, it would be
732 created.
733
734`git push origin HEAD`::
735 A handy way to push the current branch to the same name on the
736 remote.
737
738`git push mothership master:satellite/master dev:satellite/dev`::
739 Use the source ref that matches `master` (e.g. `refs/heads/master`)
740 to update the ref that matches `satellite/master` (most probably
741 `refs/remotes/satellite/master`) in the `mothership` repository;
742 do the same for `dev` and `satellite/dev`.
743+
744See the section describing `<refspec>...` above for a discussion of
745the matching semantics.
746+
747This is to emulate `git fetch` run on the `mothership` using `git
748push` that is run in the opposite direction in order to integrate
749the work done on `satellite`, and is often necessary when you can
750only make connection in one way (i.e. satellite can ssh into
751mothership but mothership cannot initiate connection to satellite
752because the latter is behind a firewall or does not run sshd).
753+
754After running this `git push` on the `satellite` machine, you would
755ssh into the `mothership` and run `git merge` there to complete the
756emulation of `git pull` that were run on `mothership` to pull changes
757made on `satellite`.
758
759`git push origin HEAD:master`::
760 Push the current branch to the remote ref matching `master` in the
761 `origin` repository. This form is convenient to push the current
762 branch without thinking about its local name.
763
764`git push origin master:refs/heads/experimental`::
765 Create the branch `experimental` in the `origin` repository
766 by copying the current `master` branch. This form is only
767 needed to create a new branch or tag in the remote repository when
768 the local name and the remote name are different; otherwise,
769 the ref name on its own will work.
770
771`git push origin :experimental`::
772 Find a ref that matches `experimental` in the `origin` repository
773 (e.g. `refs/heads/experimental`), and delete it.
774
775`git push origin +dev:master`::
776 Update the origin repository's master branch with the dev branch,
777 allowing non-fast-forward updates. *This can leave unreferenced
778 commits dangling in the origin repository.* Consider the
779 following situation, where a fast-forward is not possible:
780+
781----
782 o---o---o---A---B origin/master
783 \
784 X---Y---Z dev
785----
786+
787The above command would change the origin repository to
788+
789----
790 A---B (unnamed branch)
791 /
792 o---o---o---X---Y---Z master
793----
794+
795Commits A and B would no longer belong to a branch with a symbolic name,
796and so would be unreachable. As such, these commits would be removed by
797a `git gc` command on the origin repository.
798
799include::transfer-data-leaks.adoc[]
800
801[[CONFIGURATION]]
802CONFIGURATION
803-------------
804
805include::includes/cmd-config-section-all.adoc[]
806
807include::config/push.adoc[]
808
809GIT
810---
811Part of the linkgit:git[1] suite