Git and GitHub are essential tools for modern software development. This guide will help you get started with both tools and establish an efficient workflow.
1. Install Git
First, download and install Git from git-scm.com.
2. Configure Git
Set up your identity with the following commands:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
3. Create a New Repository
To initialize a new Git repository in your project folder:
git init
4. Add and Commit Files
Stage your files for commit and create a commit with a message:
git add .
git commit -m "Initial commit"
5. Push to GitHub
Create a repository on GitHub, then connect your local repository and push your code:
git remote add origin https://github.com/yourusername/repository.git
git push -u origin main
6. Clone an Existing Repository
To download a repository from GitHub:
git clone https://github.com/username/repository.git
7. Create a Branch and Merge
Work on features in separate branches and merge them when ready:
git branch new-feature
git checkout new-feature
# Make changes and commit them
git checkout main
git merge new-feature
8. Pull Changes from GitHub
Update your local repository with changes from GitHub:
git pull origin main
9. Resolve Merge Conflicts
When Git can't automatically merge changes, you'll need to resolve conflicts manually by editing the conflicted files, then:
git add .
git commit -m "Resolved merge conflicts"
10. GitHub Pull Requests
For collaborative projects, create a pull request on GitHub to propose changes to the original repository:
- Fork the repository on GitHub
- Clone your fork locally
- Create a branch for your changes
- Push your branch to your fork
- Create a pull request on GitHub
Conclusion
Git and GitHub provide a powerful system for version control and collaboration. With these basic commands and workflows, you can start managing your projects effectively and contribute to open-source projects. As you become more comfortable, explore advanced features like rebasing, stashing, and GitHub Actions to further enhance your development process.