Version Control Workflow: Git and GitHub for Beginners
What is Git?
Git is a distributed version control system that allows multiple developers to
collaborate on a project while keeping track of changes.
What is GitHub?
GitHub is a cloud-based hosting service for Git repositories that facilitates
collaboration, version control, and project management.
Basic Git Workflow:
1. **Initialize a Repository**:
To create a new repository, run:
```bash
git init
```
2. **Track Changes**:
To track a file or folder, use `git add`:
```bash
git add <filename>
```
3. **Commit Changes**:
Commit the changes you made:
```bash
git commit -m "Your commit message"
```
4. **Push to GitHub**:
Push your local repository to GitHub:
```bash
git push origin main
```
Branching:
- Branches are used to work on separate features without affecting the main
codebase.
- To create a new branch:
```bash
git checkout -b feature-branch
```
- To switch between branches:
```bash
git checkout main
```
Merging:
- Merge changes from one branch to another:
```bash
git merge feature-branch
```
Resolving Merge Conflicts:
- Merge conflicts happen when two developers modify the same part of a file.
- Git will mark the conflicting lines, and you’ll need to manually resolve the
conflict.
- After resolving, mark the conflict as resolved:
```bash
git add <filename>
git commit
```
GitHub Workflow:
1. Fork a repository to create your copy of a project.
2. Clone it to your local machine:
```bash
git clone <repo-url>
```
3. After making changes, push them to GitHub:
```bash
git push origin main
```
4. Submit a pull request for your changes to be reviewed.
Best Practices:
- Write clear commit messages that explain what was changed and why.
- Regularly push your changes to the remote repository to avoid conflicts.
- Use branches for new features and bug fixes, keeping `main` stable.
Conclusion:
Git and GitHub are essential tools for version control and collaboration in modern
software development. Knowing how to use them efficiently will make your workflow
smoother and more productive.