Simplify management of multiple Git repos with SSH
For those interested in a more in-depth explanation, see my blog post.
For me, starting a git project is as easy as ginit gh epic-project
.
Use multiple git endpoints
I use three Git endpoints from two different websites, and the aliases here map to the actual URLs via my SSH config, allowing me to use semantic shortcuts. The first function shown here acts like a 'helper', allowing me to specify which endpoint a repo should use.
# Test the first argument of a command for a git host identifier (aliases configured in ssh-config)
is_git_host() {
case "$1" in
'gh') host='github-personal'
user='SteveBenner'
true ;;
'bb') host='bitbucket'
user='SteveBenner09'
true ;;
'xf') host='github-work'
user='xFactorApplications'
true ;;
*) echo 'Usage: clone [host] [repository-name]'
echo "where <host> is one of:"
echo ' gh: Personal GitHub account'
echo ' xf: X-Factor Applications GitHub account'
echo ' gh: personal BitBucket account'
false ;;
esac
}
Do everything with one command
Using CLI tools shouldn't be a complex process; always make the computer do the heavy lifting. The following commands take care of everything you need to start a new repo or clone an existing one.
# Clone a git repo using hostname aliases resolved via SSH configuration
clone() { if is_git_host $1 ; then `git clone git@${host}:${user}/${2}.git` ; fi }
# Initialize a new local git repository, setting the remote using given alias
ginit() {
if is_git_host $1 ; then
git init
`git remote add origin git@${host}:${user}/${2}.git`
echo "# This repo and README were created automatically." >> README.md
cp -n ~/.gitignore_global .gitignore
git add -A
git commit -m "Initial commit."
git push -u origin master
`git config core.filemode false` # see my blog post for explanation of this line
fi
}
P.S. The code looks a bit messy due to the smaller width CoderWall uses - on github, it's a lot cleaner.
Written by Stephen Benner
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Alias
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#