skip to content
Risqi Romadhoni

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.
Terminal window
# Switch to your feature branch
git checkout feature-branch
# Rebase from the main branch
git rebase main

If conflicts occur during rebase, resolve them and continue:

Terminal window
git rebase --continue

To abort a rebase:

Terminal window
git rebase --abort

2. Cherry-Picking Commits

Cherry-picking allows you to apply specific commits from one branch to another.

Terminal window
git checkout main
git 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.

Terminal window
git rebase -i HEAD~5

In 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.

Terminal window
git stash

To apply the latest stash:

Terminal window
git stash apply

To apply and remove the stash:

Terminal window
git stash pop

To view all stashed changes:

Terminal window
git stash list

5. Undoing Changes

Reset a Commit

Soft reset (keeps changes in the staging area):

Terminal window
git reset --soft HEAD~1

Hard reset (removes changes permanently):

Terminal window
git reset --hard HEAD~1

Revert a Commit

Revert keeps the commit history intact by creating a new commit to undo the previous one:

Terminal window
git revert <commit-hash>

6. Working with Tags

Tags help mark important commits, such as release versions.

Creating a Tag

Terminal window
git tag -a v1.0 -m "Version 1.0 release"

Push a tag to the remote repository:

Terminal window
git push origin v1.0

List all tags:

Terminal window
git tag

Deleting a Tag

Terminal window
git tag -d v1.0

Remove a remote tag:

Terminal window
git push --delete origin v1.0

7. Bisect for Debugging

Git Bisect helps find the commit that introduced a bug.

Terminal window
git bisect start
git bisect bad # Mark the current commit as bad
git bisect good <commit-hash> # Mark a known good commit

Git will check out a commit in between. Test your code and mark it good or bad:

Terminal window
git bisect good # or
git bisect bad

Once found, reset bisect:

Terminal window
git bisect reset

8. 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.

Terminal window
nano .git/hooks/pre-commit

Add the following script:

#!/bin/sh
npm run lint
if [ $? -ne 0 ]; then
echo "Linting failed. Commit aborted."
exit 1
fi

Save and make it executable:

Terminal window
chmod +x .git/hooks/pre-commit

Using Husky for Git Hooks

Husky is a tool that simplifies Git hooks in Node.js projects.

Install Husky:

Terminal window
npm install husky --save-dev

Enable Husky in your project:

Terminal window
npx husky install

Add a pre-commit hook:

Terminal window
npx husky add .husky/pre-commit "npm run lint"

Ensure the hook is executable:

Terminal window
chmod +x .husky/pre-commit

Enforcing Commit Message Conventions with Commitlint

Commitlint helps ensure commit messages follow a standard format.

Install Commitlint:

Terminal window
npm install --save-dev @commitlint/{config-conventional,cli}

Create a configuration file:

Terminal window
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

Add a commit-msg hook with Husky:

Terminal window
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"

Ensure the hook is executable:

Terminal window
chmod +x .husky/commit-msg

Now, 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.

Terminal window
git lfs install
git lfs track "*.psd"

Commit and push as usual.

10. Cleaning Up Unused Data

Terminal window
git gc --prune=now

Removes 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!