Why can't I push to this bare repository?

后端 未结 6 1927
生来不讨喜
生来不讨喜 2020-12-07 07:10

Can you explain what is wrong with this workflow?

$ git init --bare bare
Initialized empty Git repository in /work/fun/git_experiments/bare/
$ git clone bare         


        
相关标签:
6条回答
  • 2020-12-07 07:32

    Yes, the problem is that there are no commits in "bare". This is a problem with the first commit only, if you create the repos in the order (bare,alice). Try doing:

    git push --set-upstream origin master
    

    This would only be required the first time. Afterwards it should work normally.

    As Chris Johnsen pointed out, you would not have this problem if your push.default was customized. I like upstream/tracking.

    0 讨论(0)
  • 2020-12-07 07:33

    This related question's answer provided the solution for me... it was just a dumb mistake:

    Remember to commit first!

    https://stackoverflow.com/a/7572252

    If you have not yet committed to your local repo, there is nothing to push, but the Git error message you get back doesn't help you too much.

    0 讨论(0)
  • 2020-12-07 07:35
    git push --all
    

    is the canonical way to push everything to a new bare repository.

    Another way to do the same thing is to create your new, non-bare repository and then make a bare clone with

    git clone --bare
    

    then use

    git remote add origin <new-remote-repo>
    

    in the original (non-bare) repository.

    0 讨论(0)
  • 2020-12-07 07:44

    I use SourceTree git client, and I see that their initial commit/push command is:

    git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags --set-upstream origin master:master
    
    0 讨论(0)
  • 2020-12-07 07:46

    If you:

     git push origin master
    

    it will push to the bare repo.

    It sounds like your alice repo isn't tracking correctly.

    cat .git/config
    

    This will show the default remote and branch.

    If you

     git push -u origin master
    

    You should start tracking that remote and branch. I'm not sure if that option has always been in git.

    0 讨论(0)
  • 2020-12-07 07:56

    Try this in your alice repository (before pushing):

    git config push.default tracking
    

    Or, configure it as the default for your user with git config --global ….


    git push does default to the origin repository (which is normally the repository from which you cloned the current repository), but it does not default to pushing the current branch—it defaults to pushing only branches that exist in both the source repository and the destination repository.

    The push.default configuration variable (see git-config(1)) controls what git push will push when it is not given any “refspec” arguments (i.e. something after a repository name). The default value gives the behavior described above.

    Here are possible values for push.default:

    • nothing
      This forces you to supply a “refspec”.

    • matching (the default)
      This pushes all branches that exist in both the source repository and the destination repository.
      This is completely independent of the branch that is currently checked out.

    • upstream or tracking
      (Both values mean the same thing. The later was deprecated to avoid confusion with “remote-tracking” branches. The former was introduced in 1.7.4.2, so you will have to use the latter if you are using Git 1.7.3.1.)
      These push the current branch to the branch specified by its “upstream” configuration.

    • current
      This pushes the current branch to the branch of the same name at the destination repository.

      These last two end up being the same for common cases (e.g. working on local master which uses origin/master as its upstream), but they are different when the local branch has a different name from its “upstream” branch:

      git checkout master
      # hack, commit, hack, commit
      
      # bug report comes in, we want a fix on master without the above commits
      
      git checkout -b quickfix origin/master  # "upstream" is master on origin
      # fix, commit
      git push
      

      With push.default equal to upstream (or tracking), the push would go to origin’s master branch. When it is equal to current, the push would go to origin’s quickfix branch.

    The matching setting will update bare’s master in your scenario once it has been established. To establish it, you could use git push origin master once.

    However, the upstream setting (or maybe current) seems like it might be a better match for what you expect to happen, so you might want to try it:

    # try it once (in Git 1.7.2 and later)
    git -c push.default=upstream push
    
    # configure it for only this repository
    git config push.default upstream
    
    # configure it for all repositories that do not override it themselves
    git config --global push.default upstream
    

    (Again, if you are still using a Git before 1.7.4.2, you will need to use tracking instead of upstream).

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