How to Create and Upload Your First Project to GitHub
Tags: github, git
Published: 2024-07-03 20:57:11

# Introduction

Creating and uploading your first project to GitHub is a fundamental skill for any developer. It provides a platform to collaborate with others, track changes, and showcase your work. This guide will walk you through the process of generating a Java project using Spring Initializr and pushing your project to GitHub. By the end of this article, you’ll have your project up and running on GitHub, ready for collaboration and version control.

# Approaches to Creating and Uploading a Project to GitHub

There are two common approaches to creating and uploading a project to GitHub. You can either create the project on GitHub, check it out, generate the code on top of it, and then push it to GitHub, or generate the code first and then upload it to GitHub. Both methods are effective, and the choice depends on your workflow preference. Below are the detailed steps for each approach.

## Option 1: Create Project in GitHub, Check It Out, Generate Code on Top, Then Push to GitHub

1. **Create a New Repository on GitHub:**
  * Go to GitHub and create a new repository.
  * Fill in the repository details, you may also select the options to include a README.md, .gitignore, and optionally a license.

2. **Clone the Repository Locally:**
  * You can do it either via IntelliJ, via File > New > Project, or using the "git clone" command from the command line.

3. **Generate Code Using Spring Initializr:**
  * In IntelliJ IDEA, go to File > New > Project.
  * Select Spring Initializr and configure your project (Group, Artifact, Dependencies, etc.).
  * Ensure you select the directory where you cloned the GitHub repository as the project location.

4. **Add and Commit Generated Code:**
  * Open the terminal in IntelliJ IDEA or your preferred terminal.
  * Navigate to your project directory.
  * Add the generated code to your local repository:
    ```
    git add .
    git commit -m "Generated initial code using Spring Initializr"
    ```

5. **Push Changes to GitHub:**
  * Push the changes to the GitHub repository:
    ```
    git push origin <your code branch>
    ```

## Option 2: Generate Code First and Then Upload to GitHub

1. **Generate Code Using Spring Initializr:**
  * Open IntelliJ IDEA.
  * Go to File > New > Project.
  * Select Spring Boot and configure your project (Group, Artifact, Dependencies, etc.).
  * Click Create, and IntelliJ IDEA will generate the project code for you.

2. **Initialize Local Git Repository:**
  * Open the terminal in IntelliJ IDEA or your preferred terminal.
  * Navigate to your project directory.
  * Run the following commands to initialize a local Git repository:
    ```
    git init
    git add .
    git commit -m "Initial commit"
    ```

3. **Create a New Repository on GitHub:**
  * Go to GitHub and create a new repository. The name of your new repository can be the same as your project directory name, but it doesn't have to be.
  * Fill in the repository details, and you can also select the options to include a README.md, .gitignore, and optionally a license.
  * Do not add any other files as they might conflict with your local repository.

4. **Push Local Repository to GitHub:**
  * In your terminal, link your local repository to the GitHub repository and push the code:
    ```
    git remote add origin <your GitHub repo URL>
    git pull origin <GitHub branch> --allow-unrelated-histories
    git push -u origin <your code branch>
    ```
  (The `--allow-unrelated-histories` option in Git is used when merging two branches that do not share a common base commit. By default, Git prevents merging histories that have no common ancestor to avoid accidentally combining unrelated projects. The `--allow-unrelated-histories` flag allows this merge to proceed.)

# Conclusion

By following the steps outlined in this guide, you have successfully created a Java project with Spring Initializr and pushed your project to GitHub. While this guide focused on a Java project, the principles and steps are applicable to a wide range of programming languages and frameworks. Now that your project is on GitHub, you can easily share it with others, manage contributions, and maintain a robust development workflow.