git


In the past few weeks I’ve been doing alot of Git and alot more collaboration with people on projects! So i wanted to point out some of the things i’ve learned and some handy tips I picked up.

I was aimee remotely and she showed me how to “stage” a commit. Which is a way to indicate “HEY this is going to be commited when you do git commit!” I used to always do commit -a which adds all the files not currently in the staging. I used perforce for awhile and learned how nice it was to have a “change list” and how you can commit only certain files at a time. Git seems like you can only have one “change list” at a time but that is fine in most cases.

git add -p

It will go through each changed file and ask if you want to stage this hunk. We were able to see what files we had changed as it showed a diff. You can confirm or deny chunk by chunk. today I like to run it to make sure I didn’t accidentally change a file. I have a habit of leaving a file open, walking away and coming back and adjusting the spacing. Probably not something I need to in a commit.

git status

This is really cool command, not only does it show you what is in staging it tells you what to do with the other files to either add/remove etc to get the commit in the shape you want. After working with git more, i went back and re-watched the peepcode screencast on git. I understand it better now!

After we committed, we realized we made a mistake in log message, so we did

git commit –amend

It will amend the last message in your git repo. I recently tried this with one of my commits, but I had already pushed to origin. I was kind of confused, so I asked aimee, she said

no If you have already pushed, you can git push –force to update, but be careful because it destroys references, so if there’s a chance someone else may have pulled the commit in the meantime you won’t want to go pulling the rug from under their feet, y’know! ;)

Ahh, makes sense aimee!!

Another sticky point for me was — what is inverse of git add? git rm?I keep a git repo of code I am working on as I to learn stuff, practice etc. Most if it is probably not really worth browsing, but i like to point my friends there when I talk about what I am doing. I wanted to clean up some stuff that I don’t want to keep and mistakenly thought if I git rm it would remove it from the staging. Nope, it removes it! Well, to revert a file back to what you use this:

git checkout — file.txt

more on this Why is “git rm” not the inverse of “git add”?

Anyways, a few tidbits of information I’ve learned recently, hope this can help you git better at git :)

sources I found helpful

I’m watching the Git peepcode by Geoffrey Grosenbach and as usual with other peepcode it is very good. This is the ultimate in geek-tv. I swear, i will burn these to a DVD sometime so i can watch them on my tv! :)

Things I like about git so far:

- branching, since I am often on the road without internet access.
- clean, just creates one .git dir in your project root. I just found a bash command that will clean out my .svn dirs from a project I’ve modified so much on the road that I’ve borked up the svn process and I’m going to convert it to git

BTW here’s that bash command if you are interested

from: http://www.anyexample.com/linux_bsd/bash/recursively_delete__svn_directories.xml

its simply:

rm -rf `find . -type d -name .svn`

Haha, I remember I wrote a perl script I think to do this like 4 years ago for someone. Who knew it was just a rather short bash command…apparently not me.

Branching is freaking awesome. But what is even more interesting is the stash. Geoffery said it was like a clipboard. You can be working away and you have some changes you are in the middle of but you don’t want to commit yet, so you stash them, then go do something else, then later get that stash and apply to your code. Interesting.

There is a progam gitk that will give you a GUI look at your branches and commits. Thats cool.

get-svn lets you use git locally and then check back into svn.

BTW I keep typing get instead of git! … grrr.. I am about ready to make an alias! :)

I like! .. and the Peepcode screencast is great, as usual. I will need to watch the branching again but I highly recommend getting this screencast if you are interested in learning git. Its only 9 bucks!