Why version control matters
Version control systems let you track changes to files, revert to previous states, collaborate safely, and maintain a history of your project. For students, version control provides a safety net for experiments, a record of progress for assignments, and a professional workflow that employers expect.
Git basics: commits, branches, and history
Git stores snapshots of your repository as commits. Each commit has a message, author, timestamp, and a pointer to its parent(s). Learn a few core commands and the concepts they represent.
Initialize and make your first commit
# create repository
git init
# stage files and commit
git add .
git commit -m "Initial commit"
View history
git log --oneline --graph --decorate --all
This command shows a compact, visual history with branches and tags.
Branches
Branches are lightweight pointers to commits. Use branches to work on features without affecting the main codebase.
# create and switch to a branch
git checkout -b feature/login
# list branches
git branch
Commit frequently with clear messages — each commit should represent a small, testable change.
Remote repositories & GitHub
Remote hosting platforms (GitHub, GitLab, Bitbucket) store your repository online, enable collaboration, and provide issue tracking and CI/CD integration. To connect a local repo to GitHub:
# create repo on GitHub, then connect locally
git remote add origin [email protected]:username/repo.git
git push -u origin main
Use SSH keys for secure authentication. GitHub also supports HTTPS with token-based authentication.
Branching strategies & workflows
Choose a workflow that matches your project and team. A few common ones:
GitHub Flow (simple)
- main branch always deployable
- create short-lived feature branches
- open pull requests and merge after review
Git Flow (release-based)
- main and develop branches
- feature, release, and hotfix branches
- useful for teams with formal release cycles
Trunk-based Development
Developers integrate small, frequent changes directly into trunk (main) often behind feature flags. This emphasizes CI and fast feedback.
For most student projects and small teams, GitHub Flow is recommended for simplicity and clarity.
Pull requests & code review
Pull requests (PRs) are the mechanism for proposing changes and requesting reviews. A good PR is small, focused, and includes a clear description and testing instructions.
Creating a PR
# push a branch then open a PR on GitHub
git push origin feature/login
Checklist for high-quality PRs:
- Reference the issue or task it addresses
- Explain the intent and high-level approach
- List visible changes and any required setup
- Include tests or screenshots when relevant
Use code review to catch design and logic issues, suggest improvements, and share knowledge across the team.
Merge vs Rebase — what to choose
When integrating changes from one branch into another, you can merge or rebase. Each has different effects on history.
Merge
git merge creates a merge commit and preserves the branch history. It’s explicit and safe for shared branches.
git checkout main
git merge feature/login
Rebase
git rebase rewrites commits to appear as if they were created on top of another base. It produces a linear history but rewrites commit hashes (don’t rebase public/shared branches).
# rebase feature onto latest main
git checkout feature/login
git fetch origin
git rebase origin/main
# resolve conflicts, then
git rebase --continue
Recommended rule: merge public branches, rebase local, private branches if you prefer linear history.
Resolving merge conflicts
Conflicts occur when the same lines in the same files were changed differently. Git marks conflicts in the file — you must edit to reconcile and then complete the merge or rebase.
<<<<<<< HEAD
// your changes
=======
// incoming changes
>>>>>>> feature/login
Resolve by choosing or combining changes, then:
git add
git commit # if merging
git rebase --continue # if rebasing
Use tools like VS Code’s merge editor, git mergetool, or specialized merge GUIs for complex conflicts.
Best practices for students & teams
- Write clear commit messages: start with a short summary line (50 chars), add a blank line, then a longer description if needed.
- Commit small, logical units: one change = one commit.
- Use .gitignore: avoid committing build artifacts, credentials, or environment files.
- Protect main branch: require PR reviews and passing CI before merging.
- Keep branches short-lived: merge back frequently to avoid large conflicts.
- Use tags and releases: tag stable versions for reproducible builds.
# example .gitignore
__pycache__/
node_modules/
.env
.DS_Store
Troubleshooting common Git issues
1. I accidentally committed sensitive data
Remove it from history using git filter-repo or BFG Repo-Cleaner, then rotate the exposed credentials. Rewriting history requires a force-push and coordination with collaborators.
2. I need to undo the last commit but keep changes
git reset --soft HEAD~1
3. I lost commits after a rebase or reset
Use git reflog to find lost commit hashes and recover them.
git reflog
git checkout -b recover
4. Push rejected (non-fast-forward)
Your local branch is behind remote. Fetch and merge/rebase before pushing.
git fetch origin
git rebase origin/main
git push origin feature/login
FAQ
Q: Should I use Git GUI or CLI?
A: Learn the CLI first — it reveals the conceptual model of commits and refs. Use GUIs or IDE integrations to speed up everyday tasks once you understand the CLI.
Q: When should I rebase?
A: Rebase when you want a clean linear history and your branch is local/private. Avoid rebasing commits already pushed and shared without coordination.
Q: How many commits are too many?
A: Prefer many small, well-described commits to one large commit. Small commits are easier to review, revert, and understand.
Key takeaways & practice
- Use branches to isolate work. Keep commits small and messages clear.
- Adopt a simple workflow (GitHub Flow) for most projects.
- Prefer CLI to learn fundamentals, then adopt GUI tools as productivity aids.
- Practice resolving conflicts, using
git rebasesafely, and recovering from mistakes withgit reflog.
Practice challenge: Fork a small open-source repo, create a feature branch, add a small improvement or fix, submit a pull request, and iterate on reviews. This hands-on flow builds confidence for team work and interviews.