# Git Basics Guide
A quick reference guide for using Git commands to create a repository, push code, and perform essential Git operations.
## 1. Setting Up Git
- **Install Git**: Download from [git-scm.com](https://git-scm.com/) and follow the installation instructions.
- **Configure Git**: Set up your name and email (required for commits).
```bash
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"- Create a Directory: Make a folder for your project and navigate to it.
mkdir project-name cd project-name - Initialize Git: Initialize a new Git repository in your project folder.
git init
- Add a README (Optional): Create a README file to describe your project.
echo "# Project Title" >> README.md git add README.md git commit -m "Add README"
- Check Status: See what changes are pending for commit.
git status
- Add Files: Stage files to commit (use
.to add all changed files).git add filename # or git add .
- Commit Changes: Commit your changes with a message.
git commit -m "Describe your changes"
- Create a Repository on GitHub: Go to GitHub, create a new repository, and copy its URL.
- Add Remote Origin: Link your local Git repository to the GitHub repo.
git remote add origin <repository-URL>
- Push Initial Commit: Send your code to GitHub (for the first push, you may need to specify
-u origin main).git push -u origin main
- For subsequent pushes:
git push
- Fetch Changes: Retrieve updates from the remote repository.
git fetch origin
- Pull Changes: Update your local repository with changes from GitHub.
git pull origin main
- Create a New Branch: Useful for adding features or working on separate tasks.
git branch branch-name
- Switch to a Branch:
git checkout branch-name
- Merge a Branch: Merge a branch into the main branch after completing work.
git checkout main git merge branch-name
- Check Commit History:
git log
- View Changes Before Committing:
git diff
- Undo Last Commit (use carefully):
git reset --soft HEAD~1 # Keeps changes git reset --hard HEAD~1 # Discards changes
- Clone Repository: Copy a repository from GitHub to your local machine.
git clone <repository-URL>
- Remote: Link to the GitHub repository (usually named
origin). - Staging Area: Where files are added before committing.
- Commit: Saves your changes locally with a description.
- Push/Pull: Send changes to or retrieve updates from GitHub.
This guide should help you get started with Git and GitHub!