basic git

Sometimes I get asked, “hey I want to learn git, what should I do?”. My answer is usually somewhere between read the Pragmatic book, watch the Peepcode, or “hey, there are only a few commands you’ll use 80% of the time, I should write them down.”

So if you just need some basic git, just to get by, this post is for you. Note that this just glosses over the basic commands, you’ll want to use the man pages and resources on the web such as the free book Pro Git to learn more.

git clone

This makes a copy of the passed repository and puts it on your machine. Note that git clone will create the directory called ‘ext-extensions’ for you. This is the first step to working with an established repository.

$ git clone

git stat

git diff

git stat tell you the current status - what files have changed. And git diff will show you a line by line diff of every file that has changed. Pipe it’s output to your favorite editor, or to a pager like less.

git commit

$ git commit -m "added this new feature" ext-extensions.js

You have just added your change into your copy of the repository. Pass a meaningful message. You need to specify the file(s) to commit, or -a to commit everything that is modified.

git push

Use git push to send all your commits to the origin server. If you don’t want to send them all, then commit some on a different branch from ‘master’. (this is not hard, but a bit more advanced. Look up git branch and git checkout).

$ git push origin master

That’s the remote repo name (origin) and the branch name (master). These are defaults; you can push/pull to/from more than one remote repo.

git pull

Use git pull to integrate changes from another git repo into yours. Similar format as git push.

$ git pull origin master

Armed and dangerous

Armed with the above commands, and a git expert nearby, I guarantee you’ll be able to survive the day only bothering them once or twice. Here are more tips to keep you going:

git add

You can batch your commits to git. Use git add to queue up a bunch of files (or directories) and then commit them all at once using git commit. Very helpful. With this usage, do not specify files on the command line. If you use git add, you also need to use…

git diff –cached

the –cached param to diff tells it to diff against the version pending commit, not the version on disk. This can get you in trouble, so before you commit ‘added’ files, make sure you have not changed them since the add (of course, this is a feature as well, as you can keep modifying a file but then only commit the original changes. It can get confusing, so just don’t do it.).

git checkout – path/to/file.txt

You’ve made some changes to a file, maybe lot’s of changes, and it’s all crap. You just want to pull the latest version from the repo and forget you even edited it. This works alot like svn revert.

These are the commands I use every day. There are some more: git log, git merge, git mv, git rm, git reset, git show, git blame…but I’ll save these for part 2. The basics are enough to allow a user without too much scm experience to survive changing code and committing files. There’s plenty of info available on the web for any git situation you may find yourself in, don’t be afraid, jump on in!