Doing it Wrong with Git

This post is for people who have used git before, but aren’t really sure what they can do besides push/pull. If you have never used version control, I would definitely recommend git! Here is a simple NetTuts tutorial for getting set up.

Git Phobia

I frequently hear people say they “don’t use git, but should,” or “haven’t had time to learn it yet.” Like me, they are intimidated by git. Though I have been “using” it for about two years, I have always been uneasy around git. Here’s why:

Until recently,

I didn’t know how to do anything but push and pull

I hated merge conflicts

I panicked when I had broken code in my local repo, but needed to pull down someone else’s changes

meanwhile, squeezing my eyes shut and praying for no conflicts (lest the dreaded VIM editor open). I even used to try to work on files no one else was working on, just to avoid a conflict. Crazy, right?

Why Use Git?

I used to think git was nothing more than a “version history”; useful just in case code needed to be rolled back to a working copy (which is just so wrong).

I’m not going to get too crazy here, because many have written on this topic. However, here are my reasons for using git:

Collaborate without accidentally overwriting someone’s changes

Keep track of who wrote what (i.e. git blame)

Use branches to work on features

Support multiple versions of a codebase

Cool deployment options

Version history, so you can roll back if necessary

Some Scenarios

Alright let’s get into some things you can do with git, besides pushing and pulling.

Scenario 1: You Are Adding a Feature Which Will Take Several Days to Finish

The Problem

You don’t want to be committing changes to the master branch as you work, because your partially-finished code will break the main codebase.

Git Solution

This is a great use case for a branch! Making a branch is like making a clone of the current codebase, wherein you can go crazy with changes without affecting your clean master branch. Then when you are happy with your changes, you can merge the temporary branch back into master. Alternatively, if you decide to scrap the idea of the new feature, you can simply delete the branch and forget it ever happened 😉

There are a couple ways to make a branch. This first one is a little more tedious:

git branch myNewBranchName
git checkout myNewBranchName

In general, you will use checkout to switch between branches. You can achieve the same thing with the following one-line command:

git checkout -b myNewBranchName

Now that you are working from this new branch, you can commit changes without fear. When you are done with your new feature, you can use the following commands to resync your code:

Scenario 2: Something Urgent Interrupts You

The Problem

You are happily writing code in your local repo when an urgent or quick-fix bug is sprung on you. You need to stop what you’re doing immediately and handle the problem. You don’t want to lose all your work, but you also don’t want to commit broken code.

Git Solution

Another situation for branches! If you aren’t already working in a non-master branch, create one (I like to call it ‘hold’) and switch to it. Your changes will follow you there, and once you commit from this new branch, those changes will only be associated with that branch.

Once you’ve fixed the bug, commit your changes, merge back into master, and push your changes live, like we did in Scenario 1. Don’t forget to delete the no-longer-necesary branch.

When your master branch is once again clean, switch into your ‘hold’ branch, and continue working. If your commit was especially bad, you can finish what you were doing, commmit, and then rebase the two most recent commits together. (Rebasing is essentially squashing two commits into one).

At this point you will be taken to VIM and asked to delete one of the commit messages. Once you do that, you can switch back into master, and do a git merge hold.

Scenario 3: You are Looking through the Codebase, and Find Some Code You Don’t Understand

The Problem

No matter how many times you read through the code, you just can’t understand what it’s doing.

Git Solution

Do a git blame on that sucker! Then you can go straight to the source and ask the author to explain it to you.

git blame index.php // or whatever your filename is

This will open the file in your terminal and display line-by-line, who last edited that line. Below, you can see that a user named m1ck3y is responsible for the second-to-last line of this file:

Other Things to Know About Git

Git has plenty of other commands and uses (such as deployment), and you can find tutorials and Git Cheat Sheets all over the web. I definitely recommend bookmarking a good cheat sheet or making your own.

In the case of artificial lighting, you can always install recessed lighting,
or track lighting to achieve more illumination in a room during the parts of the year when natural light is at
a minimum. When you want to draw people together and stimulate conversation, this
may be a great choice for your room. Pepper Spray is to
be mounted next to one is bedroom bed where one
sleeps in the event that someone enters into your personal space.