Git is a distributed version control system that allows developers to:
- Track changes in code.
- Collaborate on projects.
- Manage multiple versions of a project.
- Work offline with a full local repository.
- Working Directory: Where you make changes to files.
- Staging Area: Prepare changes for a commit.
- Local Repository: Stores committed changes.
- Remote Repository: Backup and share code (e.g., GitHub).
- Set username:
git config --global user.name "Your Name"
- Set email:
git config --global user.email "[email protected]"
- Default branch to
main
:git config --global init.defaultBranch main
- Initialize a repository:
git init
- Clone a repository:
git clone <repo_url>
- Check status:
git status
- Add files to staging:
git add .
(or specific file:git add filename
) - Commit changes:
git commit -m "Commit message"
- Add a remote:
git remote add origin <repo_url>
- Push changes:
git push -u origin main
- Pull changes:
git pull origin main
- Create a branch:
git checkout -b branch-name
- Switch branches:
git checkout branch-name
- Merge branch:
git merge branch-name
- Delete branch:
- Locally:
git branch -d branch-name
- Remotely:
git push origin --delete branch-name
- Locally:
- View commit history:
git log
- Show specific commit:
git show <commit_hash>
- Rollback changes:
- Soft reset:
git reset <commit_hash>
(keeps changes in working directory) - Hard reset:
git reset --hard <commit_hash>
(removes changes)
- Soft reset:
Use a .gitignore
file to exclude files or directories from tracking. Example:
node_modules/
*.log
.env
- Repositories: Host Git repositories online.
- Pull Requests: Request to merge changes into the main codebase.
- Issues: Track bugs, feature requests, or tasks.
- Actions: Automate CI/CD pipelines.
- Generate an SSH key:
ssh-keygen -t rsa -b 4096
- Add the key to your GitHub account.
- Test SSH connection:
ssh -T [email protected]
- Add a remote:
git remote add origin [email protected]:yourusername/repo.git
- Push changes:
git push -u origin main
- Create a branch:
git checkout -b feature/branch-name
- Make changes and commit:
git add . git commit -m "Feature: Added functionality"
- Push the branch:
git push -u origin feature/branch-name
- Create a pull request on GitHub.
- Merge branch into
main
.
- To undo changes to a specific commit:
git reset --hard <commit_hash>
- Push rollback to remote:
git push -f
- Create a
.github/workflows
directory. - Add a YAML file for automation:
name: Deploy to Vercel on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Deploy to Vercel run: npx vercel --prod
- Commit Often: Save milestones or completed features.
- Use Branches: Isolate features or bug fixes.
- Keep Changes Small: Easier to manage and review.
Master these basics to use Git and GitHub effectively. Practice regularly, and explore advanced topics like rebasing, stashing, and cherry-picking as you grow.