Git Basics – Part I

Reading Time

Until you understand the concepts upon which Git is built, you’ll feel like a stranger in a foreign land., Tom Preston-Werner, author of The Git Parable

"The following parable will take you on a journey through the creation of a Git-like system from the ground up. Understanding the concepts presented here will be the most valuable thing you can do to prepare yourself to harness the full power of Git.

The concepts themselves are quite simple, but allow for an amazing wealth of functionality to spring into existence. Read this parable all the way through and you should have very little trouble mastering the various Git commands and wielding the awesome power that Git makes available to you."

Download and read the The Git Parable

Reading time: 30 to 60 minutes.

Study & Practice Time

Take a 5 to 10 minute break before diving into the next video, as you will not only be learning about the basics of Git and version control, but you will also be practicing these concepts on your own Git repository.

Although the video duration is close to 15 minutes, this session will probably take more than 1 hour (or even more), since you will be practicing along with Colt Steele.

We suggest that you run the commands through the intergrated terminal of VSCode. (Tip: Press Ctrl+J to open/close the terminal while in VSCode)

Duration: 16 minutes

Learn Git in 15 Minutes

Additional Resources

This section contains helpful links to other content. It isn’t required, but you should consider it supplemental if you need to dive deeper into Git and Version Control.

Knowledge Check

This section contains questions for you to check your understanding of this lesson. If you’re having trouble answering the questions below on your own, clicking the small arrow to the left of the question will reveal the answers.

What kind of program is Git?
    • Git is a version control program.
What are the differences between Git and a text editor in terms of what they save and their record keeping?
    • A text editor can only make and save changes to a file.
    • Git tracks changes to the files and their contents over time.
Does Git work at a local or remote level?
    • Git works on a local level. Any changes you make are saved locally with Git.
Does GitHub work at a local or remote level?
    • GitHub works on a remote level. You must push your local changes (using Git) to Github.
Why is Git useful for an individual developer?
    • Git is useful for creating snapshots of your work. If you realize halfway through that you’ve messed up, it’s much easier to reset.
Why are Git and GitHub useful for a team of developers?
    • Git and GitHub are useful for teams because they can merge code together. A developer can work on one part of the code while a second developer works on another part. They can then use Git and GitHub to easily combine their changes.
LOOKING FOR HELP?

When looking for help, try doing so in the following order:

  • Did you try everything you could?
  • Did you read the documentation?
  • Did you Google for it?
  • Did you post your question on Slack/Forum?
  • Did you ask your fellow students for help?
  • Did you ask your Mentors for help?
  • Did you leave a comment on the comments section of this page?
  • Did you ask your Instructor for help?
    • Did you arrange and appointment with your instructor using Calendly? Visit this URL and set up an appointment: https://calendly.com/kostasx
    • Is it urgent? Did you try reaching him on Slack? Search for: Kostas Minaidis (kostasx)

UPDATED: 25.10.2020

Project: Practicing Git Basics

Introduction

This short project will demonstrate how to use Git to manage and track your project folders.

  1. You will set up a remote repository on GitHub and then copy it onto your local machine.
  2. Once this repo is set up locally, you will be able to use Git like a save button for your files and folders.
  3. When you have finalized your saves, you can then push your local repo up onto GitHub to share with everyone!

Learning Objectives

By the end of this lesson, you should be able to do the following:

  • Create a Git repository on GitHub and copy it onto your local machine.
  • Describe the difference between staging and committing changes.
  • Describe the difference between committing your changes and pushing them onto GitHub.
  • Explain how to check the status of your current repository from the command line.
  • Explain how to look at the history of your previous commits from the command line.

Assignment

In this project, we’ll walk through the basic Git workflow that you will use in all your projects.

Create the Repository

  1. You should have already created a GitHub account in the Setting up Git lesson. If you haven’t done that yet, you can sign up here.

  2. Create a new repository by clicking the button shown in the screenshot below.

New Repository

  1. Give your repository the name "git_test" in the repository name input field, and create the repository by clicking the green "Create repository" button at the bottom of the page.

  2. This will redirect you to your new repository on GitHub. To copy this repository onto your local machine, select the SSH option and copy the line next to it.

GitHub-SSH

  1. In the command line on your local machine, navigate to where you want to store this project, and then clone your repository on GitHub onto your computer with git clone followed by the URL you copied in the last step. The full command should look similar to:
git clone git@github.com:USER-NAME/REPOSITORY-NAME.git

git clone

  1. That’s it! You have successfully connected the repository you created on GitHub to your local machine. To test this, you can cd into the new git_test folder that was downloaded and then enter git remote -v in your command line. This will display the URL of the repository you created in GitHub, which is the remote for your local copy.
cd git_test
git remote -v

You may have also noticed the word origin at the start of the git remote -v output, which is the name of your remote connection. The name "origin" is both the default and the convention for the remote repository, but it could have just as easily been named "party-parrot" or "dancing-banana". (Don’t worry about the details of origin for now; it will come up again near the end of this tutorial.)

git origin

Use the Git Workflow

  1. Create a new file in the git_test folder called "README.md" with the command touch README.md.

touch README

  1. Type git status in your terminal. In the output, notice that your README.md file is shown in red, which means that this file is not staged.

git status

  1. Type git add README.md. This command adds your README.md file to the staging area in Git. Now, type git status again. In the output, notice that your file is now shown in green, which means that this file is now in the staging area.

git add README

  1. Type git commit -m "Add README.md" and then type git status once more. The output should now say, "nothing to commit", indicating that your changes have been committed.

git commit README

  1. Type git log and look at the output. You should see an entry for your "Add README.md" commit. You will also see details on the author who made the commit and the date and time for when the commit was made.

Add Another File

  1. Create a new file in the git_test folder called hello_world.txt. In the terminal, type git status, and notice hello_world.txt is not staged.

git test

  1. Open README.md in your text editor of choice and add the text "This is (YourUsername)’s first git project!" and then save the file.

first README

  1. Back in your terminal, type git status, and notice that README.md is now shown as modified, and not staged or committed. This is because you made a change to it, and it is already a tracked file.

git status modified

  1. Add README.md to the staging area with git add README.md.

  2. Can you guess what git status will output now? README.md will be displayed in green text, while hello_world.txt will still be in red. This means that only README.md has been added to the staging area.

  1. Now, add hello_world.txt to the staging area with a slightly different command: git add ., where the full stop means to add all files that are not staged. Then, type git status once more, and everything should now be in the staging area.

  1. Finally, let’s commit all of the files that are in the staging area and add a descriptive commit message git commit -m "Add hello_world.txt and edit README.md". Then, type git status once again, which will output "nothing to commit".

  1. Take one last look at your commit history by typing git log. You should now see two entries.

Push Your Work to GitHub

Finally, let’s upload your work to the GitHub repository you created at the start of this tutorial.

  1. Type git push origin master.

  1. Type git status one final time. It should output "nothing to commit, working tree clean".

  1. When you reload the repository on GitHub, you should see the README.md and hello_world.txt files that you just pushed there from your local machine.

Conclusion

The main take away from the past few lessons is how to use Git and GitHub for your projects. You now have this very powerful skill that will help you immensely when we get into the coding projects. You will be able to share your work with others for code reviews and to get help with your code if you’re stuck.

In later Git lessons, we will cover some of the more advanced Git features, such as branches, which will further expand your abilities and make you more productive.

For now, concentrate on using the basics of Git that you’ve learned here with all of your projects from now on. You will soon know each of the basic Git commands from memory!

Additional Resources

This section contains helpful links and supplemental content.

LOOKING FOR HELP?

When looking for help, try doing so in the following order:

  • Did you try everything you could?
  • Did you read the documentation?
  • Did you Google for it?
  • Did you post your question on Slack/Forum?
  • Did you ask your fellow students for help?
  • Did you ask your Mentors for help?
  • Did you leave a comment on the comments section of this page?
  • Did you ask your Instructor for help?
    • Did you arrange and appointment with your instructor using Calendly? Visit this URL and set up an appointment: https://calendly.com/kostasx
    • Is it urgent? Did you try reaching him on Slack? Search for: Kostas Minaidis (kostasx)

UPDATED: 25.10.2020

Git Basics – Part II

Introduction

In this lesson, we’ll cover common Git commands used to manage your projects and to upload your work onto GitHub. We refer to these commands as the basic Git workflow. When you’re using Git, these are the commands that you’ll use 70-80% of the time, so if you can get these down, you’ll be more than halfway done mastering Git!

Learning Objectives

By the end of this lesson, you should be able to do the following:

  • Describe how to copy an existing repository from Github onto your local machine.
  • Explain the two-stage system that Git uses to save files.
  • Describe how to upload your work to GitHub using Git.
  • Describe how to check the status of your files and how to view your commit history.

Assignment

  1. Watch the next video by Corey Schafer for a great overview of some basic Git commands.

  1. Read 5 Git Commands You Should Know, with Code Examples and spend some time practicing on the commands described in the post.

  2. Follow this excellent guide on how to Learn Git and Version Control in an Hour by Amarachi Emmanuela Azubuike . Student Approved Resource

Cheatsheet

This is a reference list of the most commonly used Git commands. (You might consider bookmarking this handy page.) Try to familiarize yourself with the commands so that you can eventually remember them all:

  • Commands related to a remote repository:
    • git clone git@github.com:USER-NAME/REPOSITORY-NAME.git or git clone https://github.com/user-name/repository-name.git
    • git push origin master
  • Commands related to workflow:
    • git add .
    • git commit -m "A message describing what you have done to make this snapshot different"
  • Commands related to checking status or log history
    • git status
    • git log

The basic Git syntax is program | action | destination.

For example,

  • git add . is read as git | add | ., where the period represents everything in the current directory;
  • git commit -m "message" is read as git | commit -m | "message"; and
  • git status is read as git | status | (no destination).

Live Session

Watch the following live session that was recorded on 23/09/2020 to understand more about the basic concepts of Git and walk yourselves through some basic examples.

Part I | Duration: 1h 33m

Part II | Duration: 1h 30m

Part III | Duration: 1h 24m

Conclusion

You may not feel completely comfortable with Git at this point, which is normal. It’s a skill that you will get more comfortable with as you use it. Therefore, we have a project coming right after this lesson where we’ll walk you through the entire Git workflow, which is the exact same process you would use in a real project.

The main thing to take away from this lesson is the basic workflow. The commands you’ve learned here are the ones you will be using the most often with Git.

Don’t worry if you don’t know all the commands yet or if they aren’t quite sticking in your memory yet. They will soon be seared into your brain as you use them over and over in future projects.

Additional Resources

This section contains helpful links to other content.

Duration: 12 minutes

Knowledge Check

This section contains questions for you to check your understanding of this lesson. If you’re having trouble answering the questions below on your own, clicking the small arrow to the left of the question will reveal the answers.

What is the Git command used to get a full copy of an existing Git repository from Github?
    • Use git clone git@github.com:<your-github-username>/<your-respository-name> to clone a GitHub repository onto your local machine.
What is the Git command used to check the status of your files?
    • Use git status to see any changes made since your last commit.
What is the Git command used to track files with Git?
    • Use git add to track files.
What is the Git command used to commit files?
    • Use git commit to commit tracked files.
What is the Git command used to view your commit history?
    • Use git log to view your commit history.
What is the Git command used to upload projects onto GitHub?
    • Use git push to send your commit to GitHub.
Explain the two-stage system that Git uses to save files.
    • A save in Git is divided into two terminal commands: add and commit. The combination of these two commands gives you control of exactly what you want to be remembered in your snapshot.
    • Staging: Think of add as adjusting the number of people or elements to be included in a photo. With Git, you can select the changes you want to save with git add. Imagine a project that contains multiple files where changes have been made to several files. You want to save some of the changes you have made and leave some other changes to continue working on them.
    • Committing: Think of commit as actually taking a photo, resulting in a snapshot. For example, to commit a file named README.md, type git commit -m "Add README.md". The -m flag stands for “message” and must always be followed by a commit message inside quotation marks. In this example, the commit message was "Add README.md".
Explain what origin is in git push origin master.
    • In Git, origin is a placeholder name for the URL of the remote repository. Git sets up the origin by default when it clones a remote repository. You can use origin to access the remote repository without having to enter a full URL every time. This also means that you can have multiple remotes for a repository by giving each a unique name.
Explain what master is in git push origin master.
    • In Git, master is the branch of the remote repository you want to push your changes to. We will get more into branches in a later lesson, but the main thing to remember is that master is the official branch in your projects where production-ready code lives.

Material based on Erik Trautman | The Odin Project
LOOKING FOR HELP?

When looking for help, try doing so in the following order:

  • Did you try everything you could?
  • Did you read the documentation?
  • Did you Google for it?
  • Did you post your question on Slack/Forum?
  • Did you ask your fellow students for help?
  • Did you ask your Mentors for help?
  • Did you leave a comment on the comments section of this page?
  • Did you ask your Instructor for help?
    • Did you arrange and appointment with your instructor using Calendly? Visit this URL and set up an appointment: https://calendly.com/kostasx
    • Is it urgent? Did you try reaching him on Slack? Search for: Kostas Minaidis (kostasx)

UPDATED: 24.02.2021

CONTRIBUTORS:

  • Alkis Green (for suggesting the "Learn Git and Version Control in an Hour" post)
  • Vincent Le for pointing out broken links and suggesting git resources

Introduction to Git

Prerequisites

Before moving on to this section, you must feel comfortable working with the terminal, be able to execute basic commands and maniulate files and folders using command line utilities like cd, ls, touch, rm, etc.

Learning Objectives

By the end of this lesson, you should be able to do the following:

  • Explain what Git and GitHub are and the differences between the two.
  • Describe the differences between Git and a text editor in terms of what they save and their record keeping.
  • Describe why Git is useful for an individual developer and a team of developers.
  • Explain what Version Control is

Introduction

Watch the next videos to learn about the history of Git and GitHub.

Duration: 5 minutes

What’s the Difference between Git and GitHub?

Duration: 2 minutes

Duration: 4 minutes

A Detailed Overview

Git is like a really epic save button for your files and directories. Officially, Git is a version control system.

A save in a text editor records all of the words in a document as a single file. You are only ever given one record of the file, such as essay.doc, unless you make duplicate copies (which is difficult to remember to do and keep track of):

essay-draft1.doc, essay-draft2.doc, essay-final.doc

However, a save in Git records differences in the files and folders AND keeps a historical record of each save. This feature is a game changer. As an individual developer, Git enables you to review how your project grows and to easily look at or restore file states from the past. Once connected to a network, Git allows you to push your project to GitHub for sharing and collaborating with other developers.

While Git works on your local machine, GitHub is a remote storage facility on the web for all your coding projects. This means that by learning Git, you will get to showcase your portfolio on GitHub! This is really important because almost all software development companies consider using Git to be an essential skill for modern web developers. Having a GitHub portfolio will provide proof to future potential employers as to what you are capable of.

In this lesson, we will briefly explore the history of Git, what it is, and what it’s useful for.

In the next lesson, we will go over the basic workflow for using Git, which should enhance your understanding and demonstrate why Git is so useful.

Finally, you will set up a project with Git that will serve as a template for your future projects.

For now, let’s learn what Git is and why it’s so powerful!

Terminology

Before moving on, we should familiarize ourselves with the basic terms of Version Control and Git.

Download this Cheat Sheet (PDF), take a first look at each term and make sure to keep this file open while you go through the Git lessons, as you will need to come back to this terms sheet quite often.


Material based on Erik Trautman | The Odin Project
LOOKING FOR HELP?

When looking for help, try doing so in the following order:

  • Did you try everything you could?
  • Did you read the documentation?
  • Did you Google for it?
  • Did you post your question on Slack/Forum?
  • Did you ask your fellow students for help?
  • Did you ask your Mentors for help?
  • Did you leave a comment on the comments section of this page?
  • Did you ask your Instructor for help?
    • Did you arrange and appointment with your instructor using Calendly? Visit this URL and set up an appointment: https://calendly.com/kostasx
    • Is it urgent? Did you try reaching him on Slack? Search for: Kostas Minaidis (kostasx)

UPDATED: 31.12.2020

Contributors: