Share via

Git and GitHub

Cheekurthi.Surekha 0 Reputation points
2026-03-26T05:53:25.2633333+00:00

Create a simple Python project (for example, a student management system using classes). Then explain step-by-step how to initialize a Git repository, commit the code, create a new branch, merge it, and push the project to GitHub. Also explain common Git commands used in the process.

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-03-26T05:53:45.0066667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Below is a concise, end-to-end example using Git and GitHub with a simple Python project, followed by explanations of the common Git commands used.


    1. Create a simple Python project

    Example project structure:

    student-management/
    ├── student.py
    └── main.py
    

    student.py:

    class Student:
        def __init__(self, student_id, name, grade):
            self.student_id = student_id
            self.name = name
            self.grade = grade
    
        def __str__(self):
            return f"{self.student_id}: {self.name} ({self.grade})"
    

    main.py:

    from student import Student
    
    students = []
    
    def add_student(student_id, name, grade):
        student = Student(student_id, name, grade)
        students.append(student)
    
    
    def list_students():
        for s in students:
            print(s)
    
    
    if __name__ == "__main__":
        add_student(1, "Alice", "A")
        add_student(2, "Bob", "B")
        list_students()
    

    Place these files in a folder named student-management.


    2. Initialize a Git repository

    From a terminal/command prompt, go into the project folder:

    cd path/to/student-management
    

    Initialize Git:

    git init
    

    This creates a new local Git repository in the current folder.


    3. Stage and commit the initial code

    Check the status:

    git status
    

    Stage all files:

    git add .
    

    Commit the staged files:

    git commit -m "Initial commit: basic student management system"
    

    This records a snapshot of the current project state in the local repository.

    (From the context, a similar commit example is shown using git commit -a -m "My first commit", where -a commits all tracked changes.)


    4. Create and switch to a new branch

    Create a new branch, for example feature-search:

    git branch feature-search
    

    Switch to that branch:

    git checkout feature-search
    

    Or do both in one step (shorthand from the context):

    git checkout -b feature-search
    

    Now all changes will be recorded on feature-search instead of main.


    5. Make changes on the feature branch and commit

    Modify main.py to add a simple search function:

    def find_student_by_id(student_id):
        for s in students:
            if s.student_id == student_id:
                return s
        return None
    
    
    if __name__ == "__main__":
        add_student(1, "Alice", "A")
        add_student(2, "Bob", "B")
        list_students()
    
        print("Searching for student with ID 2:")
        result = find_student_by_id(2)
        if result:
            print("Found:", result)
        else:
            print("Not found")
    

    Stage and commit the change:

    git status
    git add main.py
    git commit -m "Add search by student ID"
    

    6. Merge the feature branch back into main

    Switch back to main:

    git checkout main
    

    Merge the feature branch into main:

    git merge feature-search
    

    If there are no conflicts, main now includes the search feature.


    7. Create a GitHub repository and connect the local repo

    1. In GitHub, create a new empty repository (no README, no .gitignore).
    2. Copy the repository’s clone URL (HTTPS or SSH).

    From the context, the pattern to connect a local repo to a remote is:

    git remote add origin <clone URL>
    

    Run this in the student-management folder, replacing <clone URL> with the URL copied from GitHub:

    git remote add origin https://github.com/your-account/student-management.git
    

    This sets origin as the remote pointing to the GitHub repository.


    8. Push the project to GitHub

    Push the main branch to GitHub and set upstream tracking (from the context):

    git push -u origin main
    

    After this one-time setup, subsequent pushes from main can use:

    git push
    

    To push the feature branch as well (if desired):

    git push -u origin feature-search
    

    Now the project and its branches are stored remotely on GitHub.


    9. Common Git commands used and what they do

    From the context and the example above:

    1. git init
      • Initializes a new local Git repository in the current folder.
    2. git status
      • Shows the current branch, staged/unstaged changes, and untracked files.
    3. git add <file> / git add .
      • Stages files to be included in the next commit.
    4. git commit -m "message"
      • Records a snapshot of staged changes in the repository with a message.
      • Context example: git commit -a -m "My first commit" where -a stages all tracked modified files automatically.
    5. git branch <branchname>
      • Creates a new branch based on the current branch.
    6. git checkout <branchname>
      • Switches the working directory to the specified branch.
      • Shorthand from context: git checkout -b <branchname> creates and switches in one step.
    7. git merge <branchname>
      • Merges the specified branch into the current branch.
    8. git remote add origin <clone URL>
      • Adds a remote named origin pointing to a Git server (GitHub in this case).
    9. git push -u origin main
      • Pushes the local main branch to the remote origin and sets tracking so future git push commands know which remote branch to update.
    10. git push
      • Sends local commits on the current branch to the configured upstream branch on the remote.
      • Context example: git push after committing changes to share them with the remote repository.

    These commands implement the typical workflow described in the context: create a branch, commit changes, and push the branch to share it with others.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.