Documentation / git-push.txton commit Documentation/git-push.txt: explain better cases where --force is dangerous (70495b5)
   1git-push(1)
   2===========
   3
   4NAME
   5----
   6git-push - Update remote refs along with associated objects
   7
   8
   9SYNOPSIS
  10--------
  11[verse]
  12'git push' [--all | --mirror | --tags] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
  13           [--repo=<repository>] [-f | --force] [--prune] [-v | --verbose] [-u | --set-upstream]
  14           [<repository> [<refspec>...]]
  15
  16DESCRIPTION
  17-----------
  18
  19Updates remote refs using local refs, while sending objects
  20necessary to complete the given refs.
  21
  22You can make interesting things happen to a repository
  23every time you push into it, by setting up 'hooks' there.  See
  24documentation for linkgit:git-receive-pack[1].
  25
  26
  27OPTIONS[[OPTIONS]]
  28------------------
  29<repository>::
  30        The "remote" repository that is destination of a push
  31        operation.  This parameter can be either a URL
  32        (see the section <<URLS,GIT URLS>> below) or the name
  33        of a remote (see the section <<REMOTES,REMOTES>> below).
  34
  35<refspec>...::
  36        The format of a <refspec> parameter is an optional plus
  37        `+`, followed by the source ref <src>, followed
  38        by a colon `:`, followed by the destination ref <dst>.
  39        It is used to specify with what <src> object the <dst> ref
  40        in the remote repository is to be updated.  If not specified,
  41        the behavior of the command is controlled by the `push.default`
  42        configuration variable.
  43+
  44The <src> is often the name of the branch you would want to push, but
  45it can be any arbitrary "SHA-1 expression", such as `master~4` or
  46`HEAD` (see linkgit:gitrevisions[7]).
  47+
  48The <dst> tells which ref on the remote side is updated with this
  49push. Arbitrary expressions cannot be used here, an actual ref must
  50be named. If `:`<dst> is omitted, the same ref as <src> will be
  51updated.
  52+
  53The object referenced by <src> is used to update the <dst> reference
  54on the remote side, but by default this is only allowed if the
  55update can fast-forward <dst>.  By having the optional leading `+`,
  56you can tell git to update the <dst> ref even when the update is not a
  57fast-forward.  This does *not* attempt to merge <src> into <dst>.  See
  58EXAMPLES below for details.
  59+
  60`tag <tag>` means the same as `refs/tags/<tag>:refs/tags/<tag>`.
  61+
  62Pushing an empty <src> allows you to delete the <dst> ref from
  63the remote repository.
  64+
  65The special refspec `:` (or `+:` to allow non-fast-forward updates)
  66directs git to push "matching" branches: for every branch that exists on
  67the local side, the remote side is updated if a branch of the same name
  68already exists on the remote side.  This is the default operation mode
  69if no explicit refspec is found (that is neither on the command line
  70nor in any Push line of the corresponding remotes file---see below) and
  71no `push.default` configuration variable is set.
  72
  73--all::
  74        Instead of naming each ref to push, specifies that all
  75        refs under `refs/heads/` be pushed.
  76
  77--prune::
  78        Remove remote branches that don't have a local counterpart. For example
  79        a remote branch `tmp` will be removed if a local branch with the same
  80        name doesn't exist any more. This also respects refspecs, e.g.
  81        `git push --prune remote refs/heads/*:refs/tmp/*` would
  82        make sure that remote `refs/tmp/foo` will be removed if `refs/heads/foo`
  83        doesn't exist.
  84
  85--mirror::
  86        Instead of naming each ref to push, specifies that all
  87        refs under `refs/` (which includes but is not
  88        limited to `refs/heads/`, `refs/remotes/`, and `refs/tags/`)
  89        be mirrored to the remote repository.  Newly created local
  90        refs will be pushed to the remote end, locally updated refs
  91        will be force updated on the remote end, and deleted refs
  92        will be removed from the remote end.  This is the default
  93        if the configuration option `remote.<remote>.mirror` is
  94        set.
  95
  96-n::
  97--dry-run::
  98        Do everything except actually send the updates.
  99
 100--porcelain::
 101        Produce machine-readable output.  The output status line for each ref
 102        will be tab-separated and sent to stdout instead of stderr.  The full
 103        symbolic names of the refs will be given.
 104
 105--delete::
 106        All listed refs are deleted from the remote repository. This is
 107        the same as prefixing all refs with a colon.
 108
 109--tags::
 110        All refs under `refs/tags` are pushed, in
 111        addition to refspecs explicitly listed on the command
 112        line.
 113
 114--receive-pack=<git-receive-pack>::
 115--exec=<git-receive-pack>::
 116        Path to the 'git-receive-pack' program on the remote
 117        end.  Sometimes useful when pushing to a remote
 118        repository over ssh, and you do not have the program in
 119        a directory on the default $PATH.
 120
 121-f::
 122--force::
 123        Usually, the command refuses to update a remote ref that is
 124        not an ancestor of the local ref used to overwrite it.
 125        This flag disables the check.  This can cause the
 126        remote repository to lose commits; use it with care.
 127        Note that `--force` applies to all the refs that are pushed,
 128        hence using it with `push.default` set to `matching` or with
 129        multiple push destinations configured with `remote.*.push`
 130        may overwrite refs other than the current branch (including
 131        local refs that are strictly behind their remote counterpart).
 132        To force a push to only one branch, use a `+` in front of the
 133        refspec to push (e.g `git push origin +master` to force a push
 134        to the `master` branch). See the `<refspec>...` section above
 135        for details.
 136
 137--repo=<repository>::
 138        This option is only relevant if no <repository> argument is
 139        passed in the invocation. In this case, 'git push' derives the
 140        remote name from the current branch: If it tracks a remote
 141        branch, then that remote repository is pushed to. Otherwise,
 142        the name "origin" is used. For this latter case, this option
 143        can be used to override the name "origin". In other words,
 144        the difference between these two commands
 145+
 146--------------------------
 147git push public         #1
 148git push --repo=public  #2
 149--------------------------
 150+
 151is that #1 always pushes to "public" whereas #2 pushes to "public"
 152only if the current branch does not track a remote branch. This is
 153useful if you write an alias or script around 'git push'.
 154
 155-u::
 156--set-upstream::
 157        For every branch that is up to date or successfully pushed, add
 158        upstream (tracking) reference, used by argument-less
 159        linkgit:git-pull[1] and other commands. For more information,
 160        see 'branch.<name>.merge' in linkgit:git-config[1].
 161
 162--thin::
 163--no-thin::
 164        These options are passed to linkgit:git-send-pack[1]. A thin transfer
 165        significantly reduces the amount of sent data when the sender and
 166        receiver share many of the same objects in common. The default is
 167        \--thin.
 168
 169-q::
 170--quiet::
 171        Suppress all output, including the listing of updated refs,
 172        unless an error occurs. Progress is not reported to the standard
 173        error stream.
 174
 175-v::
 176--verbose::
 177        Run verbosely.
 178
 179--progress::
 180        Progress status is reported on the standard error stream
 181        by default when it is attached to a terminal, unless -q
 182        is specified. This flag forces progress status even if the
 183        standard error stream is not directed to a terminal.
 184
 185--recurse-submodules=check|on-demand::
 186        Make sure all submodule commits used by the revisions to be
 187        pushed are available on a remote-tracking branch. If 'check' is
 188        used git will verify that all submodule commits that changed in
 189        the revisions to be pushed are available on at least one remote
 190        of the submodule. If any commits are missing the push will be
 191        aborted and exit with non-zero status. If 'on-demand' is used
 192        all submodules that changed in the revisions to be pushed will
 193        be pushed. If on-demand was not able to push all necessary
 194        revisions it will also be aborted and exit with non-zero status.
 195
 196
 197include::urls-remotes.txt[]
 198
 199OUTPUT
 200------
 201
 202The output of "git push" depends on the transport method used; this
 203section describes the output when pushing over the git protocol (either
 204locally or via ssh).
 205
 206The status of the push is output in tabular form, with each line
 207representing the status of a single ref. Each line is of the form:
 208
 209-------------------------------
 210 <flag> <summary> <from> -> <to> (<reason>)
 211-------------------------------
 212
 213If --porcelain is used, then each line of the output is of the form:
 214
 215-------------------------------
 216 <flag> \t <from>:<to> \t <summary> (<reason>)
 217-------------------------------
 218
 219The status of up-to-date refs is shown only if --porcelain or --verbose
 220option is used.
 221
 222flag::
 223        A single character indicating the status of the ref:
 224(space);; for a successfully pushed fast-forward;
 225`+`;; for a successful forced update;
 226`-`;; for a successfully deleted ref;
 227`*`;; for a successfully pushed new ref;
 228`!`;; for a ref that was rejected or failed to push; and
 229`=`;; for a ref that was up to date and did not need pushing.
 230
 231summary::
 232        For a successfully pushed ref, the summary shows the old and new
 233        values of the ref in a form suitable for using as an argument to
 234        `git log` (this is `<old>..<new>` in most cases, and
 235        `<old>...<new>` for forced non-fast-forward updates).
 236+
 237For a failed update, more details are given:
 238+
 239--
 240rejected::
 241        Git did not try to send the ref at all, typically because it
 242        is not a fast-forward and you did not force the update.
 243
 244remote rejected::
 245        The remote end refused the update.  Usually caused by a hook
 246        on the remote side, or because the remote repository has one
 247        of the following safety options in effect:
 248        `receive.denyCurrentBranch` (for pushes to the checked out
 249        branch), `receive.denyNonFastForwards` (for forced
 250        non-fast-forward updates), `receive.denyDeletes` or
 251        `receive.denyDeleteCurrent`.  See linkgit:git-config[1].
 252
 253remote failure::
 254        The remote end did not report the successful update of the ref,
 255        perhaps because of a temporary error on the remote side, a
 256        break in the network connection, or other transient error.
 257--
 258
 259from::
 260        The name of the local ref being pushed, minus its
 261        `refs/<type>/` prefix. In the case of deletion, the
 262        name of the local ref is omitted.
 263
 264to::
 265        The name of the remote ref being updated, minus its
 266        `refs/<type>/` prefix.
 267
 268reason::
 269        A human-readable explanation. In the case of successfully pushed
 270        refs, no explanation is needed. For a failed ref, the reason for
 271        failure is described.
 272
 273Note about fast-forwards
 274------------------------
 275
 276When an update changes a branch (or more in general, a ref) that used to
 277point at commit A to point at another commit B, it is called a
 278fast-forward update if and only if B is a descendant of A.
 279
 280In a fast-forward update from A to B, the set of commits that the original
 281commit A built on top of is a subset of the commits the new commit B
 282builds on top of.  Hence, it does not lose any history.
 283
 284In contrast, a non-fast-forward update will lose history.  For example,
 285suppose you and somebody else started at the same commit X, and you built
 286a history leading to commit B while the other person built a history
 287leading to commit A.  The history looks like this:
 288
 289----------------
 290
 291      B
 292     /
 293 ---X---A
 294
 295----------------
 296
 297Further suppose that the other person already pushed changes leading to A
 298back to the original repository from which you two obtained the original
 299commit X.
 300
 301The push done by the other person updated the branch that used to point at
 302commit X to point at commit A.  It is a fast-forward.
 303
 304But if you try to push, you will attempt to update the branch (that
 305now points at A) with commit B.  This does _not_ fast-forward.  If you did
 306so, the changes introduced by commit A will be lost, because everybody
 307will now start building on top of B.
 308
 309The command by default does not allow an update that is not a fast-forward
 310to prevent such loss of history.
 311
 312If you do not want to lose your work (history from X to B) nor the work by
 313the other person (history from X to A), you would need to first fetch the
 314history from the repository, create a history that contains changes done
 315by both parties, and push the result back.
 316
 317You can perform "git pull", resolve potential conflicts, and "git push"
 318the result.  A "git pull" will create a merge commit C between commits A
 319and B.
 320
 321----------------
 322
 323      B---C
 324     /   /
 325 ---X---A
 326
 327----------------
 328
 329Updating A with the resulting merge commit will fast-forward and your
 330push will be accepted.
 331
 332Alternatively, you can rebase your change between X and B on top of A,
 333with "git pull --rebase", and push the result back.  The rebase will
 334create a new commit D that builds the change between X and B on top of
 335A.
 336
 337----------------
 338
 339      B   D
 340     /   /
 341 ---X---A
 342
 343----------------
 344
 345Again, updating A with this commit will fast-forward and your push will be
 346accepted.
 347
 348There is another common situation where you may encounter non-fast-forward
 349rejection when you try to push, and it is possible even when you are
 350pushing into a repository nobody else pushes into. After you push commit
 351A yourself (in the first picture in this section), replace it with "git
 352commit --amend" to produce commit B, and you try to push it out, because
 353forgot that you have pushed A out already. In such a case, and only if
 354you are certain that nobody in the meantime fetched your earlier commit A
 355(and started building on top of it), you can run "git push --force" to
 356overwrite it. In other words, "git push --force" is a method reserved for
 357a case where you do mean to lose history.
 358
 359
 360Examples
 361--------
 362
 363`git push`::
 364        Works like `git push <remote>`, where <remote> is the
 365        current branch's remote (or `origin`, if no remote is
 366        configured for the current branch).
 367
 368`git push origin`::
 369        Without additional configuration, works like
 370        `git push origin :`.
 371+
 372The default behavior of this command when no <refspec> is given can be
 373configured by setting the `push` option of the remote, or the `push.default`
 374configuration variable.
 375+
 376For example, to default to pushing only the current branch to `origin`
 377use `git config remote.origin.push HEAD`.  Any valid <refspec> (like
 378the ones in the examples below) can be configured as the default for
 379`git push origin`.
 380
 381`git push origin :`::
 382        Push "matching" branches to `origin`. See
 383        <refspec> in the <<OPTIONS,OPTIONS>> section above for a
 384        description of "matching" branches.
 385
 386`git push origin master`::
 387        Find a ref that matches `master` in the source repository
 388        (most likely, it would find `refs/heads/master`), and update
 389        the same ref (e.g. `refs/heads/master`) in `origin` repository
 390        with it.  If `master` did not exist remotely, it would be
 391        created.
 392
 393`git push origin HEAD`::
 394        A handy way to push the current branch to the same name on the
 395        remote.
 396
 397`git push mothership master:satellite/master dev:satellite/dev`::
 398        Use the source ref that matches `master` (e.g. `refs/heads/master`)
 399        to update the ref that matches `satellite/master` (most probably
 400        `refs/remotes/satellite/master`) in the `mothership` repository;
 401        do the same for `dev` and `satellite/dev`.
 402+
 403This is to emulate `git fetch` run on the `mothership` using `git
 404push` that is run in the opposite direction in order to integrate
 405the work done on `satellite`, and is often necessary when you can
 406only make connection in one way (i.e. satellite can ssh into
 407mothership but mothership cannot initiate connection to satellite
 408because the latter is behind a firewall or does not run sshd).
 409+
 410After running this `git push` on the `satellite` machine, you would
 411ssh into the `mothership` and run `git merge` there to complete the
 412emulation of `git pull` that were run on `mothership` to pull changes
 413made on `satellite`.
 414
 415`git push origin HEAD:master`::
 416        Push the current branch to the remote ref matching `master` in the
 417        `origin` repository. This form is convenient to push the current
 418        branch without thinking about its local name.
 419
 420`git push origin master:refs/heads/experimental`::
 421        Create the branch `experimental` in the `origin` repository
 422        by copying the current `master` branch.  This form is only
 423        needed to create a new branch or tag in the remote repository when
 424        the local name and the remote name are different; otherwise,
 425        the ref name on its own will work.
 426
 427`git push origin :experimental`::
 428        Find a ref that matches `experimental` in the `origin` repository
 429        (e.g. `refs/heads/experimental`), and delete it.
 430
 431`git push origin +dev:master`::
 432        Update the origin repository's master branch with the dev branch,
 433        allowing non-fast-forward updates.  *This can leave unreferenced
 434        commits dangling in the origin repository.*  Consider the
 435        following situation, where a fast-forward is not possible:
 436+
 437----
 438            o---o---o---A---B  origin/master
 439                     \
 440                      X---Y---Z  dev
 441----
 442+
 443The above command would change the origin repository to
 444+
 445----
 446                      A---B  (unnamed branch)
 447                     /
 448            o---o---o---X---Y---Z  master
 449----
 450+
 451Commits A and B would no longer belong to a branch with a symbolic name,
 452and so would be unreachable.  As such, these commits would be removed by
 453a `git gc` command on the origin repository.
 454
 455GIT
 456---
 457Part of the linkgit:git[1] suite