Git – setting up a remote repository and doing an initial push

There is a great deal of documentation and many posts on Git out there, so this is more of a note to self as I keep forgetting the steps needed to set up a remote repository and doing an initial “push”.

So, firstly setup the remote repository:


ssh git@example.com
mkdir my_project.git
cd my_project.git
git init --bare
git update-server-info # If planning to serve via HTTP
exit

On local machine:


cd my_project
git init
git add *
git commit -m "My initial commit message"
git remote add origin git@example.com:my_project.git
git push -u origin master

Done!

Team members can now clone and track the remote repository using the following:


git clone git@example.com:my_project.git
cd my_project

Bonus

To have your terminal prompt display what branch you are currently on in green, add the following to your ~/.bash_profile (I have my current directory displayed in cyan):


function git-branch-name {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "

97 thoughts on “Git – setting up a remote repository and doing an initial push”

  1. Pingback: my git reminder
  2. Make sure the git user and group owns the repository directory on the remote. If it doesn’t, use the chown command to make the proper changes.

  3. Ah, you have to create the remote project first. I skipped that step thinking it would be created automatically with the first push. Of course, it took a few more dead ends to figure out how to get everything in sync again since the remote repository was created after the local one.

    It’s not difficult, just a little confusing figuring it out.

    It would be helpful to update the post for anyone who might make the same mistake. ;-)

    Thanks for the tip!

  4. Hi,
    I am trying the following and getting the request to enter password. Which password is this ? I am not able to log in with the password which I used for the server at bootup.

    sudo git push -u origin master
    git@192.168.1.81‘s password:

    Syed

  5. Hi,
    I am gettting the following error . The only difference in steps is that I had to use “git add .” instead of “git add *”.

    mohsin@KubuntuVM:~/my_project$ sudo git push -u origin master
    error: src refspec master does not match any.
    error: failed to push some refs to ‘git@192.168.1.81:/opt/my_project.git’

    Mohsin

  6. Hi Jamie,

    I think the error comes when you have no files in my_project. Once I added a file to my_project/ and repeated the procedure it all worked.

    One final question on terminal prompt displaying what branch you are currently on.

    I am using Kubuntu and could not find ~/.bash_profile file. I only have ~/.profile. Do we have to add the code to the ~/.profile ?

    Also, are any changes required or just copy the exact code ? I am asking because, I did add the lines to my .profile and could not login the next time.

    Thanks,
    Mohsin

  7. @Mohsin Adding to the ~/.profile file should do the trick… I will post my updated prompt among other things in a few days.

  8. This was really great thank you, first time getting into git today and I found this very helpful! Great resource for a learner and well explained, consider yourself bookmarked!
    Thanks :D

  9. Fantastic, really quick to pick up. Just one thing – I had to put a ‘/’ in front of the path to the *.git folder on the server e.g.

    git remote add origin git@example.com:/y_project.git

    Cheers!

  10. Thanks for putting this up. I find myself searching every time I need to do this, and the last couple times have ended up here. Cheers.

  11. There is a great deal of documentation and many posts on Git out there, so this is more of a note to self

    And yet, this was the first google entry for the search “git push to new remote repository”. Congrats.

  12. Thank you for the write-up — it put me on the right track. Please consider updating your write-up to include an explanation of the post-receive hook and how to implement this and where to save it — something like this:

    Create a file post-receive in the hooks directory of your repository:

    #!/bin/sh
    GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

    Make sure the target directory exists, as Git won’t create it for you. Finally, set permissions on the file:

    $ chmod +x hooks/post-receive

  13. What I like about this article is that it is simple, to the point, and has none of the the cruft a lot of other tuts on the same subject have. Brilliant!

  14. Dear Lucid,
    do not assume anything. If something can go wrong it will definitely go wrong and it always goes wrong.

    You could add a few lines when explaining how to set up the initial repository on the remote server, stating with CAPITALS that:

    YOU GUYS NEED FIRST TO CREATE A git USER give it a password which you are going to share with your friends and use the following command for permissions or whatever.

    Otherwise people are confused.

  15. Hi, first thank you for your instructions, Git is going difficult to me.
    I follow your steps carefully but when finally I push my local files to the remote bare repository it looks like run well, I meen I took a bit time but the files are not sent to the remote server project folder???

    It is necesary to push there by using FTP??

    Thanks in advance!!
    Rubén

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>