Donald Hughes
Donald Hughes

Reputation: 6927

How do I do an initial push to a remote repository with Git?

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

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?

Upvotes: 246

Views: 565028

Answers (10)

Vipin
Vipin

Reputation: 5223

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

Upvotes: 5

Holger Böhnke
Holger Böhnke

Reputation: 1130

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

Upvotes: 1

Esakkiappan .E
Esakkiappan .E

Reputation: 565

Run below command

git config --local -e

change entry of

url = [email protected]:username/repo.git

to

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

Upvotes: 0

Dangerous Dev
Dangerous Dev

Reputation: 527

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

Upvotes: 3

JWo
JWo

Reputation: 593

@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 [email protected]:/path/to/my_project.git

to:

git remote add origin ssh://[email protected]/path/to/my_project

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

Upvotes: 4

Maria
Maria

Reputation: 161

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>

Upvotes: 12

Michael
Michael

Reputation: 337

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 [email protected]:/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.

Upvotes: 9

Josh Lindsey
Josh Lindsey

Reputation: 8793

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 [email protected]:/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.

Upvotes: 520

Dave Bacher
Dave Bacher

Reputation: 15972

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

git remote add origin ssh://myserver.com/path/to/project

Upvotes: 1

Benjamin Wohlwend
Benjamin Wohlwend

Reputation: 31818

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

Upvotes: 7

Related Questions