Module 2: Git Basics
Module Overview
In this module, you'll learn about version control using Git. You'll learn how to use Git to move files from a working directory to the Git staging area and a local Git commit, and how to push local changes to make them permanent.
Learning Objectives
- Use Git to move files from a working directory to the Git staging area and a local Git commit.
- Use Git to push local changes and make them permanent.
Content
Git Basics - Part 1
Understanding Git
Git is a distributed version control system that helps you track changes in your code and collaborate with others. It was created by Linus Torvalds in 2005 for development of the Linux kernel.
Key Concepts in Git
- Repository (Repo): A storage location for your project, containing all the files and the history of changes made to those files.
- Working Directory: The directory on your local machine where you modify files.
- Staging Area (Index): An intermediate area where changes are added before committing.
- Commit: A snapshot of the changes you've made, saved to the repository history.
- Branch: A parallel version of the repository that allows you to work on features separately.
- Remote: A version of your repository hosted on a server (like GitHub).
Basic Git Workflow
- Make changes in your working directory
- Stage the changes you want to commit
- Commit the changes to your local repository
- Push the changes to a remote repository (if applicable)
Git Basics - Part 2
Essential Git Commands
Setting Up
git init
- Initializes a new Git repository in the current directorygit clone <url>
- Creates a copy of a remote repository on your local machinegit config --global user.name "Your Name"
- Sets your username for commitsgit config --global user.email "your.email@example.com"
- Sets your email for commits
Working with GitHub Classroom
When working with projects from GitHub Classroom:
- Open the GitHub Classroom Repo, click the green Code button, then copy the HTTPS URL
- Clone the repository to your local machine:
git clone <copied-url>
- Create a branch and work only in that branch:
git branch <name-of-branch> git checkout <name-of-branch>
Basic Git Commands
- Fork - Creates a copy of a repository on your account (typically done through GitHub's web interface)
- Clone -
git clone <url>
- Downloads a repository to your local machine - Add -
git add .
- Stages all changes for commit (the.
includes all changes) - Commit -
git commit -m "message"
- Creates a snapshot of your staged changes with a descriptive message - Push -
git push
- Uploads your commits to the remote repository - Pull Request - A way to propose changes to a repository (done through GitHub's web interface)
Example Git Workflow
# Clone a repository
git clone https://github.com/username/repository.git
# Create and switch to a new branch
git branch feature-branch
git checkout feature-branch
# Make changes to files...
# Stage and commit changes
git add .
git commit -m "Add new feature"
# Push changes to remote repository
git push --set-upstream origin feature-branch
# Create a pull request on GitHub
Setting Your Upstream
When you're on a new branch, the first time you push you may need to set your upstream:
git push --set-upstream origin <name-of-branch>
After setting the upstream once, you can simply use git push
for future pushes.
Resource
Curated Content
What is Version Control?
What is Git?
Git - Core Concepts
Guided Project
Working with Branches
In this guided project, you'll learn how to work with Git branches, which are essential for collaborating on projects and developing features in isolation.
Key Skills You'll Practice
- Creating and switching between branches
- Managing changes across different branches
- Resolving conflicts that might arise when merging branches
- Using branches effectively in a development workflow
Working with Sprint Projects
For sprint projects, it's recommended to:
- Branch off of main into a new branch (e.g., sprint-1)
- Complete all mastery tasks for that sprint in the branch
- When starting a new sprint, create a new branch (e.g., sprint-2) from either the previous sprint branch or main
- Make sure each sprint has its own branches on GitHub, as each branch will be independently scored
Mastery Task 1 - Pulling Down The Project
Mastery Task Guidelines
Mastery Tasks are opportunities to test your knowledge and understanding through code. When a mastery task is shown in a module, it means that we've covered all the concepts you need to complete that task. You will want to make sure you finish them in a timely manner to stay on track and complete the sprint.
Each mastery task must pass 100% of the automated tests and code styling checks. Your code must be your own. If you have any questions, feel free to reach out for support.
Project Setup
We will download the project and run a test to ensure everything is working (note: at first, it won't all work).
We'll download the project using your terminal, git, and GitHub. Let's get started!
Download the project
Next, you'll need to fork the Unit Project into your GitHub account. Clicking on the fork button should create a copy of the project under your own GitHub account. You'll know it's done correctly when you see that the address in the browser's URL bar includes your own GitHub username in it (instead of "BloomTechBackend").
When you see your project's GitHub repository, click on the green Code button and copy the HTTPS URL (you may need to click "HTTPS" to get the correct link).
With the link in hand, open your terminal and find where you'd like to store your project. Then type the following command:
git clone <your copied url here>
That should clone down the project into your folder.
Make sure to clone your own fork. If you accidentally clone the original repository under the "BloomTechBackend" account, you won't be able to push code up.
Password Issues
If you are prompted to log in with your GitHub username and password, but it doesn't work with your username/password combination, that is because the password needs to be a generated token from GitHub. Not a problem! The steps to create a token are very simple.
- Go to your tokens page and click on "Generate new token" ("token" is synonymous with password in this case).
- Make sure to check the box for the "repo" scope. Checking this box will allow you to push and pull from your codebase.
- Give your token a name, and you're done! Scroll to the bottom and click "Generate token".
Once it's created, the token will appear on your screen. Copy it since it won't be available again. Use that token as your password when you clone the repository.
Downloading Java
You'll need to download the right version of Java to run this project (and all future projects). For this course, we'll be exclusively using Java 11.
The simplest way to install and use Java 11 is through IntelliJ. Open up IntelliJ and open your project. From there, go to File > Project Structure.
With the Project Structure window open, click on the SDK tab on the left and then click the + button. Select "Download JDK".
In the Download JDK window, you'll select the following, then hit download:
- Version: 11
- Vendor: Amazon Corretto
- Location: leave as is
With that part done, go to the Project tab and under Project SDK, select "corretto-11".
You're now all set with the version of Java that we'll be using throughout this course.
Running Tests
An excellent practice is ensuring the tests are running correctly and everything is in order. You can do this from the terminal by running the following:
./gradlew test --tests com.adventure.MT1
Drat! Something didn't work. This happens more than you think, but that's why we have tests. Read the error and look for a message that says, "There were failing tests. See the report at: ". Copy that path into your browser's address bar and hit return. It will open up a file that's been generated on your computer.
You should now see a failed test report. Click on the one test to get the failure's details. The top line (starting with "org.opentest4j.AssertionFailedError") will give you the file's name that you need to look up and how to make the fix. Now go and make the fix!
Once the file is found, and a fix is applied, run the command:
./gradlew test --tests com.adventure.MT1
again and see if it succeeds. If the test runs successfully, then you're done.