Why Use Git?
Git provides powerful capabilities for managing code:- Version Control: Track every change made to your codebase
- Collaboration: Work with teams without conflicts
- Branching: Experiment with features in isolated environments
- History: Review and revert changes when needed
- Backup: Distributed nature means multiple copies of your code
Getting Started with Git
To use Git effectively, you’ll need to install it and configure authentication for remote repositories like GitHub.Install Git on Ubuntu
Install Git and configure your identity for version control
Generate SSH Key for GitHub
Set up SSH authentication to securely connect to GitHub
Basic Git Workflow
Creating a Repository
Start tracking a project with Git:Making Changes
The standard workflow for making changes:Viewing History
Review your project’s history:Branching and Merging
Branches allow you to work on features independently:Working with Remote Repositories
Syncing Changes
Keep your local repository in sync with remote:Managing Remotes
Work with multiple remote repositories:GitHub Workflows
SSH Authentication
Using SSH keys provides secure, password-free authentication to GitHub. After generating and adding your SSH key to GitHub, you can:- Clone repositories using SSH URLs
- Push and pull without entering passwords
- Securely authenticate with GitHub
Pull Requests
Collaborate with teams using pull requests:- Create a feature branch
- Make and commit your changes
- Push the branch to GitHub
- Open a pull request on GitHub
- Review and discuss changes
- Merge when approved
Best Practices
Commit Messages
- Write clear, descriptive commit messages
- Use present tense (“Add feature” not “Added feature”)
- Keep the first line under 50 characters
- Add detailed description if needed
Branching Strategy
- Keep
mainbranch stable and deployable - Create feature branches for new work
- Use descriptive branch names (e.g.,
feature/user-authentication) - Delete branches after merging
Regular Commits
- Commit logical units of work
- Commit early and often
- Don’t commit sensitive information (passwords, API keys)
- Use
.gitignoreto exclude unnecessary files
Common Operations
Undoing Changes
Stashing Changes
Tagging Releases
Troubleshooting
Merge Conflicts
When Git can’t automatically merge changes:- Git marks conflicted files
- Open files and resolve conflicts manually
- Remove conflict markers (
<<<<<<<,=======,>>>>>>>) - Stage resolved files with
git add - Complete merge with
git commit
Reset vs Revert
git reset: Moves branch pointer (rewrites history)git revert: Creates new commit that undoes changes (preserves history)
revert for public branches, reset for local changes.
Next Steps
Once you’re comfortable with Git basics, explore:- Advanced branching strategies (Git Flow, GitHub Flow)
- Rebasing for cleaner history
- Git hooks for automation
- Submodules for managing dependencies
- Git LFS for large files
Related Resources
Docker Guide
Version control your Dockerfiles and container configurations
Node.js Guide
Manage Node.js project dependencies and configurations with Git