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.
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
- In GitHub, create a new empty repository (no README, no .gitignore).
- 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:
-
git init- Initializes a new local Git repository in the current folder.
-
git status- Shows the current branch, staged/unstaged changes, and untracked files.
-
git add <file>/git add .- Stages files to be included in the next commit.
-
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-astages all tracked modified files automatically.
-
git branch <branchname>- Creates a new branch based on the current branch.
-
git checkout <branchname>- Switches the working directory to the specified branch.
- Shorthand from context:
git checkout -b <branchname>creates and switches in one step.
-
git merge <branchname>- Merges the specified branch into the current branch.
-
git remote add origin <clone URL>- Adds a remote named
originpointing to a Git server (GitHub in this case).
- Adds a remote named
-
git push -u origin main- Pushes the local
mainbranch to the remoteoriginand sets tracking so futuregit pushcommands know which remote branch to update.
- Pushes the local
-
git push- Sends local commits on the current branch to the configured upstream branch on the remote.
- Context example:
git pushafter 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: