How do I properly force a Git push?

前端 未结 8 2141
一生所求
一生所求 2020-11-22 14:25

I\'ve set up a remote non-bare \"main\" repo and cloned it to my computer. I made some local changes, updated my local repository, and pushed the changes back to my remote r

相关标签:
8条回答
  • 2020-11-22 15:17

    This was our solution for replacing master on a corporate gitHub repository while maintaining history.

    push -f to master on corporate repositories is often disabled to maintain branch history. This solution worked for us.

    git fetch desiredOrigin
    git checkout -b master desiredOrigin/master // get origin master
    

    git checkout currentBranch  // move to target branch
    git merge -s ours master  // merge using ours over master
    // vim will open for the commit message
    git checkout master  // move to master
    git merge currentBranch  // merge resolved changes into master
    

    push your branch to desiredOrigin and create a PR

    0 讨论(0)
  • 2020-11-22 15:20

    And if push --force doesn't work you can do push --delete. Look at 2nd line on this instance:

    git reset --hard HEAD~3  # reset current branch to 3 commits ago
    git push origin master --delete  # do a very very bad bad thing
    git push origin master  # regular push
    

    But beware...

    Never ever go back on a public git history!

    In other words:

    • Don't ever force push on a public repository.
    • Don't do this or anything that can break someone's pull.
    • Don't ever reset or rewrite history in a repo someone might have already pulled.

    Of course there are exceptionally rare exceptions even to this rule, but in most cases it's not needed to do it and it will generate problems to everyone else.

    Do a revert instead.

    And always be careful with what you push to a public repo. Reverting:

    git revert -n HEAD~3..HEAD  # prepare a new commit reverting last 3 commits
    git commit -m "sorry - revert last 3 commits because I was not careful"
    git push origin master  # regular push
    

    In effect, both origin HEADs (from the revert and from the evil reset) will contain the same files.


    edit to add updated info and more arguments around push --force

    Consider pushing force with lease instead of push, but still prefer revert

    Another problem push --force may bring is when someone push anything before you do, but after you've already fetched. If you push force your rebased version now you will replace work from others.

    git push --force-with-lease introduced in the git 1.8.5 (thanks to @VonC comment on the question) tries to address this specific issue. Basically, it will bring an error and not push if the remote was modified since your latest fetch.

    This is good if you're really sure a push --force is needed, but still want to prevent more problems. I'd go as far to say it should be the default push --force behaviour. But it's still far from being an excuse to force a push. People who fetched before your rebase will still have lots of troubles, which could be easily avoided if you had reverted instead.

    And since we're talking about git --push instances...

    Why would anyone want to force push?

    @linquize brought a good push force example on the comments: sensitive data. You've wrongly leaked data that shouldn't be pushed. If you're fast enough, you can "fix"* it by forcing a push on top.

    * The data will still be on the remote unless you also do a garbage collect, or clean it somehow. There is also the obvious potential for it to be spread by others who'd fetched it already, but you get the idea.

    0 讨论(0)
提交回复
热议问题