How to Use Version Control with Git and GitHub: From First Commit to Pull Request
How to Use Version Control with Git and GitHub: From First Commit to Pull Request
Master the essential workflow for managing code changes and collaborating with teams using a professional Gitflow approach. This guide takes you from local initialization to a successful remote merge.
What You'll Need
- Git installed locally
- A GitHub account
- A code editor (e.g., VS Code)
Steps
Step 1: Initialize and First Commit
Navigate to your project folder in the terminal and run 'git init' to create a local repository. Stage your files using 'git add .' and record your first snapshot with 'git commit -m "Initial commit"' to establish a project baseline.
Step 2: Connect to GitHub
Create a new repository on GitHub and copy the remote URL. Link your local project to the cloud by running 'git remote add origin [URL]' and push your main branch using 'git push -u origin main'.
Step 3: Create a Feature Branch
Avoid working directly on the main branch to keep the production code stable. Use 'git checkout -b feature-name' to create and switch to a new branch dedicated to a specific task or bug fix.
Step 4: Iterative Development
Make your code changes and frequently stage them with 'git add'. Commit these changes with descriptive, imperative messages like 'git commit -m "Add user authentication logic"' to maintain a clear project history.
Step 5: Sync with Main
Before submitting your work, pull the latest changes from the main branch to ensure compatibility. Use 'git checkout main', then 'git pull origin main', and finally merge those updates back into your feature branch.
Step 6: Resolve Merge Conflicts
If Git flags a conflict, open the affected files to manually choose which code blocks to keep. Once resolved, stage the fixed files and complete the merge with a final commit.
Step 7: Push and Open Pull Request
Upload your feature branch to GitHub using 'git push origin feature-name'. Navigate to the repository on GitHub and click 'Compare & pull request' to invite team members to review your code.
Step 8: Merge and Cleanup
Once the pull request is approved, merge the branch into the main line via the GitHub interface. Delete the remote feature branch and run 'git branch -d feature-name' locally to keep your workspace clean.
Expert Tips
- Write atomic commits: keep each commit focused on a single logical change for easier debugging.
- Use .gitignore files to prevent sensitive data or dependency folders like node_modules from being uploaded.
- Always pull the latest changes before starting a new branch to avoid massive merge conflicts later.
- Follow a consistent naming convention for branches, such as 'feature/', 'hotfix/', or 'bugfix/'.
See also
- How to Start Learning Programming for Beginners: A 2024 Roadmap
- Best Practices for Clean Code in 2024: The Definitive Standard
- How to Optimize Software Performance: A Systemic Approach
- Which Programming Language is Best for Web Development?