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\] \$ "

Published by

Jamie

Hi, I am the Managing Director of SonicIQ Limited in the UK. I have been working in the web development industry since 1999 and have been running SonicIQ since 2001. Currently Ruby On Rails is my preferred development platform. I am experienced in designing with web standards, HTML5, CSS3 and Javascript.

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

  1. That is a great bonus tip! That little ~3 liner will save me countless (50, 60, who knows?!) gba (git branch all) calls in a day. I’ve actually trained myself that to ‘gba’ whenever I enter into a git directory to know which of the several branches I left myself in.

    I think you just made my evening…. and probably even my week. Thank you, thank you, thank you. :)

  2. Nice tip.. I am new bee.. and couldn’t understand the so many commands. :)
    Thanks for nice post . keep posting

  3. Thanks for the good tip! I’ve been a Subversion guy for a long time and decided to give git a try for my latest project. I’m still not completely sold, but the info you’ve given has made the initial setup a bit easier.

  4. @Jonathan No problem at all, glad it helped. I struggled with this basic stuff when I switched to Git too, it’s a whole other mindset to Subversion.

  5. I beg your parden but can you clarify whether I need to create a user account “git” and makdir my_project.git in its home directory?

    (I presume that you mean that but it helps to state it.)

  6. Instead of requiring the git-track function that you have created, you can just do this instead:

    git push -u origin master

    The remote branch is then automatically tracked. Thanks for the help creating my remote repo though :o)

  7. In

    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

    The server does not recognize “git-update-server-info” but does appear to accept “git update-server-info”

    When I try the second paragraph,

    “git push origin master”

    I get:

    ! [remote rejected] master -> master (branch is currently checked out)

    Any ideas? Why is it trying to push “master” when I’m telling it “origin”?
    I even logged back into my server and try to create a branch called “origin”, it returns

    $git branch origin
    fatal: Not a valid object name: ‘master’.

    Also tried to commit (since it said “branch is currently checked out”) and I got this

    $git commit -a
    fatal: This operation must be run in a work tree

    Thanks

  8. Thanks for the “push -u” pointer @frak! I had figured out how to do:
    “git push –all origin” and then
    “git branch –set-upstream master origin/master”
    — I didn’t realize you could combine the two as
    “git push -u –all origin”

  9. Thanks for the tips

    My situation: local branch in my laptop, cloned from origin git repo. On same git repo cloned on an other host. I want to see the git log on this host from my latopop:

    > git remote add myTag ssh://user@host:/path/to/git/repo
    > # Log of origin ok
    > git log -n 5 origin
    > But not on remote host
    > git log -n 5 myTag
    > fatal: ambiguous argument ‘myTag': unknown revision or path not in the working tree.
    > Use ‘–‘ to separate paths from revisions

  10. I would be inclined to to a gig log to show recent commits, then git checkout [the hash of the commit to go back to]. Then commit these changes to master and push.

  11. So I did everything as you said, but when I push something to my remote repository from my local machine, there’s nothing in the remote repository although the push itself seems to be a success. Master-branch is the only branch on the remote repo and there’s nothing I pushed there. What am I doing wrong?

  12. @Damnit The instructions are to setup a bare repository on the remote machine, this means that there will be no working copy and everything is stored within the ‘.git’ repository directory. If you try cloning the remote repo with ‘git clone user@host:repo.git’, you will most likely find it works as expected.

    If you want the remote machine to have a working copy, just leave off the ‘–bare’ option when initialising the repo. Hope this helps.

  13. Hi,
    I new to git now i can push to the local machine but how to push to the another machine. and
    what is example.com in
    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

  14. In the example, example.com is a domain name pointing to the server you wish to push to. This could just as easily be an IP address.

    The example also assumes a ‘git’ user, so using an IP address you’d end up with something like: ‘ssh git@1.2.3.4′.

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>