This is more a handy reference for myself, but it also acts as a very quick intro to why git is awesome and well worth using. The main difference here is I remind you what the state of your working directory is as you make changes (as this was what confused me previously – as I wasn’t quite sure what was going on). I assume you’ve installed git already – and know what it is.
Although we’ll create a demo Rails app, this applies to any app/directory.
Git Basics
Rails new gitdemo
cd git demo
# Create app and cd into the directory
git init
# Starts git repository
git add .
# Adds all files
git status
# Shows status
git commit -m "Initial commit"
# Commits what you've done. -m specifies you want to add a message, which is put in "".
git log
# Shows a log of what you've done
cd git demo
# Create app and cd into the directory
git init
# Starts git repository
git add .
# Adds all files
git status
# Shows status
git commit -m "Initial commit"
# Commits what you've done. -m specifies you want to add a message, which is put in "".
git log
# Shows a log of what you've done
Working with branches/versions
git checkout -b trying-something-out
# Creates a new branch called trying-something-out
# Branches are great because they let you work on forks, and make it easy to undo changes
# For example
rm -rf app/
# That will delete the 'app' directory
## State of your actual directory - the 'app' folder is not there
git status
# Shows you what you've done
git checkout -f
# restores you back to the previous state (keeps you in current branch tho)
## State of your actual directory - the 'app' folder is now back
# Creates a new branch called trying-something-out
# Branches are great because they let you work on forks, and make it easy to undo changes
# For example
rm -rf app/
# That will delete the 'app' directory
## State of your actual directory - the 'app' folder is not there
git status
# Shows you what you've done
git checkout -f
# restores you back to the previous state (keeps you in current branch tho)
## State of your actual directory - the 'app' folder is now back
To get rid of a branch altogether
rm -rf app/
git commit -am "Some changes made"
# commits with flag '-am' which means commit all with a message
## State of your actual directory - the 'app' folder is not there
# Here's how to get it back:
git checkout master
# Switches you back to the master branch
## State of your actual directory - the 'app' folder is back!
git branch
# Shows you which branch you are on
git branch -D trying-something-out
# Deletes branch
git commit -am "Some changes made"
# commits with flag '-am' which means commit all with a message
## State of your actual directory - the 'app' folder is not there
# Here's how to get it back:
git checkout master
# Switches you back to the master branch
## State of your actual directory - the 'app' folder is back!
git branch
# Shows you which branch you are on
git branch -D trying-something-out
# Deletes branch
To apply changes to the master branch
git checkout -b removing-tmp
rm -rf tmp/
# We create a branch and then delete the tmp directory
## State of your actual directory - the 'tmp' folder is not there
git checkout master
# Takes you back to master
## State of your actual directory - the 'tmp' folder is still not there
git merge removing-tmp
# Applies changes to the master branch
## State of your actual directory - the 'tmp' folder is still not there
git branch -d removing-tmp
# Notice the small 'd' - will only delete after a successful merge
rm -rf tmp/
# We create a branch and then delete the tmp directory
## State of your actual directory - the 'tmp' folder is not there
git checkout master
# Takes you back to master
## State of your actual directory - the 'tmp' folder is still not there
git merge removing-tmp
# Applies changes to the master branch
## State of your actual directory - the 'tmp' folder is still not there
git branch -d removing-tmp
# Notice the small 'd' - will only delete after a successful merge
Just note that changes are not saved to your repo until you add them (such as with git add .) and then commit them.
I’ve had all this in my notes for a while – I can’t remember where they came from now but think it was TheRailsTutorial.com 🙂