Skip to main content

A cosmo style guide to git

Imagine a Venn diagram with three sets - developers, git users, and Cosmo readers. I am one of those at the tiny intersection of all three. There may be others out there but I don't personally know any.

Despite the provocative title, this post has little to do with Cosmo. Cosmo is like my perfect escape route when I want to step away from real life for a bit. Thumbing through articles like '10 ways to style your hair'! '27 must have Shoes for fall!' and more of the same thing said 30 different ways is very entertaining to me. Not that I actually follow anything in there.

Cosmo does have a few things going for them -  they know how to pick a provocative title, and their sentences are short enough to convey their message without being overly verbose and preachy. You can get what they are trying to convey using 10% of your brain.

As an enthusiastic Git user, I decided to write about how it changed my life. I have never felt this inspired about any other tool to actually write a post in this very infrequently updated blog. I want to do it Cosmo style because - there is already this and other excellent git tutorials out there, and my goal is to keep it simple. What I have to say will be short, (hopefully sweet) and convey just how awesome this tool is.  I am not getting into anything overly advanced here, the point is to showcase commands with typical dev workflow scenarios that they apply to. So, here it goes:

10 must have git commands for all seasons!

1) git config --global - Your ~/.gitconfig file is like foundation. It is the base upon which the rest of your git workflow experience will rest. Use this command to edit it (or you could just use a plain old text editor). There are tons of examples online, some basics to do are 
    - set up your excludes file and add the file extensions that you want ignored by git, 
    - add aliases for commonly used commands, programmers are lazy and we hate to type. 
    - color options for diff/status/branch - see http://jblevins.org/log/git-colors for an example.

2) head^ -  Learn how to refer to git objects, like head^ is the parent of current branch, and head^3 is the great grand parent, use this with commands like  git diff and git reset.

3) git commit --amend - This is very valuable when you want to change your current commit, could be something as simple as fixing a typo in the message to adding new changes to it. Remember don't do this if the commit has already been pushed upstream! 

4) git stash - You're in the middle of a complicated local change. Boss emails you about a production issue that needs to be fixed like right now in the same code module. No worries:
 git stash ,
make your fix..
git commit
git stash pop

You're back in business, baby!
5) git branch - Make branch for any change that's non trivial. Trivial meaning touching less than 10 lines of code and one source file. Why? branches are cheap, plus they really start to help when you have a couple of different ideas on how to do something. Implement each idea in a different branch, after that it is easy to compare them head to head on things like time/space complexity /readability.

6) git cherry pick  - Did you know Maraschino cherries make your skin look younger by 10 years? Well not really, and that's not even the point. How awesome is cherry pick? You have changes a, b  in branch B1 and in branch B2 you started going on a different path with changes a` and then decided you needed change b in branch B2 as well. No need to redo anything, just git cherry pick b into B1 and you are all set!

7) git add -p  - You've made two unrelated changes to the same file(s). This lets you add only the parts you want to commit together to the index. Follow it up with a git commit, and then start over again with git add -p to make your second commit. Think how easy this makes it for your co-workers reading your commits. You'll never make an annoying commit like - "Implemented feature X, and oh while I was in there also fixed bug Y" again!.

8)   git rebase -i  - One could dedicate an entire post about interactive rebase, but this is the TL;DR version. You are the film editor and you can cut,rearrange  and merge different sequences in your code movie. Why is this important? You won't be afraid to git commit  anymore. If you feel like two of your commits could be merged into one, you could do that. Change their order ? No problemo.  Undo one of them because you'd rather split them? Con mucho gusto!

9) git reset  - This one's definitely something that comes with a 'use with caution' warning. It is a very useful way to reset state when you feel that your commit or changes are not quite where you'd like them to be. Definitely read the docs here and understand the differences between --soft, --hard, --mixed, as well as the -p option with which git reset -p when used correctly  becomes the opposite of git add -p.

10) gitweb  - gitweb comes with the core git distribution and lets you browse a repository over the web. Once you have this configured its so easy to get feedback from other people in your team on your work, even before you commit. Yes, there are tools out there that support pre commit code reviews, but require jumping over hoops. With gitweb, all they need is a web browser and a chat client - you can then collaborate easily with anyone in your team as you are working on something, rather than finishing everything and then asking for feedback.






Comments

Popular posts from this blog

What are your future plans? Why are you *still* a developer?

If I had a nickel for every time someone has asked me that question I'd have enough change for a year? Inspired by my friend's post here , I thought I'd write about how I ended up doing what I do now. Then I thought about it some more and decided to write about something else. Is it important to know where you are going in life? If you aren't moving forward in your career does it mean that you are doing something wrong? What does "moving up the ladder" even mean? I am going to attempt to answer these questions for myself. Its almost 5 years since I began my professional career. It has been great so far, lots of ups some downs as well. However, once in a while when I get the title question it still throws me off. It is usually my parents or well meaning relatives, sometimes friends that ask this. I have nothing much to say to them except "I enjoy what I am doing right now, haven't really thought about the future". But the truth is - I have thou

Craigslist, wrong calls and me

I have the worst luck when it comes to getting wrong calls on my phone. Why do I say that? It started a few weeks ago with a phone call - "I'm calling to ask about the Nissan altima you have on sale". I replied saying wrong number yada yada etc. After the third call like this, I finally asked them where they found the ad and it turned out to be Craigslist. One google search later I found the ad listing my number, probably a typo. Contacted them via Cragislist and asked them to correct it. End of story, right? Two days later, I got ANOTHER call, here is how it went. Caller: Maam, Im calling about the toyota four runner for sale Me: You mean the Altima, that was a mistake in the ad. I am not the seller and it has been corrected in the ad online. Caller: Is your number 602-*******? That's what's on there.. Me:ARGGH Another google search, and I find a DIFFERENT Craigslist ad in another city, listing my number. What are the chances of this happening to the same perso

Don't teach programming by asking students to program - What?

Saw this article in one of the ACM blogs that cited a recent study in an educational psychology journal on how teaching introductory Cs students to program by having them write code actually has a detrimental effect. More details here . I absolutely disagree with the author that this study has implications in CS! First of all it cites a study done with teaching algebra so it is a different domain. I think that the problem is that there is too little programming going on rather than too much. I also wonder of anyone has considered that an introductory programming class probably has a mix of two very different types of students - one just exploring CS as one of many career options, the other the kind that took their dad's computer apart in the garage and started programming at age 8. The latter kind still have to take the introductory class because of degree requirements but now it gives you a class sample that's skewed and hard to make any conclusions about. If everyone that to