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

前端 未结 30 2013
半阙折子戏
半阙折子戏 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:18

    For me working solution is:

    ON REMOTE:

    git checkout -b some_tmp_name
    

    ON LOCAL:

    git push
    

    ON REMOTE:

    git checkout master
    git branch -d some_tmp_name
    

    But this is not the real solution it's just workaround.

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

    Just in case someone finds it useful. For me it was a git server permissions issue. I checked out the project from the beggining and push a simple file and then I got the "Push rejected: Push to origin/master was rejected"

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

    You have 3 options

    1. Pull and push again:

      git pull; git push
      
    2. Push into different branch:

      git push origin master:foo
      

      and merge it on remote (either by git or pull-request)

      git merge foo
      
    3. Force it (not recommended unless you deliberately changed commits via rebase):

      git push origin master -f
      

      If still refused, disable denyCurrentBranch on remote repository:

      git config receive.denyCurrentBranch ignore
      
    0 讨论(0)
  • 2020-11-22 00:20

    You will need to change the config file on the remote server once you have created empty(bare) repository, say

    root@development:/home/git/repository/my-project# cat config 
    

    there you will see

    [core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    

    You will make this bare to false to true and I removed logallrefupdates = true (not sure of its use!)

    to

    [core]
    repositoryformatversion = 0
    filemode = true
    bare = true
    

    You may test following

    $ git remote show origin
    * remote origin
    Fetch URL: my-portal@development:/home/XYZ/repository/XYZ
    Push  URL: my-portal@development:/home/XYZ/repository/XYZ
    HEAD branch: (unknown)
    

    This HEAD branch: (unknown) will be shown if you are unable to PUSH. So if the HEAD branch is unknow, you should change bare to true and after push successful you can reuse the

    git remote show origin
    

    and you will see

     HEAD branch: master
    
    0 讨论(0)
  • 2020-11-22 00:22

    With a few setup steps you can easily deploy changes to your website using a one-liner like

    git push production
    

    Which is nice and simple, and you don't have to log into the remote server and do a pull or anything. Note that this will work best if you don't use your production checkout as a working branch! (The OP was working within a slightly different context, and I think @Robert Gould's solution addressed it well. This solution is more appropriate for deployment to a remote server.)

    First you need to set up a bare repository somewhere on your server, outside of your webroot.

    mkdir mywebsite.git
    cd mywebsite.git
    git init --bare
    

    Then create file hooks/post-receive:

    #!/bin/sh
    GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f
    

    And make the file executable:

    chmod +x hooks/post-receive
    

    On your local machine,

    git remote add production git@myserver.com:mywebsite.git
    git push production +master:refs/heads/master
    

    All set! Now in the future you can use git push production to deploy your changes!

    Credit for this solution goes to http://sebduggan.com/blog/deploy-your-website-changes-using-git/. Look there for a more detailed explanation of what's going on.

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

    Using this to push it to the remote upstream branch solved this issue for me:

    git push <remote> master:origin/master
    

    The remote had no access to the upstream repo so this was a good way to get the latest changes into that remote

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