Git tries its darndest to be as hard as possible to use.Â More than anything, it suffers from too-many-options-itis and correspondingly confusing options, often a warning flag for a programming language or tool.Â But git’s core is good, and it has a lot of features that are quite endearing.Â I’m not an expert, but I’ll share what I know.
Git is a distribute version control system.Â The core concept of DVCSs is that there is no central repository, and that every user has a copy of the entire repository locally.Â The second part is a simple truth for all DVCSs.Â The first part is kind of a lie.Â Yes, you can operate with a bunch of independent nodes, and you’ll get nothing done.
Yes, in the end, you’ll have a central repository, or quite possibly multiple central repositories.Â But you’ll have something central.Â This is where you will deploy from, how you coordinate with teammates, and generally work, just like you did with CVS and SVN.Â So don’t let that freak you out.
Powered by the power of the concept of diff
Git is driven entirely by diffs — sets of relative changes (differences) to files, including new and deleted files.Â It is trivial to have git create output very similar to the output produced by Unix’s diff command.
This is a powerful metaphor, though.Â You’re now freed from worrying about file dates as a dirty marker.Â That is, you can make a change, save it, come back a week later, undo it, save it, and suddenly you don’t have a change to commit any longer.Â You don’t even have to do any sort of “revert to repository version” action.
These relative changes can be juggled and reorganized in a variety of ways.Â You can
- roll back to a certain change — git just undoes the diffs applied.
- push or pull changes to and from other repositories
- collect a bunch of commits and merge them into one commit — git calls that squashing.
- move a bunch of commits between branches — merging or sometimes rebasing (basing your commits on something else — get it?)
- you can stash a bunch of changes to replay later somewhere else — even if they’ve never been committed anywhere.
Day by day
Git is a bit like an onion, it has layers — lots of them.Â Specifically, I’m talking about when and where files and changes are tracked.Â You’ll come across these divisions fairly frequently, so it is helpful to be familiar with them.
- Untracked files – these are changes to a branch on the local filesystem.Â Git is aware of them, but will take no action unless explicitly acted upon.Â Note that you can change branches out from underneath these files, and they will remain as local changes — unless it conflicts with a file changed on the branch.
- Staged files – these are changes ready to commit.Â Think of these as a changeset, an atomic diff to apply to the branch.Â You can build this up over time, gathering all the changes before the commit. Â They will all share the same commit message, as well. These files are in your Git Index, a term you may come across from time to time.
- Changes to your local branch – these are committed, finished changes.Â They are stored locally, and can even be “uncommitted” if need be.Â But these changes are not shared with anyone.Â Since no one else knows about these changes, they are still subject to merges and merge conflicts.
- Changes in the shared repository – these are changes made to a branch remotely.Â This is where your changes go to live and be safe, just like CVS, SVN.Â While you’re doing work at all the other layers, this layer can and likely will change.Â I do recommend you sync with this repo as often as practical, and before you do any commits if you can.
The Git Community Book is a great resource for more on git and how to use it.Â In fact, it almost goes into too much detail on everything, just like Git does.Â But it is a great cookbook — search for anything git and you’ll likely have a hit there in the top 10 results.
In a future article, I’ll describe how to use git in practical, no-nonsense terms.