I work as an application developer. Most of my career has been spent in large corporate environments that have configuration management teams, systems administrators, dbas, etc in addition to software engineers. As such I am a version control user, and not an administrator or power user.
During my daily routine I commit to a remote repo, and I update my existing repo. I regularly compare my local changes with existing ones, and I regularly look at commit history.
I rarely branch code, I rarely check out branches or commit to branches other than the trunk.
I’ve done this with CVS (which still remains my preferred vcs), Visual Source Safe, SVN, and StarTeam (which happens to be my least favorite vcs).
When I first learned about GIT a couple of years ago, I was interested to hear that it was somehow different than these other systems.
I’ve been using GIT very irregularly for the last couple of years, I have a small github account. But I haven’t used it enough to commit the standard commands (listed above) to memory. So this then is my meager attempt at doing so:
*** This assumes you’ve installed git and have configured the global settings.
-
Creating a repo:
mkdir project (create some code in the directory) cd project git init
But wait there’s more…
This is deceptively easy. In fact you’re more likely to want to create a local and remote repository, a la github style for example.
In that case, you’re going to want to do this:- Create a remote repo
- Generate your ssh-keys
- Git init, like above
-
git add file_to_commit git commit -m 'first commit of all files added' git remote add origin git@github.com:user/git-repo.git
- Check out an existing repo:
This is pretty straightforward, but assumes you’ve set up your ssh-keys.git clone [url]
- Commit local changes:
Once you start working, a benefit of having a local repository is that you can commit a lot, without having to worry about breaking a continuous integration environment in the process.
This is pretty much the same as with other vcs.
First add:git add somefile.txt
Then commit:
commit -m 'my message'
Caveat:
- It’s probably useful to add a couple of files at once, rather than one at a time:
git add one.txt two.txt
or
git add 'documentation/*.screen'
will recursively add all new files ending with .screen from the documentation directory
-
git status -s
will tell you what has been added, and what has been changed since the add. (-s is for short format)
- Commit changes to remote repo
git push origin master
- It’s probably useful to add a couple of files at once, rather than one at a time:
-
Remove stuff:
git rm file
will remove the file from the staging area entirely and also off your disk.
git rm -r dir
recursive delete
-
Check what’s been committed:
This is different than status because I want to know what’s different between my remote repo and my local one.git diff --stat master...origin
Git will automatically figure out what the common commit of the two commit is and do the diff off of that.
Hat tips:
Git Reference – my favorite site I’ve found explaining git. It’s practical, provides examples, and the authors don’t go out of their way to make simple concepts seem complex. I’ve paraphrased a few of their examples here.