GitHelp...

For those who are seeking help with Git & Github

(Still a work in progress...)

Git-ting well... Git

Downloading Git

What even is Git?

Git is a Version Control System (VCS) that keeps track of older versions of files, and gives us the ability to roll back and maintain different versions of the same files at the same time.

Git is vital for developers today, because of one simple word, collaboration! Git allows many developers to clone a repository or "repo" (collection of files) from a main codebase down to their machines, work separately on portions of code, and then commit their changes back to the main codebase where everybody can update the new changes.

Then What is Github?

Github is a platform/cloud-based service that allows developers to store and manage their project's code, in addition to keeping track to changes in the codebase, merging together different versions/branches of code and much more!

Also worth noting that are other service out there like Github, such as BitBucket or GitLab, but they essentially work like Github

Git Workflow

Essentially there are three "trees" maintained by git. The first tree is called the "Working Directory". This tree contains the acutal files. Next, is the "Index" tree, which acts as a staging area before pushing files up. The last tree is the "HEAD" which simply a reference to the most recent commit made in your current branch, most of the time...

Changing Directories

We've learned a bit about Git, but lets get started.

Note: the commands below, "cd, mkdir, and ls" are native commands of the terminal in general. Due to the way Git integrates with the terminal, it inheirits these commands to make use of them for Git and more.

Changing Directories

In order to change to different directories in our machine, the command we use is:

cd "directory name"

or

cd desktop/projects/GitHelp

Now what if we need to go up only one directory in our folder structure? We will use:

cd ..

Listing

Sometimes when in a directory, you may want to see what files live within that directory. The command we use is:

ls

In turn this will put out a response similar to this:

> css images index.html js

Making Directories and Files

Now that we can change directories, we can learn how to make them!

Directories

"Directory" is just a fancy word for a folder

The command we use to create a directory/folder in our system is:

mkdir "directory/folder name"

We can also create multiple directories at once by:

mkdir css js images

Making directories is only a small part of the battle, later we will learn how to:

Change between directories and list everything in a given directory

Files

We use Files everyday whether it be a docx, pptx, or even text files.

However, with Git we can make every file we need all from the command line using the command:

touch "file name"

You can also create multiple files at once by:

touch index.html css/styles.css js/scripts.js

One thing to keep in mind when creating files is to make sure you are in the right directory before creating it.

Different Ways of Intializing a Git Repo

We learned how to change and make directories/files, lets git started!

Command Line/Terminal

Lets say you already began working on a project, but didnt set up git yet, you can do it all from your terminal.

Once you create a repo on GitHub, you can run the following commands but you need insert your own repo name

git init
git add .
git commit -m "first commit"
git remote add origin git@github.com:jakeaguilar/git-example.git
git remote -v
git push -u origin master

Github

You can also create and intialize a repo from Github as well

Below, once creating repo on Github, click the "Intialize this repository with a README"

githubrepoinit

After, clicking "Create repository", it will bring you to this page down below:

gitclonerepo

From here, click on the green button saying, "Clone or download"

A dropdown will appear, saying Clone with SSH/or HTTPS, then click on the clipboard to the right of the link.

After copying the link, open your terminal/git bash and change directories into whereever you want to save your repo/projects, for me I keep all my work in a "projects" folder.

From here, the git command you will use to clone your Github repo down to your machine is:

git clone git@github.com:jakeaguilar/git-example.git gitbashclone

Remotes

Linking our local code, to a "remote" version

Think of remotes as clone of your local repository on your machine, however it is just on another machine or server

You may have noticed in the section above explaining how to intialize git, some commands containing remote in them. Those commands were linking our local repos to our remote versions on Github.

git remote

If you were just to type this in your terminal, it would show you a list your remotes that your local repo is connected to, most of the time it is just your remote version you have on your own github account.

git remote -v

git remote -v simply tells you the name of your remote versions, as well as the url for the remote version.

git-remote-v
git remote add origin git@github.com:jakeaguilar/git-example.git

The example command above, is the same line from the above section showing us different ways to intialize git. Let's break it down:

git remote add origin "url"

This command tells git to add a remote version named "origin", (this can be named anything, but convention to use origin) at a given url, which is your github repo url made, when you create the repo on gituhb. Below is where you can find the url for your repo on Github:

git-remote-url

Pushing to Github

We have directories and files to push to Github, but how?

git add .

git add . ,simply put is a command that tells git to track ANY chnages made to the project, and then ADD them to the staging area.

This could include:

Project Files, such as, html pages, stylesheets, script files, images, and more...

File/Directory changes,: any code edits, directory path changes

git commit -m "example text"

git commit -m "", is the command you use when you've told git you have changes made to your project in your staging area but now you need to commit them to the HEAD

think of git commits, as a snapshot of your certain project at a given point in time, they arent pushing your changes up a to a remote server.

Now important note: if you just type "git commit", it will open a VI edtior, if that happens, don't panic all you simply need to do is:
1.) Press the "esc" key
2.) type ":q!", this will abort the commit due to an empty commit message.

git-commit git-commit-q!

git push

git push, does kind of what the name suggests, it pushes up your commits you made previously up to your remote repo.

Now depending on whether youve pushed up to your branch before will vary how you use the git push command

If you've never pushed to your branch before the command you will use is:

git push -u origin "branch name"

This command does a couple things, one it's pushing your commits made up to your remote, and then the "-u" is linking the local branch with the remote version of the branch

However, lets say you have already pushed to this branch before and you just need to push new commits made, you can just use:

git push

Creating Versions

We've worked on only one version of our code, but how do we make different versions?

However, the whole purpose of git is to allow you to work on multiple different branches(versions) of code, without conflicting with each other.

Git lets developers have as many branches active as they want, and they can all be worked on independently from one another until they want to merge them back together

So far we have only worked on one branch called master. This branch is created by default by git.

git branch

By using the command: git branch, it will output a list of branches currently in the repo, what branch you're on, and asterisk where the HEAD is.

To create a new branch using git branch you can use the command below:

git branch development

Below is an example of using git branch to list branches and creating a new branch:

git-branch

git checkout

Lets assume you already created a new branch called, exampleBranch, in order to switch to that branch, you'd use the command:

git checkout exampleBranch

You're now on the branch called, exampleBranch, but what if from here you want to go back to another branch? You'd use the same command except switch out the branch name for the branch you would like to switch to, like below:

git-checkout

Now that we know how to create branches, and switch to branches, there is a shortcut to create a new branch and immediately switch to it. The command is:

git checkout -b "branchName"

Here's an example of the command in use, and switching back to the original branch i was working on:

Inspecting a Repository

How can we check if files are untracked, committed, pushed?

git status

The git status command is a relatively simple command. It just shows you what's been happening with git add and git commit.

It lists which changes have been tracked,staged/not staged by git, what branch you're on, and if your remote has updates you need to pull down.

Below is an exmaple, and shows that my current changes are not staged:

git-status

It is best practice to check that state of your repo before you begin working on your projects, this will prevent you working on outdated code, or accidentally committing a portion of code you didn't mean to.

git log

The git log command displays your commit snapshots.

It will let you see your project history, filter through it, and even search for specifc changes.

The main difference between git status and git log is git status lets you inspect your working directory and staging area, while git log only works for committed history.

If your git log takes up more than terminal screen, you can scroll down with your space, arrow keys, or mouse scroll. To exit your git log simply just press "q".