6

Git version: 1.7.12.3

As the question states, that seems like a really bad idea to me. Without any additional flags or confirmations git push -f will force push all of the tracking branches to remote.

If a developer has a few outdated branches, that are tracking remotes, and he executes that command, all of the tracking branches will have been rolled back to his outdated copies, which causes loss of valuable work.

This can be done accidentally, or by someone not very experienced with git. It really seems like git should do a little more hand-holding in such a dangerous case, and require an additional flag, or ask for a confirmation.

Is there a remedy for this?

gylaz
  • 13,221
  • 8
  • 52
  • 58

3 Answers3

9

Since it's January, 2016 now, I guess it's worth to add some updated info to this question:

  1. As hobbs said, git push --force behaves exactly like normal git push in terms of pushing current or all the changed branches.
  2. git push pushes either all branches or a single one dependent on this configuration of the push.default
  3. More details in this post , but in order to push current branch only, your .gitconfig should looks like:

    [user]
            name = User Name
            email = example.mail@gmail.com
    [push]
            default = simple

  1. In order to see your .gitconfig in Unix-like OS just do cat ~/.gitconfig. This post shows to how to do this on Windows.
  2. Pushing current branch only (simple mode) become default only in Git 2.0 (released on 2014-12-17).
  3. Those, who is using Git 1.7 (or who has updated from 1.7), has matching mode as default mode (pushing all the branches).
Community
  • 1
  • 1
Alex
  • 521
  • 7
  • 7
3

As stated git push default behavior is to push all branches with matching names on the remote.

As the documentations says about -f/--force:

Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check. This can cause the remote repository to lose commits; use it with care.

So adding an extra check in the -f behavior is like asking "Are you sure you want to delete the file?" and then "Are you really sure?"

The default behavior of git push will change in a future major version of git, either 1.9 or 2.0 but certainly not in 1.8, to only push the branch you are working on. Until this is the case, you can set the following option:

git config remote.origin.push HEAD

But still if you use -f and you are on a outdated branch your worries still exists.

Peter van der Does
  • 14,018
  • 4
  • 38
  • 42
2

Because git push -f does what git push does, only with a -f. And because git push is configured by default to push all matching branches if there's no branch argument. You should do git config push.default upstream or git config push.default simple to change the default push behavior (see git help config for more info on what this does).

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • 7
    It's rather harsh on the questioner to say "you configured git push to push all matching branches if not given an argument", when it's the default behaviour! – Mark Longair Oct 17 '12 at 15:02