225

I've read through countless tutorials and I keep coming up short. Here's what I've got:

  • I'm running RubyMine on my Windows desktop
  • I've installed Git on my WebFaction hosting account per their instructions
  • Git appears to be working fine on both machines

Here's what I'm doing:

  1. On server:
    • mkdir project
    • git init
    • git add .
    • git commit #==> nothing to commit
  2. On client:
    • Create new project in RubyMine
    • Git init in top directory of project
    • Push changes to server #==> failed to push some refs to...

What steps am I missing?

smonff
  • 3,399
  • 3
  • 36
  • 46
Donald Hughes
  • 6,627
  • 8
  • 35
  • 46

10 Answers10

489

On server:

mkdir my_project.git
cd my_project.git
git --bare init

On client:

mkdir my_project
cd my_project
touch .gitignore
git init
git add .
git commit -m "Initial commit"
git remote add origin youruser@yourserver.com:/path/to/my_project.git
git push origin master

Note that when you add the origin, there are several formats and schemas you could use. I recommend you see what your hosting service provides.

Josh Lindsey
  • 8,455
  • 3
  • 24
  • 25
  • The only thing I changed, since I'm working out of RubyMine, is that I replaced the touch .gitignore with creating a rails project with its 66 default files. Thank you very much! – Donald Hughes Feb 25 '10 at 20:50
  • 1
    Kudos for listing the commands. This is how I've set up remote repositories too. – Dave Bacher Feb 25 '10 at 20:55
  • 5
    I should add that if you want other people to collaborate with you on this repo, you should add `--shared` to the end of the `git --bare init` command. This will setup the necessary permissions. – Josh Lindsey Feb 25 '10 at 20:56
  • Thanks for this answer. It is very helpful to have stuff like this that fills in the missing stuff in what are very good books on git, but still don't cover a lot of conversion work from CVS. – octopusgrabbus Nov 30 '11 at 18:43
  • 31
    I like to run `git push --set-upstream origin master` instead of `git push origin master` the first time. This allows me to just type `git push` or `git pull` instead of `git push origin master` every time. Whatever fits your preferences. – Rick Smith Aug 22 '13 at 16:33
  • Or set up an alias & just type something like gp. – Jay Julian Payne Oct 27 '16 at 05:13
  • This almost worked for me. The only thing that I had to add was the --set-upstream or -u flag for the git push origin master. So when I changed to git push -u origin master it worked for me, thanks! – Realhermit Oct 05 '18 at 20:36
  • `git init --bare my_project.git` will create the folder for you and init inside. – Neonit May 13 '21 at 10:36
  • Is that really necessary to do anything serverside? Could we just push to remote in order to create a brand-new remote repo? – andreagalle Jun 03 '22 at 12:23
  • i've been always pushing via `git push -u origin HEAD` - pushes your **current** branch to origin – Raphael Jun 12 '23 at 09:48
10

If your project doesn't have an upstream branch, that is if this is the very first time the remote repository is going to know about the branch created in your local repository the following command should work.

git push --set-upstream origin <branch-name>
bli
  • 7,549
  • 7
  • 48
  • 94
Maria
  • 141
  • 1
  • 6
8

You can try this:

on Server:

adding new group to /etc/group like (example)

mygroup:1001:michael,nir

create new git repository:

mkdir /srv/git
cd /srv/git
mkdir project_dir
cd project_dir
git --bare init (initial git repository )
chgrp -R mygroup objects/ refs/ (change owner of directory )
chmod -R g+w objects/ refs/ (give permission write)

on Client:

mkdir my_project
cd my_project
touch .gitignore
git init
git add .
git commit -m "Initial commit"
git remote add origin youruser@yourserver.com:/path/to/my_project.git
git push origin master

(Thanks Josh Lindsey for client side)

after Client, do on Server this commands:

cd /srv/git/project_dir
chmod -R g+w objects/ refs/

If got this error after git pull:

There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details

git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream new origin/<branch>

try:

git push -u origin master

It will help.

Michael
  • 317
  • 6
  • 11
7

You have to add at least one file to the repository before committing, e.g. .gitignore.

Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100
  • I guess the way I'm trying to use it is to add the files initially from my client, since that's where I write the code. Is that conceptually out of place in git? Do I need to commit on the client first and then push to the server? – Donald Hughes Feb 25 '10 at 20:33
4

@Josh Lindsey already answered perfectly fine. But I want to add some information since I often use ssh.

Therefore just change:

git remote add origin youruser@yourserver.com:/path/to/my_project.git

to:

git remote add origin ssh://youruser@yourserver.com/path/to/my_project

Note that the colon between domain and path isn't there anymore.

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
JWo
  • 650
  • 1
  • 6
  • 23
1

You need to set up the remote repository on your client:

git remote add origin ssh://myserver.com/path/to/project
Dave Bacher
  • 15,652
  • 3
  • 63
  • 86
  • I ran that command, but a "git push origin master" still results in a "failed to push some refs". I tried doing a "git pull origin master" and received a "couldn't find remote ref master". – Donald Hughes Feb 25 '10 at 20:31
  • I'm not sure whether it makes a difference, but my remote repositories are created with `git --bare init` as @Josh Lindsey recommends. – Dave Bacher Feb 25 '10 at 20:54
  • i had the same issue "failed to push some refs"... trying to push master branch from a repo to one i created... then i noticed the source doesn't have a master branch - make sure the branch you are trying to push exists :) – Datum Geek Aug 27 '19 at 17:02
1

I am aware there are existing answers which solves the problem. For those who are new to git, As of 02/11/2021, The default branch in git is "main" not "master" branch, The command will be

git push -u origin main
Dangerous Dev
  • 487
  • 3
  • 7
  • 20
  • It is still an ongoing process, seems to apply to new repositories on GitHub https://github.com/github/renaming and GitLab already https://about.gitlab.com/blog/2021/03/10/new-git-default-branch-name/ – escalator May 17 '21 at 14:33
1

When you have a local git repo and want to add origin on this existing repo:

git remote add origin ssh://myserver.com/path/to/project
git pull origin main --allow-unrelated-histories
git push -u origin main
Vipin
  • 4,851
  • 3
  • 35
  • 65
0

Run below command

git config --local -e

change entry of

url = git@github.com:username/repo.git

to

url = https://github.com/username/repo.git
Esakkiappan .E
  • 525
  • 6
  • 8
0

If you run into the incident as mentioned by @dangerous-dev but you have a local default branch called master and a remote one called main push it using:

git push -u origin master:main

respectively using the long version:

git push --set-upstream origin master:main
Holger Böhnke
  • 971
  • 11
  • 17