Friday, April 29, 2016

Git Branch, Diff, and Merge for Noobs

Prerequisites: terminal basics, git basics (e.g. git add, git commit)

Branching helps you try out new features while leaving a copy of your existing codebase intact. This is nice for working on teams, because it lets each team member work on a different section of code, without them having to sift through the changes the other team members are making at the same time (as displayed by "git diff".)

Diff is for difference. When you tell your terminal " git diff 'master' ", you're telling git to show you the differences between your working directory (the branch you've checked out) and the master branch.

The process goes something like this. In the terminal, from your working directory:

git checkout -b "name_of_new_branch"

[Go to text editor, make changes to your code]

git diff "master"

At this point, the terminal summarizes the differences for you. A few helpful hints:

  • @@ marks one changed section. (In addition to the lines you changed, diff shows you the lines surrounding them, to give you some context.)
  • - marks the lines you removed (or made changes to)
  • + marks the additions or changes you made (on your new branch)

When you're done making changes (you've completed your new feature or whatever,) you switch back to the branch you were on before -- say, "master" :

git checkout "master"

And use "git merge [branch]" to apply your changes:

git merge "name_of_new_branch"

If you're done using the branch you made, you can delete it:

git branch -d "name_of_new_branch"

Rinse and repeat.

Monday, April 18, 2016

Grunt for Noobs

Prerequisite knowledge: Node.js, npm

Grunt is a JavaScript minification tool. It can take a bunch of separate .js files, pack them all into a single .js file, and make that file really small by doing things like removing comments and unnecessary whitespace, and turning long variable names into single characters.

Conveniently, this also serves to obfuscate your code, making it trickier to hack around in or repurpose without permission.

This can be useful when you've reached the stage where you have a bunch of code in lots of different files, and you want to put them all into one file to stick them on your server (this is part of deployment.)

I used the following video to learn Grunt. It gets right to the point with a nice variety of examples.