DEV Community

Cover image for Git Guide: Pushing a Project to a Repository with a Python Project πŸπŸš€
Niladri Das
Niladri Das

Posted on

Git Guide: Pushing a Project to a Repository with a Python Project πŸπŸš€

This guide will walk you through the steps to push a local project to a remote Git repository.

Step 1: Navigate to Your Project Directory

Open a terminal and navigate to your project directory using the cd command:

cd path/to/your/project
Enter fullscreen mode Exit fullscreen mode

Step 2: Initialize a Git Repository

Initialize a new Git repository in your project directory:

git init
Enter fullscreen mode Exit fullscreen mode

Step 3: Add Your Files

Add all the files in your project to the Git repository:

git add .
Enter fullscreen mode Exit fullscreen mode

Step 4: Commit Your Changes

Commit the changes with a message:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

Step 5: Link Your Local Repository to the Remote Repository

Link your local repository to the remote repository:

git remote add origin your-repository-url
Enter fullscreen mode Exit fullscreen mode

Replace your-repository-url with the URL of your remote repository.

Step 6: Push Your Local Repository to the Remote Repository

Finally, push your local repository to the remote repository:

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

If you're asked for a username and password, enter your GitHub username and password.

Congratulations! You've now pushed your project to a remote Git repository.

User Information Input and Storage 🐍

This Python project prompts the user for their name, age, and hobbies, and stores this information in a JSON file.

import json

def get_input(prompt, validation_func):
    while True:
        data = input(prompt)
        if validation_func(data):
            return data
        else:
            print("Invalid input. Please try again.")

def get_hobbies():
    hobbies = []
    while True:
        hobby = input("Enter a hobby (type 'done' when finished): ")
        if hobby.lower() == 'done':
            break
        elif hobby:  # check if hobby is not an empty string
            hobbies.append(hobby)
        else:
            print("Invalid input. Please try again.")
    return hobbies

def save_user_info(name, age, hobbies):
    user_info = {
        "name": name,
        "age": age,
        "hobbies": hobbies
    }
    try:
        with open("user_info.json", "w") as file:
            json.dump(user_info, file)
    except IOError:
        print("Error writing to file. Please check if the file is open in another program.")

def main():
    name = get_input("Enter your name: ", lambda x: x)
    age = get_input("Enter your age: ", lambda x: x.isdigit() and int(x) > 0)
    print("Enter your hobbies (type 'done' when finished):")
    hobbies = get_hobbies()

    save_user_info(name, age, hobbies)

    print("\nName:", name)
    print("Age:", age)
    print("Hobbies:", ", ".join(hobbies))

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode
{"name": "Niladri", "age": "23", "hobbies": ["Cooking", "Football", "Cricket"]}
Enter fullscreen mode Exit fullscreen mode

How to Run

  1. Ensure you have Python installed on your machine.
  2. Clone this repository or download the main.py file.
  3. Open a terminal and navigate to the directory containing main.py.
  4. Run the command python main.py.

How it Works

When you run main.py, the program will prompt you for your name, age, and hobbies.

For the hobbies, you can enter as many as you like. After each hobby, press enter to input another one. When you're done entering hobbies, type 'done' and press enter.

The program will then store your information in a JSON file named user_info.json in the same directory.

Future Runs

On subsequent runs, the program will first display the information entered during the previous run. If it's the first time you're running the program or if the user_info.json file doesn't exist, it will print "No previous user information found."

Project Level

This project is suitable for both high school and introductory university level courses in programming or computer science. It's a good project for learning and practicing Python basics.

Follow me on GitHub & 𝕏 for more excitement! πŸš€

Top comments (0)