Skip to content

Instantly share code, notes, and snippets.

@sirsaw
Last active July 9, 2019 22:44
Show Gist options
  • Select an option

  • Save sirsaw/d90b421ce7b76f8948627069e9ff5087 to your computer and use it in GitHub Desktop.

Select an option

Save sirsaw/d90b421ce7b76f8948627069e9ff5087 to your computer and use it in GitHub Desktop.

Beginner's Guide to Git

By Shane Wathen, For Turing Mod 0, Session 4, Assignment 3

Summary:

Git is used to keep track of, and allow access to, multiple saved versions of a file or project (multiple files) over time. In other words, Git allows you to go back in time to a previously saved version of a file, in the event that you made a mistake or unwanted change in a more recent version.

Before a project is uploaded to a GitHub repo, it begins locally on one person's machine. This is the level where Git itself operates. Git keeps track of the file changes within your own computer based on a "commit." Essentially, you can create a marker in time for a specific version of a file or set of files by creating a commit in Git. This will allow you to go back to that point in time for a specific file should you make a mistake or unwanted change.

Note: Git is different from GitHub. GitHub is used as an online repository, or storage space, for your Git projects. This allows you to have a non-local backup of your commits. More importantly, GitHub allows collaboration between multiple people on a single project. Each collaborator can download or "pull" a copy of the project from GitHub to work on it locally. Once someone has made changes they want to share with the other collaborators, they can then "push" or upload their local commit(s) to the GitHub repository for the others to see. Other people can then add on to those changes, allowing multiple people to work on a single project.

Step-by-Step Outline for Using Git

Each of these steps is explained in greater detail below.

  1. Create or identify the folder containing files where you want to use Git
  2. Initialize Git within the desired folder
  3. Add files to Git staging
  4. Commit files to Git
  5. Use Git status and/or diff commands

1. Creating a Folder for a Git

If you already have a folder created, skip the first sub-step of this section

  1. Using your computer's terminal, create a directory / folder where you'd like to have your files which will be tracked by Git. To do this, use the mkdir command.
  2. Navigate into the desired folder using the cd command
  3. Make note of the folder path for future reference using the pwd command
  4. Determine the contents of the folder using the ls command

2. Initializing Git

  1. Once you have navigated into your desired folder using the terminal, type git init
  2. To check if Git was successfully enabled, type git status. If successful, you should see the following message:
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

3. Adding Files to Git Staging

Now we'll add files to Git's staging area. The staging area is where you prepare your files to be tracked by Git.

  1. Type ls to list the contents of your current folder
  2. Make a note of the names of files - and their extensions - which you want Git to keep track of. In this example, we will be using a files named "testfile.txt" and "testfile2.txt"
  3. Type "git add (name)" once for each file, replacing (name) with the actual file name. For example, type git add testfile.txt and git add testfile2.txt to add both files to Git's staging area.
  4. Since you will not receive a confirmation message when adding files to the staging area, you can use Git Status. Type git status to confirm the files have been added to Git's staging. You should see something similar to the following message:
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   testfile.txt
	new file:   testfile2.tx

4. Committing Files to Git

Commit is used when you want to create a "save point" or a marker in time for the current version of your staged files

  1. Each commit should be accompanied by a commit comment, which gives some information on what was done on that commit.
    • Commit comments always begin with a capital letter
    • Commit comments should give a general idea of what was done on that commit
    • For instance, you should note if you have added or changed a file.
    • The argument to create a commit comment is -m followed by the commit message in quotes.
    • The comment for your first commit should always be "Initial commit."
  2. Since this is our first commit for this project, type git commit -m "Initial Commit." You should see something resembling the following message as a confirmation:
2 files changed, 2 insertions(+)
 create mode 100644 testfile.txt
 create mode 100644 testfile2.txt
  1. For subsequent commits, you will need to re-add the desired files to staging, then commit them using a different commit message outlining the changes.

5. Using Git Diff and Git Status

As already mentioned abvove, git status can be used to display the current status of your Git management.
Additionally, we can use git diff followed by a filename to show us any differences between files which are currently staged, versus their previously commited versions.

(One Last Thing: Adding an Image with Markdown)

Since I could not find an appropriate / relevant place to add an image above, please accept this picture of my dogs as proof of my ability to embed an image in a markdown document: alt text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment