Python Shikhi. Learn with Fun


image

Basic of Git & GitHub



Git and GitHub are essential tools in modern software development for version control and collaboration. Let’s break down the basics:

What is Git?
Git is a version control system (VCS). It helps developers manage changes to their code over time by keeping track of every modification in a project, who made it, and why. This is essential when working with teams or managing large projects.

Git is a distributed version control system, which means:

Each developer has a full copy of the project history.
Changes are tracked, so you can revert to previous versions.
It supports multiple developers working on the same project simultaneously without conflicts.
Git's Workflow Basics
Local repository: This is where you work locally on your machine.
Staging area: Before committing, files are first added here. It acts as a preparation zone.
Commit history: Once files are committed, they are part of the project’s permanent history.
Remote repository: Usually hosted on a platform like GitHub, the remote repo is shared among developers.
Key Git Concepts:
- Repository (Repo): A storage space where your code lives. It tracks all changes made to the files.
- Commit: A snapshot of the project at a specific point in time. Each commit has a message that explains the changes.
- Branch: A parallel version of the repository. You can work on different branches (like a new feature) without affecting the main codebase.
- Merge: Combining changes from one branch into another, often from a feature branch into the main branch.
- Clone: Downloading a repository from a remote location (like GitHub) to your local machine.
- Push and Pull:
  - Push: Upload local changes to a remote repository.
  - Pull: Fetch changes from a remote repository to your local machine.

 

Setting up Git (Initial Setup)

Before using Git, you’ll need to configure it on your machine.

Step 1: Install Git
You can install Git from here.

Step 2: Configure Git

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

These commands set your name and email, which will be attached to your commits.

Step 3: Initializing a Git Repository
To start tracking a project with Git, you need to initialize a repository.

git init

 
Git Basic Commands
Checking Status
To see the current state of your repository (untracked files, changes not yet committed):

git status

This is the most important command for seeing what’s happening in your repository.

Adding Files
You need to tell Git which files to track. You do this by adding them to the staging area.

git add <file_name>   # Adds a specific file
git add .             # Adds all files in the current directory

The staging area is like a waiting room for changes before they are committed.

Committing Changes
After staging your files, you create a commit, which records the changes in your local repository.

git commit -m "Descriptive commit message"

A commit message should describe the changes made, e.g., “Fixed bug in login feature.”

Viewing Commit History
To see a list of all commits made in the repository:

git log

This shows each commit, including the author, date, and commit message. You can also use:

git log --oneline

To show a more concise version.

Creating Branches
Branches allow you to work on different features or fixes without affecting the main codebase.

git branch <branch_name>  # Creates a new branch
git checkout <branch_name>  # Switches to an existing branch

For example, you might create a feature-x branch to develop a new feature while keeping the main branch stable.

Merging Branches
When you’re done working on a feature, you can merge it back into the main branch.

git checkout main          # Switch to the main branch
git merge <branch_name>    # Merge the changes from your branch into the main branch

Merging combines your feature branch with another branch.

Undoing Changes
Sometimes you make changes you don't want to keep.

To unstage a file:
git reset <file_name>

This moves the file out of the staging area but keeps the changes.

To revert changes in a file:
git checkout -- <file_name>

This restores the file to its previous committed state.

 

 

 

 

 

 

 

 

What is GitHub?
GitHub is a web-based platform that hosts Git repositories, enabling collaboration and sharing code with others. It provides additional features like issue tracking, project management, and pull requests to help teams work together.

Key GitHub Concepts:
- Repository: A project that is hosted online on GitHub.
- Pull Request (PR): A way to propose changes to a repository. Developers review the code before merging it into the main branch.
- Fork: A copy of someone else’s GitHub repository. You can make changes to this copy without affecting the original repository.
- Issues: A way to track bugs, tasks, or feature requests in a repository.
- GitHub Actions: Automated workflows for continuous integration and deployment (CI/CD).

Basic Git Commands:
- git init : Initialize a new Git repository.
- git clone <url> : Clone an existing repository from GitHub to your local machine.
- git status : Check the status of changes in your local repository.
- git add <file> : Stage a file for committing.
- git commit -m "message" : Commit changes with a message explaining the update.
- git push : Push your commits to a remote repository.
- git pull : Pull the latest changes from a remote repository.
- git checkout <branch> : Switch to a different branch.

Using GitHub:
- Create a repository : You can create a new repository via the GitHub website by clicking on the “+” sign and choosing “New repository.”
- Fork and Clone : If you want to contribute to an open-source project, you can fork it, clone it to your local machine, make changes, and push your updates.
- Pull Requests : After pushing changes to a repository, you can create a pull request to suggest your changes for review.