Learn Git & Github

follow: https://x.com/iadityarxj
What is Git?
Git is a distributed version control system that helps developers track changes in their codebase, collaborate with others, and maintain a history of changes.
What is GitHub?
GitHub is a web-based platform for hosting Git repositories. It offers tools for collaboration, code review, and project management.
Difference between Git and GitHub
Git and GitHub are different tools with related purposes. Git is a free, open-source version control system used to track changes in files and manage code locally on your computer. It works across various operating systems, including Windows, macOS, and Linux. On the other hand, GitHub is a web-based platform for hosting Git repositories, allowing developers to store, share, and collaborate on projects online. While GitHub is one of the most popular hosting services, alternatives like GitLab and Bitbucket also exist. In short, Git is the tool, and GitHub is an online service that complements it.
What is a Version Control System (VCS)?
A Version Control System (VCS) is a tool used to track and manage changes to files, projects, or codebases over time. It allows developers to collaborate effectively, revert changes, and maintain a history of modifications. Some popular tools include Git, SVN, and Mercurial, with Git being the most widely used. A VCS ensures efficiency, collaboration, and reliability in project management.
Install Git
Windows:
Download Git from Git.
Run the installer and follow the instructions.
macOS:
Open Terminal.
Install Git using Homebrew:
brew install git
Linux:
Open Terminal.
Use your package manager to install Git:
sudo apt install git # For Debian/Ubuntu sudo yum install git # For Red Hat/CentOS
Configuring Git
After installing Git, set up your username and email to associate your commits:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
You can verify your configuration with:
git config --list
Basic Git Commands
Initializing a Repository
Start a new Git repository:
git init

Checking Repository Status
View the status of your repository:
git status

Staging and Committing Changes
Add files to the staging area:
git add <file>
git add . # Add all changes

Commit the changes:
git commit -m "Commit message"

Viewing Commit History
See a log of commits:
git log
git log --oneline

Cloning a Repository
Download an existing repository:
git clone <repository_url>

Pushing and Pulling Changes
Push changes to a remote repository:
git push origin <branch_name>

Pull updates from a remote repository:
git pull origin <branch_name>

Branching and Merging
Creating and Switching Branches
Create a new branch:
git branch <branch_name>

Switch to an existing branch:
git checkout <branch_name>

Merging Branches
Merge a branch into the current branch:
git merge <branch_name>

Deleting a Branch
Remove a branch:
git branch -d <branch_name>

Using GitHub
Connecting a Local Repository to GitHub
Create a new repository on GitHub.
Link your local repository:
git remote add origin <repository_url>
Pushing Initial Commit
Upload your first commit:
git push -u origin main
Creating Pull Requests
Push changes to a branch.
Open a pull request on GitHub to propose your changes.
Advanced Git Features
Stashing Changes
Temporarily save uncommitted changes:
git stash
Viewing and Applying Stashes
List stashed changes:
git stash list
Apply the most recent stash:
git stash apply
Resolving Merge Conflicts
When merging branches, conflicts may arise. To resolve them:
Edit conflicting files.
Mark conflicts as resolved:
git add <file>Complete the merge:
git commit
Troubleshooting Common Issues
Undoing the Last Commit
Soft reset (keep changes staged):
git reset --soft HEAD~1
Hard reset (discard changes):
git reset --hard HEAD~1
Removing Files
Remove a file from staging:
git reset <file>
Delete a file from the repository:
git rm <file>
git commit -m "Remove file"
Conclusion
By mastering Git and GitHub, you can enhance your workflow, collaborate seamlessly, and maintain a robust history of your codebase.




