-2

When I use this command 'git push origin master' I have faced this error:

( ! [rejected]  master -> master (fetch first)
error: failed to push some refs to 'https: // github.com/rk/cp.git')

Then I have the search according to this error, and I got an answer here and try to follow his opinion, but I have not found any solution and continuously faced this error.

Then I used this command git push origin master -f: it works fine.

My question is:

If I use this command for push regularly, will I face any problem?

If there is any problem so, how I solve this error?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 6
    Short answer: yes, you can have issues by using `push --force`. First thing: use `--force-with-lease` instead. Second thing: explain us why you need to force in the first place? What did you do before your push is rejected? – Gaël J Oct 02 '21 at 16:35
  • 1
    Potentially, yes. See [–force considered harmful; understanding git’s –force-with-lease](https://blog.developer.atlassian.com/force-with-lease/) – Schwern Oct 02 '21 at 17:46
  • Yes, you will have a problem as remote state of the branch is different from what you have locally and git tells you about it, asking you to incorporate changes first before pushing your changes. You need to do git pull first. – Dmitry Oct 02 '21 at 17:52
  • 1
    "error: failed to push some refs" But it said much more than that. What? – matt Oct 02 '21 at 21:42
  • 1
    You are overwriting the branch on the remote side. Any commits you or somebody else might have pushed will be gone after your force push. If that's a "problem" depends on whether you need those commits – knittl Oct 03 '21 at 06:22

1 Answers1

0

my question is if I use this command for push regularly, will I face any problem?

From "git push --force-with-lease vs. --force", with Git 2.30+, the safest command would be:

git push --force-if-includes

When a local branch that is based on a remote ref, has been rewound and is to be force pushed on the remote, "--force-if-includes" runs a check that ensures any updates to the remote-tracking ref that may have happened (by push from another repository) in-between the time of the last update to the local branch (via "git pull", for instance) and right before the time of push, have been integrated locally before allowing a forced update.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250