About Me

Curriculum Vitae

A brief list of my current skill set

Bloggybits

Gosh This Site Is Old
Thursday, 18th November 2021, 22:08

I might update this one day, but until I do take a lot of it with a pinch of salt!

Automatically Cropping Images is Hard
Monday, 21st October 2013, 19:00

But maybe we can use face detection?

The Git Cheat Sheet
Friday, 6th September 2013, 11:30

for github, bitbucket, that kinda stuff

CoffeeScript and TypeScript are a Burden
Saturday, 17th August 2013, 11:21

Be sure you understand the cons as well as the pros

Changing the Order of the jQuery Event Queue
Wednesday, 3rd July 2013, 20:27

It's just a push to the left

How Do Spammers Get My Email Address?
Wednesday, 15th May 2013, 18:03

I think these days I have a pretty good idea

XSLT, node.js 0.10 and a Fun Two Days of Native Modules and Memory Leaks
Thursday, 25th April 2013, 17:14

documentation makes things less cryptic, so lets not write much of it

Fixing CentOS high cpu usage when running as a virtual machine under VirtualBox
Sunday, 21st April 2013, 20:28

innotek rocks! I mean Sun... I mean Oracle...

Repairing a dK'Tronics Keyboard and Scoping Out a ZX Spectrum 48k - Part One
Sunday, 17th March 2013, 23:51

What signals inside of it actually look like

Tabs vs Spaces and Why You Should Always Use Tabs
Monday, 4th March 2013, 19:51

Spaces are bad, just real bad

Projects and Sillyness

MAME Cabinet Diary

How I built my own arcade cabinet

Loading Screen Simulator

I don't miss the ZX Spectrum, I still use it!

The Little Guy Chat Room

It's a Pitfall inspired chat room

GPMad MP3

A fully featured MP3 player what I wrote

GP Space Invaders

My first little emulator

GP32 Development Page

Some info and links about this cute little handheld

Disney Nasties

Uncensored images, you must be 18 to view them

Diary of a Hamster

Learn about how hamsters think, first hand

Utilities

Time Calculator

A simple little online utility for working out how many hours to bill a client

A Few Links

The Git Cheat Sheet
Friday, 6th September 2013, 11:30

Believe it or not, Git is actually quite simple 99% of the time. You just need to do certain things properly and avoid certain other things, mostly though it won't let you do anything damaging beyond making a mess of your local repository.

Whilst there are UIs out there which help, you can't beat the command-line, not because it's more geeky and cool, though perhaps some might argue it is. No, it's best for two simple reasons, control and information. It gives you complete control without hiding away features, and it gives you all the information you can stomach without hiding any issues.

But it isn't always easy to find a good guide to all the git commands you'll need 99% of the time, so here is a quick one, based on the simple fact I have a terrible memory so I end up making a list for when I forget.


Quick Terminology

I'm not here to explain Git, but just to make sure we are on the same page here are a few terms used in this article:

Repository - a git repository, that exists either locally on your own PC, or on a remote server somewhere
Local - stuff on your own computer
Remote - stuff on a remote server somewhere
Branch - either the main set of code, or an off-shoot based on the main set of code
Master - the main branch
Commit - saving your latest changes to a branch, also the act of doing a commit
Head - the most up to date committed code of a branch, aka the tip


Downloading a Repository

Real simple one this, you'll use it when you start work on someone elses repository, want to just get a copy of a remote repository into a subfolder of the current directory:

    git clone URL

examples:

    git clone https://github.com/RobeeeJay/Fastworks.js.git
git clone git@github.com:RobeeeJay/Fastworks.js.git


Creating a New Repository

If you are inside the root directory that you want to make a repository of, then you can create a new repository ready to add files into it with this:

    git init

That creates the .git subdirectory. If you want to upload this to a new remote repo, you need to add that destination with a command like this:

    git remote add origin URL

examples:

    git remote add origin https://github.com/RobeeeJay/Fastworks.js.git
git remote add origin git@github.com:RobeeeJay/Fastworks.js.git


Any Command From Now On

Just something worth noting, all the commands from now on can be done anywhere inside any directory or subdirectory of a repo. It doesn't really matter! So don't feel you always have to visit the root directory to do use them. Hang around wherever you like, just as long as it's inside the repo somewhere.


Changing Branches Within a Repository

You'll be doing this a lot, it changes your local repository between all the branches it is currently aware of.

    git checkout BRANCHNAME

examples:

    git checkout master
git checkout 0.8.3
git checkout 798ed8a00350a209a77407a53bce935133e06d5c

As the cryptic hash on that last example shows, you can also use it to checkout individual commits in case you need to work out how far back you need to go for a major bug to dissappear. You can also use it to check out any tagged commits, tags being useful ways to mark a state-in-time of a codebase so you don't have to use hashes like the above.


Updating Branches and Changes From the Remote

Git isn't automatic, it doesn't know about any new branches or changes on the server. To get those you have to tell it to fetch them:

    git fetch

This will not change any local code, it just makes the local repo aware of any changes to the remote.


Creating a New Branch

Only a lone gunman works on master, and even then it's handy to try out new things without polluting your release code with untested bugs. Doing this involves creating branches, and every branch is based off another, it's like a tree with the master branch being the main trunk.

    git checkout -b NEWBRANCHNAME PARENTBRANCH

examples:

    git checkout -b newfeature master
git checkout -b 0.8.4 master
git checkout -b experiments 0.8.4


Deleting a Local Branch

Maybe you made one by mistake, maybe it's so old and you're done with it and don't need it anymore. Any branch other than master can be nuked.

    git branch -d BRANCHNAME

examples:

    git branch -d newfeature
git branch -d 0.8.4


Deleting a Remote Branch

The above only deletes them locally, any you or someone else pushed to the remote repo will remain there until deleted. You can do this like so:

    git push origin --delete BRANCHNAME

examples:

    git push origin --delete newfeature
git push origin --delete 0.8.4


Deleting Local Branches No Longer on Remote

So someone tidied up the remote and you want all branches on your local repo that are no longer on remote to go too. BE CAREFUL here, you may not want a local branch deleted on the remote to be killed! To be safe, do a dry run first!

    git remote prune -n origin

Remove the -n to do it for real.


View a List of Branches

Want to see a list of all branches in your local repo? Do this:

    git branch

Want to see a list of all branches in your local repo and the remote repo? Do this:

    git branch -a


Update Your Local Branch With Remote Changes

It's always a good idea to keep your local branch up to date, and whenever any changes happen to the remote branch, or any branches yours is based on, then you should pull and merge any remote changes. In fact if you are not the only one working on a project, you should always pull before you push your changes.

    git pull origin REMOTEBRANCH

examples:

    git pull origin newfeature
git pull origin 0.8.4


See Local Changes Not Yet Committed

Added files, made changes, want to see what branch you are on as you've forgotten? Check the status of your current branch with:

    git status

This will show anything modified, and also any files that have been deleted and added, plus those which you haven't yet added to the repo.


Compare Your Branch to Any Other Branch

Sometimes you just want to make sure nothing odd has changed between a branch you are on and another. You can do this by showing a diff, and you can do it against local and remote branches. Just make sure you recently fetched!

    git diff BRANCH

examples:


git diff master
git diff origin master
git diff origin newfeature


Add Files to a Branch

Git only tracks files you add to it. You can use wildcards, add whole directories, and so on. But it won't track anything unless you add them.

    git add FILENAME

examples


git add *
git add static/stylesheets/basic.css
git add ../file.js


Remove Files From a Branch

Sometimes you need git to not track a file, because perhaps you added it before including that file in .gitignore. Easily done!

    git rm FILENAME

examples

    git rm *
git rm static/stylesheets/basic.css
git rm ../file.js


Commit Your Changes

Made your changes and ready to committ them to your branch? The easiest way is to commit all current changes on tracked files.

    git commit -a

Whatever your default editor is will be used (EDITOR env variable usually) for your commit message, any line commented with a # is stripped hence why you don't need to delete the list of files committed.


Push Your Changes to Remote

Don't wait, do this often. I tend to do it mostly after I've finished with a branch for the day, but if you work with an active team do it more.

    git push origin BRANCHNAME

examples:

    git push origin master
git push origin newfeature


Undo Changes Put in the Wrong Branch or Stashing Changes

You've started making changes, then you realise you are on the wrong branch and meant to do it in another or a new one. What do you do? Stash is your friend! It saves all current changes since the last commit so you can retrieve them later.

    git stash

Think of it as like reverting to the last commit but with your changes safe. You can then switch to the right branch, and do:

    git stash pop

There are other uses for this, but really if you are using them for anything more than trying one small change then another, you are doing something wrong. But say you wanted to try a small code change and maybe set a few undos along the way, you could stash a few. Then you can use this:

    git stash list

Which shows you all the stashes and then you can pop any of them off (a one time only deal remember!) with:

    git stash pop X

Where X is the number on the list.


Undo the Last Commit

Accidentally committed? Need to undo it? Don't panic, as long as you haven't committed to a remote repo you can do this:

    git reset --soft HEAD^

Now you can edit your code and re-commit.


Undo All Changes Since Last Commit

This is not the same as undoing a commit, this will delete ALL of your changes since the last commit. They'll be totally gone and no way to get them back! That is ALL your work since you last did a "git commit" deleted. Still want the command? Okay...

    git reset --hard HEAD


Undo All Local Changes so Local Matches Remote

Made a total mess? Need to revert your entire local branch so it is the same as the remote? You must be really in trouble to need this, even more than the last one. But here we go:

    git reset --hard origin/BRANCHNAME

examples

    git reset --hard origin/master
git reset --hard origin/newfeature


Remember Kids... Pull Regularly, Push Often

Merges fail and require human intervention the less frequently you do them, so pull often. The more often you push, the easier it is for others to pull your updates regularly. And most of all, the less a disaster your HDD dying is.

Postscript

This is by no means a fully comprehensive guide to using git. It is however pretty much every command I've used regularly and needed occasionally this last year. If you think I've missed something major, let me know in the comments and I'll consider adding it.

Comments

Add Your Own Comment