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:
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.
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.
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 ,
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
Post a Comment