Skip to content

Commit 2f2dc22

Browse files
committed
doc: git-push: create PUSH RULES section
Right now the rules for when a `git push` is allowed are buried at the bottom of the description of `<refspec>`. Put them in their own section so that we can reference them from `--force` and give some context for why they exist. Having the "PUSH RULES" section also lets us be a little bit more specific with the rule in `--force`: we can just focus on the rule for pushing for a branch (which is likely the one that's most relevant) and leave the details about what happens when you push to a tag or a ref that isn't a branch to the later section. Signed-off-by: Julia Evans <[email protected]>
1 parent c44beea commit 2f2dc22

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

Documentation/git-push.adoc

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -91,48 +91,6 @@ is ambiguous.
9191
configuration (see linkgit:git-config[1]) suggest what refs/
9292
namespace you may have wanted to push to.
9393
94-
--
95-
+
96-
The object referenced by <src> is used to update the <dst> reference
97-
on the remote side. Whether this is allowed depends on where in
98-
`refs/*` the <dst> reference lives as described in detail below, in
99-
those sections "update" means any modifications except deletes, which
100-
as noted after the next few sections are treated differently.
101-
+
102-
The `refs/heads/*` namespace will only accept commit objects, and
103-
updates only if they can be fast-forwarded.
104-
+
105-
The `refs/tags/*` namespace will accept any kind of object (as
106-
commits, trees and blobs can be tagged), and any updates to them will
107-
be rejected.
108-
+
109-
It's possible to push any type of object to any namespace outside of
110-
`refs/{tags,heads}/*`. In the case of tags and commits, these will be
111-
treated as if they were the commits inside `refs/heads/*` for the
112-
purposes of whether the update is allowed.
113-
+
114-
I.e. a fast-forward of commits and tags outside `refs/{tags,heads}/*`
115-
is allowed, even in cases where what's being fast-forwarded is not a
116-
commit, but a tag object which happens to point to a new commit which
117-
is a fast-forward of the commit the last tag (or commit) it's
118-
replacing. Replacing a tag with an entirely different tag is also
119-
allowed, if it points to the same commit, as well as pushing a peeled
120-
tag, i.e. pushing the commit that existing tag object points to, or a
121-
new tag object which an existing commit points to.
122-
+
123-
Tree and blob objects outside of `refs/{tags,heads}/*` will be treated
124-
the same way as if they were inside `refs/tags/*`, any update of them
125-
will be rejected.
126-
+
127-
All of the rules described above about what's not allowed as an update
128-
can be overridden by adding an the optional leading `+` to a refspec
129-
(or using `--force` command line option). The only exception to this
130-
is that no amount of forcing will make the `refs/heads/*` namespace
131-
accept a non-commit object. Hooks and configuration can also override
132-
or amend these rules, see e.g. `receive.denyNonFastForwards` in
133-
linkgit:git-config[1] and `pre-receive` and `update` in
134-
linkgit:githooks[5].
135-
+
13694
Pushing an empty <src> allows you to delete the <dst> ref from the
13795
remote repository. Deletions are always accepted without a leading `+`
13896
in the refspec (or `--force`), except when forbidden by configuration
@@ -145,6 +103,7 @@ the local side, the remote side is updated if a branch of the same name
145103
already exists on the remote side.
146104
+
147105
`tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`.
106+
Not all updates are allowed: see PUSH RULES below for the details.
148107

149108
--all::
150109
--branches::
@@ -332,14 +291,12 @@ allowing a forced update.
332291

333292
-f::
334293
--force::
335-
Usually, the command refuses to update a remote ref that is
336-
not an ancestor of the local ref used to overwrite it.
337-
Also, when `--force-with-lease` option is used, the command refuses
338-
to update a remote ref whose current value does not match
339-
what is expected.
294+
Usually, `git push` will refuse to update a branch that is not an
295+
ancestor of the local branch or commit being pushed.
340296
+
341-
This flag disables these checks, and can cause the remote repository
342-
to lose commits; use it with care.
297+
This flag disables that check, the other safety checks in PUSH RULES
298+
below, and the checks in --force-with-lease. It can cause the remote
299+
repository to lose commits; use it with care.
343300
+
344301
Note that `--force` applies to all the refs that are pushed, hence
345302
using it with `push.default` set to `matching` or with multiple push
@@ -508,6 +465,44 @@ reason::
508465
refs, no explanation is needed. For a failed ref, the reason for
509466
failure is described.
510467

468+
PUSH RULES
469+
----------
470+
471+
As a safety feature, the `git push` command only allows certain kinds of
472+
updates to prevent you from accidentally losing data on the remote.
473+
474+
Because branches and tags are intended to be used differently, the
475+
safety rules for pushing to a branch are different from the rules
476+
for pushing to a tag. In the following rules "update" means any
477+
modifications except deletes. Deletions are always allowed, except when
478+
forbidden by configuration or hooks.
479+
480+
1. If the push destination is a **branch** (`refs/heads/*`): only
481+
fast-forward updates are allowed: the destination must be an ancestor
482+
of the source commit. The source must be a commit.
483+
2. If the push destination is a **tag** (`refs/tags/*`): all updates will
484+
be rejected. The source can be any object
485+
(since commits, trees and blobs can be tagged).
486+
3. If the push destination is not a branch or tag:
487+
* If the source is a tree or blob object, any updates will be rejected
488+
* If the source is a tag or commit object, any fast-forward update
489+
is allowed, even in cases where what's being fast-forwarded is not a
490+
commit, but a tag object which happens to point to a new commit which
491+
is a fast-forward of the commit the last tag (or commit) it's
492+
replacing. Replacing a tag with an entirely different tag is also
493+
allowed, if it points to the same commit, as well as pushing a peeled
494+
tag, i.e. pushing the commit that existing tag object points to, or a
495+
new tag object which an existing commit points to.
496+
497+
You can override these rules by passing `--force` or by adding the
498+
optional leading `+` to a refspec. The only exception to this is that no
499+
amount of forcing will make a branch accept a non-commit object.
500+
501+
Hooks and configuration can also override or amend these rules,
502+
see e.g. `receive.denyNonFastForwards` and `receive.denyDeletes`
503+
in linkgit:git-config[1] and `pre-receive` and `update` in
504+
linkgit:githooks[5].
505+
511506
NOTE ABOUT FAST-FORWARDS
512507
------------------------
513508

0 commit comments

Comments
 (0)