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

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

    Summary

    You cannot push to the one checked out branch of a repository because it would mess with the user of that repository in a way that will most probably end with loss of data and history. But you can push to any other branch of the same repository.

    As bare repositories never have any branch checked out, you can always push to any branch of a bare repository.

    There are multiple solutions, depending on your needs.

    Solution 1: Use a Bare Repostiory

    As suggested, if on one machine, you don't need the working directory, you can move to a bare repository. To avoid messing with the repository, you can just clone it:

    machine1$ cd ..
    machine1$ mv repo repo.old
    machine1$ git clone --bare repo.old repo
    

    Now you can push all you want to the same address as before.

    Solution 2: Push to a Non-Checked-Out Branch

    But if you need to check out the code on your remote <remote>, then you can use a special branch to push. Let's say that in your local repository you have called your remote origin and you're on branch master. Then you could do

    machine2$ git push origin master:master+machine2
    

    Then you need to merge it when you're in the origin remote repo:

    machine1$ git merge master+machine2
    

    Autopsy of the Problem

    When a branch is checked out, committing will add a new commit with the current branch's head as its parent and move the branch's head to be that new commit.

    So

    A ← B
        ↑
    [HEAD,branch1]
    

    becomes

    A ← B ← C
            ↑
        [HEAD,branch1]
    

    But if someone could push to that branch inbetween, the user would get itself in what git calls detached head mode:

    A ← B ← X
        ↑   ↑
    [HEAD] [branch1]
    

    Now the user is not in branch1 anymore, without having explicitly asked to check out another branch. Worse, the user is now outside any branch, and any new commit will just be dangling:

          [HEAD]
            ↓
            C
          ↙
    A ← B ← X
            ↑
           [branch1]
    

    Hypothetically, if at this point, the user checks out another branch, then this dangling commit becomes fair game for Git's garbage collector.

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

    Older versions of Git used to allow pushes to the currently checked out branch of a non-bare repository.

    It turns out this was a terribly confusing thing to allow. So they added the warning message you see, which is also terribly confusing.

    If the first repository is just acting as a server then convert it to a bare repository as the other answers recommend and be done with it.

    If however you need to have a shared branch between two repos that are both in use you can achieve it with the following setup

    Repo1 - will act as the server and also be used for development

    Repo2 - will be for development only

    Setup Repo1 as follows

    Create a branch to share work on.

    git branch shared_branch
    

    To be safe, you should also create a $(REPO).git/hooks/update that rejects any changes to anything other than shared_branch, because you don't want people mucking with your private branches.

    repo1/.git/hooks  (GIT_DIR!)$ cat update
    #!/bin/sh
    refname="$1"
    oldrev="$2"
    newrev="$3"
    
    if [ "${refname}" != "refs/heads/shared_branch" ]
    then
       echo "You can only push changes to shared_branch, you cannot push to ${refname}"
       exit 1
    fi
    

    Now create a local branch in repo1 where you will do your actual work.

    git checkout -b my_work --track shared_branch
    Branch my_work set up to track local branch shared_branch.
    Switched to a new branch 'my_work'
    

    (may need to git config --global push.default upstream in order for git push to work)

    Now you can create repo2 with

    git clone path/to/repo1 repo2 
    git checkout shared_branch 
    

    At this point you have both repo1 and repo2 setup to work on local branches that push and pull from shared_branch in repo1, without needing to worry about that error message or having the working directory get out of sync in repo1. Whatever normal workflow you use should work.

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

    What you probably did to cause this:

    This kind of thing happens when you go to bang out a little program. You're about to change something which was already working, so you cast your level-3 spell of perpetual undoability:

    machine1:~/proj1> git init
    

    and you start adding/committing. But then, the project starts getting more involved and you want to work on it from another computer (like your home PC or laptop), so you do something like

    machine2:~> git clone ssh://machine1/~/proj1
    

    and it clones and everything looks good, and so you work on your code from machine2.

    Then... you try to push your commits from machine2, and you get the warning message in the title.

    The reason for this message is because the git repo you pulled from was kinda intended to be used just for that folder on machine1. You can clone from it just fine, but pushing can cause problems. The "proper" way to be managing the code in two different locations is with a "bare" repo, like has been suggested. A bare repo isn't designed to have any work being done in it, it is meant to coordinate the commits from multiple sources. This is why the top-rated answer suggests deleting all files/folders other than the .git folder after you git config --bool core.bare true.

    Clarifying the top-rated answer: Many of the comments to that answer say something like "I didn't delete the non-.git files from the machine1 and I was still able to commit from machine2". That's right. However, those other files are completely "divorced" from the git repo, now. Go try git status in there and you should see something like "fatal: This operation must be run in a work tree". So, the suggestion to delete the files isn't so that the commit from machine2 will work; it's so that you don't get confused and think that git is still tracking those files. But, deleting the files is a problem if you still want to work on the files on machine1, isn't it?

    So, what should you really do?

    Depends upon how much you plan to still work on machine1 and machine2...

    If you're done developing from machine1 and have moved all of your development to machine2... just do what the top-rated answer suggests: git config --bool core.bare true and then, optionally, delete all files/folders other than .git from that folder, since they're untracked and likely to cause confusion.

    If your work on machine2 was just a one-time thing, and you don't need to continue development there... then don't bother with making a bare repo; just ftp/rsync/scp/etc. your files from machine*2* on top of the files on machine*1*, commit/push from machine*1*, and then delete the files off of machine*2*. Others have suggested creating a branch, but I think that's a little messy if you just want to merge some development you did on a one-time basis from another machine.

    If you need to continue development on both machine1 and machine2... then you need to set things up properly. You need to convert your repo to a bare, then you need to make a clone of that on machine1 for you to work in. Probably the quickest way to do this is to do

    machine1:~/proj1> git config --bool core.bare true
    machine1:~/proj1> mv .git/ ../proj1.git
    machine1:~/proj1> cd ..
    machine1:~> rm -rf proj1
    machine1:~> git clone proj1.git
    machine1:~> cd proj1
    

    Very important: because you've moved the location of the repo from proj1 to proj1.git, you need to update this in the .git/config file on machine2. After that, you can commit your changes from machine2. Lastly, I try to keep my bare repos in a central location, away from my work trees (i.e. don't put 'proj1.git' in the same parent folder as 'proj1'). I advise you to do likewise, but I wanted to keep the steps above as simple as possible.

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

    An article I found that might be useful to others is Git in 5 minutes.

    I had an Xcode project under Git version control that I wanted to push up to a Virtual Distributed Ethernet (VDE) I have in a DC. The VDE runs Centos 5.

    None of the articles I read about Git talked about bare repositories. It all sounded so simple until I tried what I thought should be easy coming from an SVN background.

    The suggestions here to make the remote repository bare worked. Even better for my requirements was to clone the Xcode project to projectname.git, copy that to the remote server; then pushes magically worked. The next step will be getting Xcode to push without errors about commits, but for now I'm okay doing it from Terminal.

    So:

    cd /tmp (or another other directory on your system)<br/>
    git clone --bare /xcode-project-directory projectname.git<br/>
    scp -r projectname.git sshusername@remotehost.com:repos/<br/>
    

    To push changes from your Xcode project after you've committed in Xcode:

    cd /xcode-project-directory<br/>
    git push sshusername@remotehost.com:repos/projectname.git<br/>
    

    I'm certain there is a smoother more sophisticated way of doing the above, but at a minimum this works. Just so everything is clear, here are some clarifications: /xcode-project-directory is the directory your xcode project is stored in. It's probably /Users/Your_Name/Documents/Project_Name. projectname is literally the name of the project, but it can be anything you care to call it. Git doesn't care, you will.

    To use scp you need to have a user account on the remote server that's allowed SSH access. Anyone running their own server will have this. If you're using shared hosting or the like, you might be out of luck.

    remotehost.com is the name of your remote host. You could as easily use its IP address. Just for further clarity I'm using Gitosis on the remote host with SSH keys, so I'm not prompted for passwords when I push. The article Hosting Git Repositories, the Easy (and Secure) Way tells you how to set all that up.

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

    git config --local receive.denyCurrentBranch updateInstead

    https://github.com/git/git/blob/v2.3.0/Documentation/config.txt#L2155

    Use that on the server repository, and it also updates the working tree if no untracked overwrite would happen.

    It was added in Git 2.3 as mentioned by VonC in the comments.

    I've compiled Git 2.3 and gave it a try. Sample usage:

    git init server
    cd server
    touch a
    git add .
    git commit -m 0
    git config --local receive.denyCurrentBranch updateInstead
    
    cd ..
    git clone server local
    cd local
    touch b
    git add .
    git commit -m 1
    git push origin master:master
    
    cd ../server
    ls
    

    Output:

    a
    b
    

    Yay, b got pushed!

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

    I just ran into this problem with a deployment git repository on Heroku.

    I don't know why Heroku has a non-bare repository on their side, but as a workaround I was able to reset the remote repository, and reupload.

    You shouldn't use Heroku's copy of your repository as your only git repository for collaboration, but just in case, I'll say clearly: Do not do this unless you are sure you have a full copy of your repository stored securely somewhere other than Heroku. Doing a reset will delete the repository contents.

    To reset:

    1. Install the Heroku toolbelt (which contains the command line client) if you haven't already.
    2. Install the heroku-repo plugin if you haven't already.

      heroku plugins:install https://github.com/heroku/heroku-repo.git
      
    3. Do the reset, which deletes the repository and creates a new, empty one

      heroku repo:reset
      
    4. Push to your Heroku remote as you normally would; it will reupload everything.

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