tryGit - Code School (Free Course) notes
Foreword:
Iâll resume taking notes on Ruby/Rails when Iâm able to get my Konsole IDE to work as I would like it to, until then, Iâm going to sharpen my GitHub skills. GitHub, is a Version Control System, it allows users to seamlessly edit, manage, and change (most often code) files that have been uploaded to directories. The notes Iâll be taking will be from Code School.
Another great place to find documentation for git is:
 https://git-scm.com/documentation (Iâll have excerpts from this site in quotations as well to try and help explain the git commands Iâm using in tryGit.)
tryGit:
The class begins having you initialize a directory for git from the current working directory. If youâre from console (at home directory toast@localhost:~$ for example), you begin to first move to the directory in question, say a folder called âCode Schoolâ. We can check to see if we have this directory made by first using ls (lists contents of directory). If there isnât a directory called âCode Schoolâ listed, we can create it using mkdir (make directory). Careful though, if you type mkdir Code School youâll find by using ls, that you now have two directories, one named Code, and another named School. You can fix this by using rmdir command. To create a directory called âCode Schoolâ, weâd use mkdir âCode Schoolâ. To move into this directory, weâd use cd (Change directory). The command would look like cd âCode Schoolâ/ (or cd âCode Schoolâ/). We now find ourselves in the ~/Code School/ directory. I use the tilda to reference the login directory because, if we wanted to return to the directory we were just in (the login directory), we can use cd ~ and weâll move back, alternatively we can go all the way to the system root directory with cd /. Lets return to the lesson at hand though:
Now that weâre in a working directory that we want to connect to git, we can initialize a git repository by using git init
> git init
Initialized empty Git repository in /.git/
Success! (This will not be shown when using console, Iâll be putting this text in bold to indicate that in future uses)
The advice that Code School provides in this section is:
Directory: A folder used for storing multiple files.
Repository: A directory where Git has been initialized to start version controlling your files.
Next weâre told to use the git status command, which âshowâs the working tree status.â
$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
Success!
The advice that Code School provides in this section is:
The .git directoryÂ
On the left you'll notice a .git directory. It's usually hidden but we're showing it to you for convenience.
If you click it you'll notice it has all sorts of directories and files inside it. You'll rarely ever need to do anything inside here but it's the guts of Git, where all the magic happens.
The advice section here describes how if we used git init in our Code School directory we made earlier, and now used the ls -a command, weâd see a new .git directory in our folder. (the -a just means to list all files, including those that have . before their name.
In the following section theyâll have created a simple .txt file called octocat.txt and theyâll ask us to use git status again.
> git status
# On branch master
#
# Initial commit
#
# Untracked files:
# Â (use "git add <file>..." to include in what will be committed)
#
# octocat.txt
nothing added to commit but untracked files present (use "git add" to track)
Success!
They tell you afterwards:Â âNotice how Git says octocat.txt is "untracked"? That means Git sees that octocat.txt is a new file.To tell Git to start tracking changes made to octocat.txt, we first need to add it to the staging area by using git add.â
With their advice being:
staged: Files are ready to be committed.
unstaged: Files with changes that have not been prepared to be committed.
untracked: Files aren't tracked by Git yet. This usually indicates a newly created file.
deleted: File has been deleted and is waiting to be removed from Git.
The next thing weâll do is change octocat.txt from being untracked, to staged, or as seen above files that are ready to be committed. This also means weâll be changing the file to be tracked. Weâll do this by the following command:
$ git add octocat.txt
Nice job, you've added octocat.txt to the Staging Area
Theyâre advice for using the git add command and helpful extras are:
add all: You can also type git add -A . where the dot stands for the current directory, so everything in and beneath it is added. The -A ensures even file deletions are included.
git reset: You can use git reset <filename> to remove a file or files from the staging area.
Code School is going to have us use, git status again to show the changes that are being made to the file.
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# Â (use "git rm --cached <file>..." to unstage)
#
# new file: Â octocat.txt
#
Success!
Verbatim from Code School: âNotice how Git says changes to be committed? The files listed here are in the Staging Area, and they are not in our repository yet. We could add or remove files from the stage before we store them in the repository.To store our staged changes we run the commit command with a message describing what we've changed.â Youâll notice as well, that if we staged something using git add -A (the -A meaning add âALLâ), but only wanted to stage 9 of 10 things, we could use the git rm --cached <file> to unstage something before we commit.
Their helpful hints this section being:
Staging Area: A place where we can group files together before we "commit" them to Git.
Commit: A "commit" is a snapshot of our repository. This way if we ever need to look back at the changes we've made (or if someone else does), we will see a nice timeline of all changes.
Lets commit our file now:
$ git commit -m "Add cute octocat story"
[master (root-commit) 20b5ccd] Add cute octocat story
1 file changed, 1 insertion(+)
create mode 100644 octocat.txt
Success!
Verbatim again:Â âGreat! You also can use wildcards if you want to add many files of the same type. Notice that I've added a bunch of .txt files into your directory below.I put some in a directory named "octofamily" and some others ended up in the root of our "octobox" directory. Luckily, we can add all the new files using a wildcard with git add. Don't forget the quotes! [here theyâre asking us to use git add â*.txtâ]â
With a bit of a warning:
Wildcards: We need quotes so that Git will receive the wildcard before our shell can interfere with it. Without quotes our shell will only execute the wildcard search within the current directory. Git will receive the list of files the shell found instead of the wildcard and it will not be able to add the files inside of the octofamily directory.Â
So lets do what theyâre asking us and move along:
$ git add '*.txt'
Success!
Their word of advice:
When using wildcards you want to be extra careful when doing commits. Make sure to check what files and folders are staged by using git status before you do the actual commit. This way you can be sure you're committing only the things you want.
So letâs go ahead and be extra careful:
$ git status
# On branch master
# Changes to be committed:
# Â (use "git reset HEAD <file>..." to unstage)
#
# new file: Â blue_octocat.txt
# new file: Â octofamily/baby_octocat.txt
# new file: Â octofamily/momma_octocat.txt
# new file: Â red_octocat.txt
#
We can now see that by using our wildcard, not only did we at the two new files in the directory we were working in, but we also added both files that were in the newly created octofamily/ directory.Â
Everything looks good, so lets run git commit -m âAdd all the octocat family txt filesâ. (I didnât mention this the first time we used git commit, but the â-mâ the follows after our commit is for adding a message to go along with our commit. So when we view it on github, and subsequent changes, we can see the message attached to the commit. Often, this is what was changed, or added/removed from the repository with the commit)
$ git commit -m 'Add all the octocat txt files'
[master 3852b4d] Add all the octocat txt files
4 files changed, 4 insertions(+)
create mode 100644 blue_octocat.txt
create mode 100644 octofamily/baby_octocat.txt
create mode 100644 octofamily/momma_octocat.txt
create mode 100644 red_octocat.txt
Success!
To continue the verbatim notes:Â âSo we've made a few commits. Now let's browse them to see what we changed.Fortunately for us, there's git log. Think of Git's log as a journal that remembers all the changes we've committed so far, in the order we committed them.â
More useful logs:
Use git log --summary to see more information for each commit. You can see where new files were added for the first time or where files were deleted. It's a good overview of what's going on in the project.
$ git log
commit 3852b4db1634463d0bb4d267edb7b3f9cd02ace1
Author: Try Git <[email protected]>
Date: Â Sat Oct 10 08:30:00 2020 -0500
  Add all the octocat txt files
commit b652edfd888cd3d5e7fcb857d0dabc5a0fcb5e28
Author: Try Git <[email protected]>
Date: Â Sat Oct 10 08:30:00 2020 -0500
  Added cute octocat story
Success!
Normally when you use this, youâd see the date being when you created each commit, youâll also see the message that you attached to the commits. These commits though, weâre they going to? tryGit will cover this topic next using:
git remote: Git doesn't care what you name your remotes, but it's typical to name your main one origin.
It's also a good idea for your main repository to be on a remote server like GitHub in case your machine is lost
âTo push our local repo to the GitHub server we'll need to add a remote repository.This command takes a remote name and a repository URL, which in your case is https://github.com/try-git/try_git.git.Go ahead and run git remote add with the options belowâ
In the tutorial they âcreated a new empty GitHub repository for you to use with Try Git at https://github.com/try-git/try_git.git.â The first section of these notes dealt with creating your own directory.











