From Init to Rebase: A Developer’s Guide to Smart Git Practices
Git is a distributed version control system that lets multiple developers collaborate on a codebase efficiently. Whether you're just starting out or managing complex branching strategies with multipl
From Init to Rebase: A Developer’s Guide to Smart Git Practices
Git is a distributed version control system that lets multiple developers collaborate on a codebase efficiently.
Whether you're just starting out or managing complex branching strategies with multiple pull requests, understanding Git’s core and extended capabilities is essential.
Let’s dive into a complete Git workflow—repo setup, branching strategies, pull requests, and everything in between.
Setting Up Git
Set your Git identity (only once per machine):
• git config --global user.name "Your Name"
• git config --global user.email "you@example.com"
Create or Clone a Repository
To start a new project:
• mkdir my-project
Push a branch:
• git push origin branch-a
Raising a Pull Request
1. Push your branch to the remote.
2. Go to the repository on GitHub.
3. Click "Compare & pull request".
4. Add title, description, and reviewers.
5. Submit the PR.
Merging a Branch
• git checkout main
• git pull origin main
• git merge branch-a
• git push origin main
Resolving Merge Conflicts
1. Open the conflicting file.
2. Look for conflict markers:
Example:
<<<<<<< HEAD
your code
=======
incoming code
>>>>>>> branch-a
3. Edit and clean up the conflicts.
4. Then run:
◦ git add .
◦ git commit
Keeping Feature Branches Updated
Use rebase to apply your changes on top of the latest main, when you want a clean, linear commit history.:
• git checkout branch-a
• git pull origin main --rebase
Managing Multiple Pull Requests
For each new feature or fix:
• git checkout main
• git pull origin main
• git checkout -b feature-a
• Commit and push changes
• Repeat for feature-b, feature-c, etc.
Always branch off main, not other feature branches.
Advanced Git Commands and When to Use Them
git stash
Temporarily save uncommitted changes:
• git stash
• git stash pop
Use when switching branches without committing.
git cherry-pick
Copy a specific commit to your current branch:
• git cherry-pick <commit-hash>
Use when you need a fix or change from another branch.
git revert
Undo a commit (without rewriting history):
• git revert <commit-hash>
Use when the commit is already pushed and needs to be safely undone.
git reset
Move the current branch back to a previous commit:
• git reset --soft <commit> – Keeps changes staged
• git reset --hard <commit> – Discards all changes
Use before pushing to clean up commits.
git rebase -i (interactive)
Drop or rearrange commits:
• git rebase -i HEAD~3
You'll see:
pick abc123 First change
pick def456 Second change
pick ghi789 Debug logs
Change pick to drop or rearrange lines.
Use before pushing to keep your commit history clean.
Clean Up Merged Branches
After merging:
• git branch -d branch-a – Delete local branch
• git push origin --delete branch-a – Delete remote branch
Final Thoughts
Git isn't just about version control — it’s about workflow control.
Learn the commands, understand the flow, and you'll never lose your code or your sanity again.
No comments yet. Login to start a new discussion Start a new discussion