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

3 comments ↓

#1 Showing the current Git branch in your bash prompt « duesenklipper on 12.02.08 at 2:48 pm

[...] I came across a neat way of always being aware of what Git branch you’re working on: just show it in your bash prompt. I like the idea, but the way it’s shown gives you a completely new prompt, overwriting your [...]

#2 Nathaniel Bibler on 12.03.08 at 3:55 am

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

#3 Magnus on 11.29.09 at 7:26 am

I liked your tips on how to have your terminal prompt display any Git branch name. Added it to my bashrc.

Leave a Comment