Git: Moving from GUI to Command Line

Posted on February 28, 2013

Preface

First of all, the ladies love command line. You are guaranteed to pick up twice as many chicks because you are in a completely black application window with monospace font. That’s dark. Like, Sabbath dark. Don’t be hesitant to make the font color green, too. This only adds mystery to your environment and, again, the ladies will be all up ons.

Using Git in the command line really isn’t that hard…

It’s a piece of cake. And you don’t need to abandon the GUI. Sometimes it’s nice to see what’s going on, so don’t feel like a jabroni if you hit up something like Tower or Github for Mac.

Diving in

First of all, setup Git so all your diffs are in color. As suggested by Erik Reagan, this helped me a ton. Just run: git config --global color.ui true

The most frequently used Git commands

$ git pull Pull all repo changes

$ git add This will add specific files and folders into your repository. Pro tip: You can recursively add files when using the add command. For example, if you add a new folder called “hello_world” into your repo, just run $ git add hello_world to add the folder and everything inside of it. You can also recursively add everything by running $ git add . This recursively adds everything in the current working directory.

$ git rm This will remove specific files and folders from your repository. Note: This will not recursively remove files and folders. In order to remove the “hello_world” directory and everything in it, you need to run $ git rm -r hello_world/ The -r states this command should run recursively.

$ git commit -m "My commit message" This is how you’ll commit your changes to the repository. Run this after you’ve added/removed any and all files in your change.

$ git push Pretty self-explanatory. This pushes your changes to the repo.

$ git status Very handy. This will show you all modifications you’ve moved since your last pull. It will show you the added/removed/modified files and folders.

$ git diff [file/folder] Will show you the modifications you’ve made since the last pull. Note: If you don’t specify a folder or file, it will show you the diff of all modified files and folder. Press q in this view to leave the diff.

$ clear After some time, your Terminal is going to get filled up with text. Use this to keep yourself sane. Curtis Blackwell mentioned You can also use ⌘ + k if you prefer keyboard shortcuts.

Typical Workflow

Here’s how a typical workflow is going to work for you.

  1. $ git pull Grab the latest changes
  2. CODE!
  3. $ git status Handy to easily add and remove files/folders in the next step
  4. $ git add [files/folders] / $ git rm [files] $ git rm -r [folder] Stage your documents for commit
  5. $ git commit -m "Add change" Always use active verbs in your commits. Think of commits as “patches” of code. Instead of “Integrated new contact form” use “Integrate new contact form”.
  6. $ git pull Merge changes into your code
  7. $ git push Push changes to repo

Troubleshooting

Something didn’t go right? Here’s a couple things to try.

Merging issues

These seem to happen less when I work in the CLI, but sometimes they happen. Here’s some tips that have worked for me:

  1. Use branches: When you use branches, it keeps your code more modular. You can have branches for things like “photo gallery functionality” or “authenticated contact form”. Typically it is a Bad Idea™ to do your coding in the “Master” branch. This represents the live, fully-working code. Introducing semi-ready commits should never go here. See “Branch Workflow” below on how to setup this up.</li>
  2. Manually resolve merge issues: If you have a conflict in a file, open it up in your text editor and find the merge issues. Clean them up by hand and then save the changes. Once you’ve done this, re-add the file with $ git add filename.ext, run $ git commit -m "Add change", $ git pull for good measure, and $ git push.

Branch Workflow

It’s easy to use branches in CLI.

  1. $ git pull Get the latest changes
  2. $ git checkout -b branch_name This creates a new branch off of the branch you are currently working in and names it “branch_name”.
  3. CODE!
  4. $ git add [files/folders] / $ git rm [files] / $ git rm -r [folder] Stage your documents for commit
  5. $ git commit -m "Add changes" Commit changes to branch
  6. $ git checkout master Moves back to the “Master” branch
  7. $ git pull Pull any changes
  8. $ git merge branch_name Merges your branch from (2) into the “Master” branch
  9. $ git push Pushes changes into “Master” branch in the repository
Google