The Git Trinity

I use Git for personal projects. I got frustrated with the Git UI clients. I would get myself into tree conflicts (or other problems I still don’t fully understand), and the UI couldn’t get me out. Plus I’ve been compelled to learn more about console programming.  So I started using Git Bash — the Linux-like console. And I learned a lot.

When I started, I knew only enough about Git to perform source-control basics: committing/pushing or updating/pulling. My entire git experience can be summed up in these Bash commands:

committing/pushing

  1. git add .
  2. git commit -m”A short note about my code changes”
  3. git push
  4. username: I enter my username here
  5. password: I enter my password here
updating/pulling
  1. git pull
  2. username: I enter my username here
  3. password: I enter my password here

I know these commands by muscle-memory. I type them so fast the console can’t keep up.

  • But I don’t really appreciate what they mean.
  • And since I’ll push a change roughly every 5 minutes, they get repetitive.

Today I made a major effort to learn more Git by re-reading Git’s Git Basics. The foundation of Git are its three states.  (hence the title of this blog; the “Trinity”.) These three states can be visualized in terms of a workflow.

All three diagrams identify the 2nd “middle”-state; the “index”, also known as the “staging area”. The latter two drawings point out something important: I can go directly from 1st state, “working directory” to the 3rd state, “git directory” using a single command.

Since I don’t leverage the middle-state, “the staging area”  (I’m still too inexperienced…), I can consolidate the first two commands (add, commit) into a single command:

  • git commit -a -m”A short note about my code changes”

And Oliver Steele taught me how to make an alias to make this even shorter. By executing this:

  • git config –global –add alias.ci “commit -a”
    • Update; I initially wrote:
      • git config –global –add alias.ci “git commit -a”
      • The “git” part is redundant in an alias; the git command must be used before an alias can be used

This adds a configuration to your User’s Git setup. (Specifically the file described by second bullet here.) That file will now contain a section that looks like this:

[alias]
ci = commit -a

The alias section describes shortcuts for commands. Now I can avoid “commit -a”; I only need to type:

  • git ci -m”A short note about my code changes”

That really doesn’t save much time. A real pain was typing my username and password every time I pushed. It seems like there are three ways around this:

  1. SSH intead of HTTPS
  2. HTTPS with Credential Helper
  3. HTTPS; Changing the Remote for Origin

Edit:

I’ll explore those in my next post! This SO answer does a great job of explaining the latter two options.

I highly recommend Credential Helper.

Now it reduces the 5 separate push commands, to these two:

  1. git ci -m”A short note…”
  2. git push
    1. No credentials needed!

Great! So maybe I’ll use the next post to explore Vim, my Git’s default editor!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: