git

=Git=

Git isn't pretty. It's not even fun to use. It's actually kind of a pain. However, it is extremely powerful and by learning to use it well now you will get a significant leg up on your peers, especially if you start looking to work as a programmer professionally.

You probably care especially about using git to submit your work for this class. If that's what you're looking for, go to submitting work.

Read this:
I have a summary below, but an awesome git resource that I //highly// recommend is [] the page at [] is probably almost as good, if you want a second source. Work through some of those examples!

The primary point of git is to **never lose work**. There are easier and even better ways this could be accomplished, but the secondary purpose of git is to **share code and develop together**.

How you should be working:
Here is the basic idea of git:
 * 1) You create (git init) or clone (git clone) a repository. Update from this repository by pulling (git pull)
 * 2) You edit files.
 * 3) When you have changes you might want to save (and you should save more often than you think), add (git add) the files you want to save.
 * 4) Commit your changes (git commit). You can now always go back to the state you had at this commit.
 * 5) Repeat steps 2-4. **Remember that you need to add each file that changes __every__ commit.**

Even better:
Work in separate branches to isolate code:
 * 1) New assignment? After you pull (git pull), create a new branch (git branch homework17) and switch to it (git checkout homework17). You can combine those last two commands with (git checkout -b homework17)
 * 2) Use the above structure.
 * 3) Do you have a different assignment you want to work on? Switch to that branch instead (git checkout project9), and see the files in your working directory change.
 * 4) Done? After you patch from your current branch (git format-patch master --stdout > username-homework17.patch), you can
 * 5) leave your branch alone (just "git checkout master" and never look back)
 * 6) delete your branch ("git checkout master", "git branch -d homework17"); I don't recommend this
 * 7) merge your branch back into your main tree so that it is more easily available ("git checkout master" to change back; "git merge homework17" to bring homework 17 into your main branch). This should be safe, but you can test for conflits by doing the merge the other way around first (stay in branch homework17 and pull in master: "git merge master").

To undo bad changes:
Of course, in the above workflow, git isn't actually doing anything at all for you. Instead, lets say you break something, and you have no idea how to get back to a working state. Use git help reset to see some examples. git-reset is a very powerful tool, and can save your bacon.
 * 1) Look through your log (git log) to find a commit (nasty long hex number) that you know is good, based on the date or the comment.
 * 2) If you want, keep your current (broken) code around in another branch; create a new branch (git branch broken1)
 * 3) Reset your current branch to your old commit (git reset --hard HEAD~3 #goes back 3 commits in time) or (git reset --hard 7c4af... # go to specific commit)

In case of fire:
The above will not help you if 1) you have a hard drive failure or 2) you delete your entire git tree. To deal with this, you also need to store your git tree in another location. My favorite way is to keep a bare repository on a remotely accessible machine and push your changes to it. Now periodically repeat #5 to keep that repository updated. If you ever need to recover your entire git tree, or you want to work from a different machine, you can always get a copy of that tree with ``"git clone ssh://username@login.cs.nmt.edu/~username/cse221.git"``
 * 1) ssh into login.cs.nmt.edu, create and change to a new directory called cse221.git ("mkdir cse221.git; cd cse221.git").
 * 2) Set the permissions so that only you can read it ("chmod -R 700 ."). This keeps other students from stealing your work.
 * 3) Initialize this directory as a //bare// git repository (git init --bare).
 * 4) Logout, and go to the machine you're developing on.
 * 5) Push your current tree into your new repository ("git push ``ssh://username@login.cs.nmt.edu/~username/cse221.git master"``)

You can even make this easier by setting up a bash alias so that you don't have to type so much. Put "alias backupgit='git push ``ssh://username@login.cs.nmt.edu/~username/cse221.git master``' " in your .bashrc profile, and then you can push to your remote repository just by executing the command "backupgit". Definitely checkout SSH Better to see how to make this less painful.