Git push error '[remote rejected] master -> master (branch is currently checked out)'

前端 未结 30 2171
半阙折子戏
半阙折子戏 2020-11-22 00:07

Yesterday, I posted a question on how to clone a Git repository from one of my machines to another, How can I \'git clone\' from another machine?.

I am now

相关标签:
30条回答
  • 2020-11-22 00:34

    OK, in case you want a normal remote repository, then create an extra branch and check it out. Push it into one branch (which is not checked out) and merge it with one which is currently active later after pushing from locally.

    For example, on a remote server:

    git branch dev
    git checkout dev
    

    On the local setup:

    git push 
    

    On remote server:

    git merge dev
    
    0 讨论(0)
  • 2020-11-22 00:37

    I like the idea of still having a usable repository on the remote box, but instead of a dummy branch, I like to use:

    git checkout --detach
    

    This seems to be a very new feature of Git - I'm using git version 1.7.7.4.

    0 讨论(0)
  • 2020-11-22 00:37

    The best way to do this is:

    mkdir ..../remote
    cd ..../remote
    git clone --bare .../currentrepo/
    

    This will clone the repository, but it won't make any working copies in .../remote. If you look at the remote, you'll see one directory created, called currentrepo.git, which is probably what you want.

    Then from your local Git repository:

    git remote add remoterepo ..../remote/currentrepo.git
    

    After you make changes, you can:

    git push remoterepo master
    
    0 讨论(0)
  • 2020-11-22 00:39

    You can simply convert your remote repository to bare repository (there is no working copy in the bare repository - the folder contains only the actual repository data).

    Execute the following command in your remote repository folder:

    git config --bool core.bare true
    

    Then delete all the files except .git in that folder. And then you will be able to perform git push to the remote repository without any errors.

    0 讨论(0)
  • 2020-11-22 00:39

    I'm sure most people viewing this question will stop at the first two huge answers, but I'd still like to offer my solution.

    I had an Eclipse + EGit web project setup when encountering the described error. What helped me was simply using the GitHub app, which seemed to magically resolve the issue. While EGit would always refuse the push, the GitHub desktop app would just shrug its shoulders and push my changes. Maybe it handles the multi-login-situation more gracefully.

    0 讨论(0)
  • 2020-11-22 00:40

    You can get around this "limitation" by editing the .git/config on the destination server. Add the following to allow a git repository to be pushed to even if it is "checked out":

    [receive]
    denyCurrentBranch = warn
    

    or

    [receive]
    denyCurrentBranch = false
    

    The first will allow the push while warning of the possibility to mess up the branch, whereas the second will just quietly allow it.

    This can be used to "deploy" code to a server which is not meant for editing. This is not the best approach, but a quick one for deploying code.

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