Mastering Git for Efficient Development
/ 3 min read
Introduction
As you become more comfortable with Git, you’ll need to leverage its advanced features to streamline your workflow, optimize collaboration, and manage complex projects efficiently. This guide covers some of Git’s powerful commands and techniques.
1. Working with Rebase
Rebasing vs. Merging
Rebasing allows you to integrate changes from one branch into another more cleanly compared to merging.
- Merge: Creates a new commit to join branches.
- Rebase: Moves the base of your branch to a new commit.
# Switch to your feature branchgit checkout feature-branch
# Rebase from the main branchgit rebase mainIf conflicts occur during rebase, resolve them and continue:
git rebase --continueTo abort a rebase:
git rebase --abort2. Cherry-Picking Commits
Cherry-picking allows you to apply specific commits from one branch to another.
git checkout maingit cherry-pick <commit-hash>This is useful when you want to bring only select changes into a branch without merging everything.
3. Interactive Rebase
Interactive rebasing helps you clean up your commit history by squashing, reordering, or editing commits.
git rebase -i HEAD~5In the interactive editor, replace pick with:
squash (s): Merge commit with the previous one.edit (e): Modify commit message or content.reword (r): Change commit message.
4. Stashing Changes for Later
If you need to switch branches but don’t want to commit yet, stash your changes.
git stashTo apply the latest stash:
git stash applyTo apply and remove the stash:
git stash popTo view all stashed changes:
git stash list5. Undoing Changes
Reset a Commit
Soft reset (keeps changes in the staging area):
git reset --soft HEAD~1Hard reset (removes changes permanently):
git reset --hard HEAD~1Revert a Commit
Revert keeps the commit history intact by creating a new commit to undo the previous one:
git revert <commit-hash>6. Working with Tags
Tags help mark important commits, such as release versions.
Creating a Tag
git tag -a v1.0 -m "Version 1.0 release"Push a tag to the remote repository:
git push origin v1.0List all tags:
git tagDeleting a Tag
git tag -d v1.0Remove a remote tag:
git push --delete origin v1.07. Bisect for Debugging
Git Bisect helps find the commit that introduced a bug.
git bisect startgit bisect bad # Mark the current commit as badgit bisect good <commit-hash> # Mark a known good commitGit will check out a commit in between. Test your code and mark it good or bad:
git bisect good # orgit bisect badOnce found, reset bisect:
git bisect reset8. Git Hooks
Hooks allow you to run scripts before or after Git events (e.g., pre-commit, pre-push).
Example: Create a pre-commit hook to check for linting issues.
nano .git/hooks/pre-commitAdd the following script:
#!/bin/shnpm run lintif [ $? -ne 0 ]; then echo "Linting failed. Commit aborted." exit 1fiSave and make it executable:
chmod +x .git/hooks/pre-commitUsing Husky for Git Hooks
Husky is a tool that simplifies Git hooks in Node.js projects.
Install Husky:
npm install husky --save-devEnable Husky in your project:
npx husky installAdd a pre-commit hook:
npx husky add .husky/pre-commit "npm run lint"Ensure the hook is executable:
chmod +x .husky/pre-commitEnforcing Commit Message Conventions with Commitlint
Commitlint helps ensure commit messages follow a standard format.
Install Commitlint:
npm install --save-dev @commitlint/{config-conventional,cli}Create a configuration file:
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.jsAdd a commit-msg hook with Husky:
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"Ensure the hook is executable:
chmod +x .husky/commit-msgNow, commits that don’t follow the conventional format will be rejected.
9. Managing Large Repositories with Git LFS
Git LFS (Large File Storage) helps manage large files efficiently.
git lfs installgit lfs track "*.psd"Commit and push as usual.
10. Cleaning Up Unused Data
git gc --prune=nowRemoves unnecessary files and optimizes the local repository.
Conclusion
Mastering these advanced Git techniques will improve your workflow, efficiency, and collaboration. Keep practicing and refining your Git skills to become a power user!
