Notes from Code School's Git Real Course - Level 2
Git Diff
$ git diff
Use to show unstaged differences since last commit. But once you stage a file ($ git add <filename>), it will no longer show up.
$ git diff --staged
If your files are already staged, use this to see the differences since the last commit.
Unstaging Files
$ git reset HEAD <filename>
This will remove the specified file from the staging area. Note: no quotes around filename
e.g.s
$ git reset HEAD LICENSE
$ git reset HEAD index.html
$ git reset HEAD styles.css
Discard Changes
$ git checkout -- <filename>
This will reset the file to the state it was in at the time of the last commit. You are essentially pulling down the last version of the file and overwriting the current changed version.
Stage and Commit in 1 Step
$ git commit -a -m "modify README.txt"
This will allow you to do the staging and commit in one fell swoop. But, if you have any untracked files, this action will not stage or commit them.
Undoing a Commit
$ git reset --soft HEAD^
Moves your last committed items back to the staging area. HEAD refers to the last commit you made.
![]() |
Git Timeline |
Adding to a Commit
Amending a commit is a two step process, first add file to the staging area, then commit with a new message.
$ git add <filename>
$ git commit --amend -m "New message that will overwrite original message"
- The new commit message overwrites the old commit message
- --amend adds to the last commit
- whatever has been staged will be amended into the last commit
Undoing All Changes Made By a Commit
You can rollback changes by using the $ git reset command. This will wipe out the committed changes completely. They will not land back in the staging area like a soft reset.
$ git reset --hard HEAD^
Undo all changes since the last commit
$ git reset --hard HEAD^^
Undo all changes made in the last 2 commits.
How to Share Files
Remote Repository Hosting
Git itself doesn't handle authorization and access issues (who can see/share your files). Options for remote repo hosting include a hosted solution or a self-managed solution.
Hosted solution example: GitHub, BitBucket
Self-Managed solution example: Gitosis or Gitorious
How to Store Your Files on GitHub
- Go to https://github.com/
- Create an account
- Create a new remote repository (using the GUI)
- GitHub will give you a remote URL to push your project to, as well as the exact text to use in your command line to do so
The commands will look something like this:
$ git remote add origin <insert the url github provided here between "">
$ git push -u origin master
e.g.
$ git remote add origin "https://github.com/helengutz/sample_app.git"
$ git push -u origin master
- origin = name of the remote repository
- master = name of local repository
- url = address of the remote repository
- by using -u, you are telling the system to remember that address, so you can just do git push next time
The $ git remote add origin "https://github.com/helengutz/sample_app.git" part creates a bookmark of the remote repository
To view all your remote repositories, run:
$ git remote -v
The $ git push -u origin master part actually pushes the files to the remote repository
Pushing To Remote
$ git push -u origin master
Use the above the first time you commit to a new remote repository, after that you can just do git push
master(local) --> origin(remote)
You will also be asked for your GitHub username and password to ensure you have authorization. You can go to https://help.github.com/articles/set-up-git to learn about password caching so you don't have to enter it every time.
Pulling From Remote
$ git pull
Use this to pull down changes from the remote repository. Its important to do this often if multiple people are working on the same project.
master(local) <-- origin(remote)
Having Multiple Remotes
Its not uncommon to have multiple remote repositories. One for testing for example, and another for production.
![]() |
working with multiple remote repositories workflow |
To Add a New Remote Repository
$ git remote add <name> <address>
e.g.
$ git remote add pb git://github.com/paulboone/ticgit.git
- example sourced from: http://git-scm.com/book/ch2-5.html
To Remove Remotes
$ git remote rm <name>
e,g.
$ git remote rm pb
- This removes the repository added in the above example
To Push Remotes
$ git push -u <name> <branch>
e.g.
$ git push -u origin master
$ git push -u pb master
- the branch is usually master
- remote repositories are usually called "origin", but can be named whatever you want, like "pb"
- by using -u, the next time you push you don't have to specify name and branch
Heroku Remote
Heroku is a hosting platform. To use it you need a Heroku account, and the Heroku gem installed.
Once you have these, you can run:
$ heroku create
This gives you a git repository ssh address, and creates a remote repository called "heroku"
$ git push heroku master
Use to push files to heroku and deploy your app
About this blog: This blog is an attempt to summarize my learnings as I learn them. Expect inaccuracies, incompleteness and inconsistencies.