287

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 alice
Cloning into alice...
done.
warning: You appear to have cloned an empty repository.
$ cd alice/
$ touch a
$ git add a
$ git commit -m "Added a"
[master (root-commit) 70d52d4] Added a
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to '/work/fun/git_experiments/bare'

Doesn't git push always push to the repository I cloned from?

3
  • Shouldn't you specify branch to push?
    – Rekin
    Commented May 27, 2011 at 21:14
  • 3
    not after a clone!!! after the problem is fixed, it works great and no need to specify the branch...just on this first checkout of an empty repository does this occur which is VERY VERY annoying...they should fix this issue. Commented May 23, 2012 at 16:49
  • Hope this post would be useful to somebody when trying to do above- samranga.blogspot.com/2015/07/… The error in the question can be popped even when tried to Create a git BitBucket repository from an already locally existing project Commented Jul 2, 2015 at 13:08

6 Answers 6

487

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.

12
  • 1
    I'm doing sudo apt-get upgrade git-core and sudo apt-get upgrade git and it think no update is necessary. git --version returns 1.7.3.1. Any idea what's missing? I admit currently apt-get update doesn't work for me, but it did not too long ago.
    – ripper234
    Commented May 27, 2011 at 21:35
  • 1
    @ripper234: The current version of git is 1.7.5.3 You can either live with the inconvenience, use a different workflow, or install the latest git manually w/o debian/ubuntu packaging. Commented May 27, 2011 at 21:37
  • Ah right, I forget that software take some time before it gets packaged. I'm a linux noob, coming from Windows, and used to click-here-to-install-the-newest-version.
    – ripper234
    Commented May 27, 2011 at 21:47
  • 9
    Regarding “Recent version don't have this problem”: Even in recent versions, the default for pushes does not seem to have changed from matching; maybe you have push.default set to upstream/tracking (or current) in your ~/.gitconfig? Commented May 28, 2011 at 6:11
  • 4
    Try git push origin master:master to make it explicit. If that doesn't work, check to see what branch you are on: git branch perhaps you have not made the first commit or you made that commit on a branch other than master. Commented Feb 22, 2014 at 16:37
42

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
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
19
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.

5
  • ...so you downvoted the answer? It is the standard way to push everything to a new, bare repository. If it didn't work for you, there is some other issue.
    – ebneter
    Commented May 28, 2011 at 7:59
  • You're right, I probably shouldn't have, I know you were just trying to help. If you edit it I'll undo my downvote.
    – ripper234
    Commented May 28, 2011 at 11:14
  • Thanks, edited it with another way to accomplish the same task.
    – ebneter
    Commented May 29, 2011 at 4:03
  • Your answer helped me thanks ;) But at the end of the command, the path must be present like this: git push --all ../test_repo the repo's URL at the end of the command ;)
    – Metafaniel
    Commented Aug 29, 2012 at 21:47
  • @Metafaniel That depends on how you set it up. If your local repo already has the remote properly configured, "git push --all" should work as is.
    – ebneter
    Commented Aug 29, 2012 at 23:55
7

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).

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.