How do I properly force a Git push?

前端 未结 8 2140
一生所求
一生所求 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:01

    Just do:

    git push origin <your_branch_name> --force
    

    or if you have a specific repo:

    git push https://git.... --force
    

    This will delete your previous commit(s) and push your current one.

    It may not be proper, but if anyone stumbles upon this page, thought they might want a simple solution...

    Short flag

    Also note that -f is short for --force, so

    git push origin <your_branch_name> -f
    

    will also work.

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

    If I'm on my local branch A, and I want to force push local branch B to the origin branch C I can use the following syntax:

    git push --force origin B:C
    
    0 讨论(0)
  • 2020-11-22 15:06

    I had the same question but figured it out finally. What you most likely need to do is run the following two git commands (replacing hash with the git commit revision number):

    git checkout <hash>
    git push -f HEAD:master
    
    0 讨论(0)
  • 2020-11-22 15:11

    I would really recommend to:

    • push only to the main repo

    • make sure that main repo is a bare repo, in order to never have any problem with the main repo working tree being not in sync with its .git base. See "How to push a local git repository to another computer?"

    • If you do have to make modification in the main (bare) repo, clone it (on the main server), do your modification and push back to it

    In other words, keep a bare repo accessible both from the main server and the local computer, in order to have a single upstream repo from/to which to pull/pull.

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

    use this following command:

    git push -f origin master
    
    0 讨论(0)
  • 2020-11-22 15:16

    First of all, I would not make any changes directly in the "main" repo. If you really want to have a "main" repo, then you should only push to it, never change it directly.

    Regarding the error you are getting, have you tried git pull from your local repo, and then git push to the main repo? What you are currently doing (if I understood it well) is forcing the push and then losing your changes in the "main" repo. You should merge the changes locally first.

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