Home Subscribe

2. Introducing Git and GitHub

As a Python programming teacher, we’ve covered a lot of ground in writing and structuring code. Now, it’s time to introduce you to an essential tool that will help you manage your code effectively, collaborate with others, and keep track of your project’s history: Git. In this article, we will learn how to use Git locally and remotely using GitHub.

2.1. What is Git?

Git is a distributed version control system (DVCS) that helps you track changes in your code over time. With Git, you can easily switch between different versions of your code, collaborate with other developers, and manage your project’s history.

2.2. What is GitHub?

GitHub is a web-based platform that provides hosting for software development using Git. It offers all of the distributed version control and source code management (SCM) functionality of Git, along with additional features like access control, collaboration, and task management.

2.3. Installing Git

Depending on your operating system, follow these steps to install Git:

Windows:

WSL (Windows Subsystem for Linux):

  • Open your WSL terminal.

  • Run the following command to update your package list:

sudo apt update
  • Install Git with the following command:

sudo apt install git

Linux (Debian-based distributions):

  • Open your terminal.

  • Run the following command to update your package list:

sudo apt update
  • Install Git with the following command:

sudo apt install git

macOS:

  • Install Homebrew (if not already installed) by following the instructions at https://brew.sh.

  • Open your terminal and run the following command to install Git:

brew install git

2.4. Configuring Git

Before using Git, it’s essential to configure your name and email address, which will be attached to your commits. Run the following commands in your terminal:

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

2.5. Initializing a Git Repository

To start using Git for a project, you need to initialize a new Git repository. Navigate to your project folder in the terminal and run:

git init

This command creates a new .git folder in your project directory, which will store all the necessary Git data.

2.5.1. Basic Git Workflow

  • Stage files: Add the files you want to track with Git using the git add command:

git add .

The . (dot) adds all files in the current directory. To add specific files, replace the . with the filenames.

  • Commit changes: Save the staged changes in your repository with the git commit command:

git commit -m "Your commit message"

The `-m`flag allows you to add a meaningful message describing the changes.

  • Check the status: To see the current status of your repository, use the git status command:

git status

2.5.2. Using GitHub

  • Create a GitHub account: Sign up for a free account at https://github.com.

  • Create a new repository: Click the "+" icon in the upper-right corner and select "New repository." Give your repository a name, choose whether to make it public or private, and click "Create repository."

  • Connect your local repository to GitHub: To connect your local repository to the remote repository on GitHub, run the following command in your terminal:

git remote add origin https://github.com/your-username/repository-name.git

Replace "your-username" and "repository-name" with the appropriate values.

  • Push your changes to GitHub: To push your local commits to the remote repository, run the following command:

git push -u origin main

This command pushes your changes to the "main" branch on the remote repository. The -u flag sets the upstream tracking reference, so you only need to use git push for future pushes.

  • Clone a repository: To clone an existing repository from GitHub, run the following command:

git clone https://github.com/your-username/repository-name.git

Replace "your-username" and "repository-name" with the appropriate values.

  • Fetch and merge changes: To fetch the latest changes from the remote repository and merge them into your local branch, run the following command:

git pull
  • Branching: To create a new branch and switch to it, run the following command:

git checkout -b new-branch-name

To switch between branches, use git checkout branch-name.

  • Merging branches: To merge changes from one branch to another, first switch to the target branch and then run the following command:

git merge source-branch-name

2.6. Conclusion

In this article, we’ve introduced you to the essentials of Git and GitHub. By understanding these fundamental concepts and commands, you’ll be well-prepared to manage your Python projects more effectively, collaborate with others, and maintain a complete history of your code. As you continue working with Git, you’ll discover even more powerful features that can help streamline your development process.



Add Comment

* Required information
1000
Drag & drop images (max 3)
What is the day after Friday?

Comments

No comments yet. Be the first!