Doing it Wrong with Git

April 10th, 2013 10 Comments

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
  • I had no idea how to quit for the night with unfinished code

Here is everything I used to know about git and git workflow:

git pull
git add .
git commit -m 'some message'
git push origin master

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:

  1. Collaborate without accidentally overwriting someone’s changes
  2. Keep track of who wrote what (i.e. git blame)
  3. Use branches to work on features
  4. Support multiple versions of a codebase
  5. Cool deployment options
  6. 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:
//switch back to master and make sure it is utd with remote origin
git checkout master
git pull
//merge the new branch into master and push to remote origin
git merge myNewBranchName
git push origin master
//delete the no-longer-necessary branch:
git branch -d myNewBranchName

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.

git checkout -b hold
git add .
git commit -m 'Working on x. Commit needs rebasing'

Next, you can switch back to master and do a clean pull. Then create a new branch where you can work peacefully.
git checkout master
git pull
//move to a second branch called quickfix
git checkout -b quickfix

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).

git checkout hold
//do some work
git add .
git commit -m 'Describe your changes'
git rebase -i HEAD~2

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:

git blame

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.

Here are a few resources I like:
Using Git for Deployment
Pretty Git Log
Git Cheat Sheet with Examples
Exhaustive Git Cheat Sheet
Colored Output, Shortcut Commands, Autocompletion, and Bash Prompt
Feel free to share your favorite resources!

10 Responses

  1. Great info, didn’t know anyone went into this kind of detail about GIT. I hope to see some never posts. Keep me updated.

  2. says:

    All right, you are right buddy,on a regular basis updating webpage is really necessary amid support of SEO. Nice argument keeps it up.

  3. What’s up, this weekend is good for me, because this point in time
    i am reading this fantastic educational post here at my home.

  4. Janell says:

    I think the admin of this site is truly working hard in support of his site, because
    here every data is quality based stuff.

  5. What’s up to all, the contents existing at this web site are truly
    remarkable for people experience, well, keep up the good work fellows.

  6. Vanita says:

    If some one wants expert view regarding blogging
    and site-building after that i suggest him/her to pay
    a quick visit this blog, Keep up the good job.

  7. Brigitte says:

    On parle de gonflement abdominal, trouble de la digestion, perturbation de l’humeur et des comportements,
    migraine, etc.

  8. En seguida coges un peine con dientes anchos y procedes a peinar tu pelo
    de forma suave, pero siempre y en toda circunstancia trate de
    que su cabello esté húmedo.

  9. 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.

  10. Mona says:

    Quality articles or reviews is the secret to attract the users to go to see
    the web site, that’s what this site is providing.

Leave a Reply