Color Mode

Creating and Managing Your Git Repository

Managing your code with Git is an essential skill for developers. In this assignment, you will learn how to create a Git repository on GitHub, clone it to your local computer, and add the necessary files for your practice project. This approach is simpler and more intuitive than creating a repository locally first, mirroring how most developers work with Git in professional environments and making it easier to get started with version control.

If you are new to Git, AI can guide you through the setup process. Be sure to provide information about your operating system and your level of experience with Git and GitHub to get tailored assistance. In addition to AI support, there are numerous online resources, including YouTube tutorials, that can help you along the way. If you prefer manuals and documentation, we recommend reviewing the Pro Git book, which offers a comprehensive guide to both Git basics and advanced topics.

Assignment Instructions

If you are new to Git, we recommend starting with the GitHub-first approach. This method allows you to create your repository directly on GitHub, which is simpler and ensures your remote repository is set up correctly from the beginning. If you are comfortable with the terminal and prefer to learn the full process of creating a local repository first, you can follow the alternative instructions at the end of this document.

1. Install and Configure Git

Before working with repositories, ensure Git is installed, updated, and properly configured on your system.

Check if Git is Installed

Run the following command to check if Git is installed:


        git --version
    

If Git is installed, this will display the version number. If not, proceed to the next section to install Git.

Install Git (if needed)

Follow the instructions for your operating system to install Git:

Configure Git

Configure your global Git settings with your name and email address. This information will be attached to all your commits and is publicly visible in your repository history. If you are concerned about privacy, you may use an alias or your GitHub username instead of your real name, and consider using a no-reply email address:


        git config --global user.name "Your Name"
        git config --global user.email "your-email@example.com"
    

For privacy, you can use GitHub's no-reply email address. Replace [your-username] with your actual GitHub username:


        git config --global user.email "[your-username]@users.noreply.github.com"
    

2. Create Repository on GitHub

Start by creating your repository directly on GitHub. This approach is simpler and ensures your remote repository is set up correctly from the beginning.

  1. Log in to GitHub: Go to github.com and sign in to your account.
  2. Create a new repository:
    • Click the green New button or the + icon in the top right corner
    • Select New repository
  3. Configure your repository:
    • Repository name: cse340-practice-[lastname] (replace [lastname] with your actual last name)
    • Description: "Practice project for CSE 340" (optional but recommended)
    • Visibility: Public (this is fine for coursework)
    • Initialize repository: Check "Add a README file"
    • Add .gitignore: Select "Node" from the template dropdown
  4. Create repository: Click the green Create repository button.

Congratulations! Your repository now exists on GitHub with a README file and a Node.js .gitignore file automatically created. This gives you a solid foundation to start with.

3. Clone Repository to Your Computer

Now you will download (clone) your repository from GitHub to your local computer so you can work on it.

  1. Copy the repository URL: On your GitHub repository page, click the green Code button and copy the HTTPS URL; you may use the SSH URL if you have set up SSH keys.
  2. Choose a location: Navigate to a directory on your computer where you want to store your project. This should not be in a cloud-synced folder like OneDrive or Google Drive.
  3. Open terminal: Open your terminal or command prompt in that location; or navigate to it using the cd command.
  4. Clone the repository: Run the following command, replacing the URL with your copied repository URL:
    
                    git clone https://github.com/[your-username]/cse340-practice-[lastname].git
                
  5. Navigate to your project: Move into the newly created directory:
    
                    cd cse340-practice-[lastname]
                

You now have a local copy of your repository that is automatically connected to GitHub!

4. Customize the .gitignore File

GitHub created a Node.js .gitignore file for you, but you need to add one more entry to ensure environment files are not tracked.

Open the .gitignore file in your text editor and add .env to the beginning of the file on its own line. The file should include this line along with many other Node.js related entries that GitHub added automatically. Save the file when you are done.

If you are missing your .gitignore file, you can create a new one in the root of your repository with the following content:

            .env
            node_modules/
        

5. Add the restore.js File

During development, your server may occasionally crash or stop responding, which can leave behind orphaned server processes (background tasks) that continue running even after the main process has ended. These orphaned processes can use system resources, prevent your server from restarting properly, and may cause your server to reconnect to an outdated process instead of starting a new one.

Download the restore.js file and save it in the root directory of your project repository. To use it, run:


        node restore.js 3000 3001
    

The restore.js utility script helps you identify and terminate orphaned server processes that may remain after crashes. Keeping this script in your repository ensures you can quickly resolve issues if your server stops responding or fails to restart due to lingering processes. In the future if you realize your server is not updating and continues to show old content, running this script can help clear out any stuck processes and get your server back to a clean state.

If you are interested in learning more orphaned processes and how to manage them, consider reading the article What is an Orphan Processes? by GeeksforGeeks.

6. Configure VS Code Settings

Create a .vscode directory in the root of your repository to store Visual Studio Code settings. This will improve your development experience by customizing how VS Code displays your project files.

Inside the .vscode directory, create a settings.json file with the following content:


        {
            "explorer.compactFolders": false
        }
    

This setting disables compact folder display in the VS Code file explorer, making it easier to navigate your project structure.

7. Commit and Push Your Changes

Now you will save your changes to the repository and upload them to GitHub:

  1. Check the status: See what files have been added or changed:
    
                    git status
                
  2. Add all changes: Stage all the new files:
    
                    git add .
                
  3. Commit your changes: Create a commit with a descriptive message:
    
                    git commit -m "Added project files and VS Code settings"
                
  4. Push to GitHub: Upload your changes to GitHub:
    
                    git push
                

Visit your repository page on GitHub to verify that your files have been uploaded successfully.

Commit Often

From this point forward, make a habit of committing your code frequently. Regular commits allow your instructor and teammates to review your progress and provide timely feedback. A good rule is to commit after completing each major assignment or significant milestone in your work. Always write clear and meaningful commit messages that explain the purpose of the change. To help you stay consistent, consider following the Conventional Commits standard. For a quick reference, see the Conventional Commits Cheatsheet.

GitHub Student Benefits

As a student, you qualify for GitHub Pro for free, which offers additional features and benefits that can enhance your learning experience. These benefits are not required for this course, but they can be useful for your development projects and future career.

How to Get GitHub Pro

  1. Visit the GitHub Student Developer Pack page.
  2. Click "Get your pack" and sign in with your GitHub account.
  3. Verify your student status using your school email address. If needed, you can get a verification letter from https://web.byui.edu/verify.
  4. Wait for verification to complete. Once approved, you will have access to GitHub Pro and other developer tools.

Alternative Assignment Instructions

Command Line First (Optional)

If you prefer to create your repository locally first and then connect it to GitHub, you can use this alternative approach. This method requires more steps but gives you experience with the complete Git workflow.

This section is optional. The GitHub-first approach described above is recommended for beginners and is the method used by most professional developers. If you prefer to learn the full process, you can follow these steps to create a local repository first and then push it to GitHub.

Create Local Repository First

  1. Create a directory for your project and navigate to it:
    
                    mkdir cse340-practice-[lastname]
                    cd cse340-practice-[lastname]
                
  2. Initialize a Git repository:
    
                    git init
                
  3. Create a basic .gitignore file:
    
                    echo ".env" > .gitignore
                    echo "node_modules/" >> .gitignore
                
  4. Create an initial commit:
    
                    git add .gitignore
                    git commit -m "Initial commit with .gitignore"
                
  5. Create a repository on GitHub (without initializing files).
  6. Connect your local repository to GitHub:
    
                    git remote add origin https://github.com/[your-username]/cse340-practice-[lastname].git
                    git branch -M main
                    git push -u origin main
                

Conclusion

Your Git repository is now set up and connected to GitHub. You have a solid foundation for version control that includes proper file ignoring, development utilities, and VS Code configuration. As you progress through the course, continue using Git to manage your code changes, backup your work, and submit assignments.

Remember to commit your changes frequently with descriptive messages and push to GitHub regularly to keep your remote repository up to date.