Getting Started With Git Part I Common Commands

28 Feb 2013

Git is one of those tools that is more appreciated as it is learned and used. It has been noted as being difficult to use and confusing, but really it is just another language that will be grown accustomed to as it is put into practice.

Luckily for those who aren't particularly interested in using console commands (which in opinion is not something to be avoiding), Github has a great GUI client called Github for Mac. The client can obviously not offer all of the functionality that the command line tool can, but it cover most of the uses of its target audience.

Getting my start in design before working my way into development, Github for Mac was fantastic for me to begin learning the importance of version control before I was ready to make good use of Terminal. I learned the hard way more than once that keeping only one version of some code is a bomb just waiting to go off. Keeping consistent and well documented commits can save headaches almost daily, and is also extremely convenient for new developers referencing old code that didn't make it to production.

However if moving past the GUI and making those commands yourself sounds interesting, much more can be accomplished with commands in Git and Github than ever could through the Github for Mac client (as was the intention).

Hopefully this blog post can spawn a little Git series. As I'm just starting to work my way into more complex uses it would be great to have beginner, intermediate, and advanced Git articles on the subject both for my reference and for those hoping to improve as well.

Whether you're transitioning from the GUI or not, the first few commands are simple and will be some of your most used.

config

git config is the first command you will get to know. You may not use it much, but it is necessary to hook up to your Github account and create your own repositories.

You can use git config alone to open up the config menu and will show you the command options. Note that you can set preferences like your username or email for specific file locations as well as actions and types on values. We'll dig deeper into this in a later article.

init

This command is how you get your first repository going. Simple cd into your desired directory and run git init to set up the .git directory that will link your repository to the service.

clone

If someone already has a git repository of the project you are going to work on, but you'd like to work on it on your own machine, you can clone an exact copy by running git clone. Another great feature of Git is that when a repository is cloned, it takes note of its original location so that it can be easily pushed, pulled, or merged after making more changes.

add

Using git add simply adds the changes made in the current directory to the index. This is many times used in conjunction with...

commit

The core of version control command in Git, git commit points a new commit object using the changes in the index placed by git add, then sets the branch. Adding a commit message can be done by using -m followed by your message, and adding -a is shorthand for the git add command. This allows combining the two into one command, sending the working changes to the index, then setting the commit and branch.

Commit without add: git commit -m 'MY COMMIT MESSAGE'

Commit with add: git commit -a -m 'MY COMMIT MESSAGE'

status

git status shows the state of current files like changes to the current directory and added changes that are ready to be committed. Status doesn't move any code, it just shows the status of changes.

In the next post I'll dig into working with repositories with commands like git branch, git push, git pull, and git merge.