I wanted to write a quick cribsheet of the GIT commands I do when starting a new project, 'cos I'm forgetful.

I'm not saying what I do is a good process, it's just what I've found myself doing when I start a new project, it doesn't follow any specific guidelines I've seen so far, but then it also doesn't seem to be completely wrong, so caveat lector*

A quick note about my git setup

I have my "origin" repo tree on a local NAS box, shared with the name gitrepos, then I mount that share on any box I do dev or deployment work on, as the folder /mnt/nas/gitrepos.

This means that as far as git commands are concerned, the origin repos are all under a local directory tree, no SSH or HTTPs as far as they're aware, but I'm still doing "proper" git style by having local and remote repos and push/pulling between them.

So, whenever I start a new project, it's initial moments of life are usually me making a new directory and hacking about on some random files. Maybe I'll be copying from another project, mashing keys in some REPL terminal until something sane appears, or even (in rare organised moments) doing a proper project creation command like mvn archetypes:create or going to Spring Boot's Initializr, or maybe even using some cool code generator like Jekyll, Yeoman or JHipster. I guess I may even be using a big IDE and make use of their project creation templates.

Whatever those initial shakey hacking moments, at some point I'll be looking at a pile of hacked together code which actually does something I might want to develop, or at least keep for amusement. At that point, I realise I should have started putting things in source control earlier.

Ah well, it's never too late and at least I remembered before the disk died or I fat-fingered a rm command. So, I should 1 tidy up, 2 make a local git repo and 3 stick my code in the local repo.

~/projects/coolproj $ mvn clean
~/projects/coolproj $ git init
~/projects/coolproj $ git add .
~/projects/coolproj $ git commit -am 'First commit. When will I learn?'
~/projects/coolproj $ git status

Hopefully that git status tells me everything is fine. If I've accidentally committed a bunch of cruft, I can just bin the new .git directory, tidy up properly and redo-from-start. Possibly creating a .gitignore file before the add command. Maybe even using gitignore.io - kids these days have it so easy!

Well, that's a local repo, so I can start committing, branching, diffing and all that good stuff if I want. But it's only on my local disk, I want to get this onto my NAS where the magic RAID and backup gnomes can guard it from all harm. So before doing any more hacking I'll 4 create a remote repo, 5 tell the local repo about it and 6 push the local code into it.

~/projects/coolproj $ git init --bare /mnt/nas/gitrepos/coolproj
~/projects/coolproj $ git remote add origin /mnt/nas/gitrepos/coolproj
~/projects/coolproj $ git push origin master
~/projects/coolproj $ git status
~/projects/coolproj $ git diff origin/master

Hopefully that git diff just returns with nothing. If so, I'm done - everything is in local and remote repos, all up to date, and I'm ready to carry on hacking away at the code.

* Latin for "don't listen to me, I have no idea what I'm talking about"

Previous Post Next Post

© Me. Best viewed with a sense of humour and a beer in hand.