Friday, March 7, 2014

Git Command Line Basics

Here is a sample workflow, not meant as a complete tutorial, but rather a clean, solid workflow that should get you up and working with Git from the command line in no time.

Setting It Up

If you haven't already, go to Github.com and create a repository for your project (e.g., 'my_project').

Navigate to your project folder, and make sure you know where you are first!:
Tip: replace 'my_projects' and 'my_project' with your own details
Tip: you don't need to type the '$'
Tip: '#' indicates lines generated by your terminal, not typed by you
$ cd ~/my_projects/my_project
$ pwd
# /Users/you/my_projects/my_project

Set your git credentials if you haven't already, then initialize an empty repository for your project:
$ git config --global user.name "Your Name"
$ git config --global user.email your.email@example.com
$ git init

# Initialized empty Git repository in /Users/you/my_projects/my_project/.git/

Add your github.com origin, and if you want, see your origin settings with either of the following two commands:
Tip: replace 'username' and 'repository' with your own details
$ git remote add origin https://github.com//.git
$ git remote show origin
* remote origin   
  Fetch URL: git@github.com:you/my_project.git  
  Push  URL: git@github.com:you/my_project.git  
  HEAD branch: master
  Remote branches
$ git remote -v
# origin https://github.com/you/my_project.git (fetch)
# origin https://github.com/you/my_project.git (push)

If your files are on your desktop, push them up as your master branch:
$ git push -u origin master

Or, if you want to pull down the current version of a remote repository:
$ git pull

If, when trying to pull down the remote repository, you get a message that you have uncommitted changes locally (preventing you from 'fast-forwarding'), you can either dump the changes on your local machine:


$ git reset --hard
$ git pull
or, you can 'stash' those changes for the time being, pull down the remote repository, then re-'apply' those changes:


$ git stash
$ git checkout -b newStuff
$ git stash apply
$ git commit ...

Working

Create a new branch.  At any point you can also check to see what branch you are on (indicated by an '*'), and check your status:
$ git checkout -b my-working-branch 
Switched to a new branch 'my-working-branch ' 
$ git branch 
master 
my-working-branch 
$ git status 
# On branch my-working-branch 
nothing to commit, working directory clean

When you're ready, make sure git has all your new files and changes, then commit your changes to your working branch (with a special message):
$ git add .
$ git commit -m "this is pretty cool"

Switch back to the master branch, merge the working branch into the master branch, then delete the working branch:
$ git checkout master
$ git merge my-working-branch 
$ git branch -d my-working-branch
Tip: if you messed up your working branch and want to abandon it, skip the 'merge' step and use a capital "-D" when deleting the working-branch. 

Push your changes up to Github.com, and you're done! 
$ git push


_____ Update: if you're having problems with Gemfile.lock, try:
git reset HEAD -- Gemfile.lock
git checkout Gemfile.lock