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

There is loads of documentation and posts on Git out there so this is more of a note to self as I keep forgetting the steps to setting 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 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
git-track origin

Note: the ‘git-track’ command is a bash function we use to save manually editing the .git/config file (add the following to your ~/.bash_profile file as outlined by darkliquid):

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function git-track {
  CURRENT_BRANCH=$(parse_git_branch)
  git-config branch.$CURRENT_BRANCH.remote $1
  git-config branch.$CURRENT_BRANCH.merge refs/heads/$CURRENT_BRANCH
}

h4. Bonus

To have your terminal prompt display what branch you are currently on in green, add the following to your ~/.bash_profile:

function parse_git_branch_and_add_brackets {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="\h:\W \u\[\033[0;32m\]\$(parse_git_branch_and_add_brackets) \[\033[0m\]\$ "

24 comments

  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. I liked your tips on how to have your terminal prompt display any Git branch name. Added it to my bashrc.

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

  4. Thanks! I’ve added the bit about how to create the empty remote repository, to the erratum for the relevant section of the book “Pragmatic Version Control Using Git” (http://pragprog.com/titles/tsgit/errata)

    Your info was exactly what I was looking for. Cheers!

  5. Thanks so much for this. Been looking around for how to setup a bare repo and do that initial push.

  6. thx! Great tip!

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

  8. @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.

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

  10. YES! Finally it works! Thank you for the great tipps! I love the colored branch name in my command line!

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

  12. @Yoichi Yes that is correct, sorry I should have been more explicit.

  13. Some good ideas. I’m looking forward to adding them to my GIT toolkit.

  14. This works fine with bitbucket.org
    Thanks :)

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

  16. One Word…Godsend! Thank you!

  17. Fantastic!! thank you, it’s been so hard to find a writeup this concise… all i needed were these steps!

Leave a comment