Published on

Git Workflow

Author

Git provides a robust set of commands for managing and refining your codebase, making collaboration and version control more efficient. Here are some advanced techniques to enhance your Git workflow:

  • Amending Commits: If you need to modify the most recent commit—whether it's the message or the content—use:
  git commit --amend

This command allows you to adjust the last commit without creating a new one.

  • Fixup Commits for PR Reviews: For a cleaner PR review process, you can add a commit that specifically addresses changes needed from a previous commit using:
git commit --fixup HASH

Replace HASH with the commit ID you're correcting. This helps in organizing and consolidating changes during reviews.

  • Rebasing with Autosquash: To integrate and clean up your commit history, especially before merging a feature branch back to the main branch, use:
git rebase --autosquash -i main

This command rebases your current branch onto main, automatically squashing commits marked as fixups.

  • Safe Force Pushing with --force-with-lease: When you need to force push but want to ensure you're not overwriting others' work, this option offers a safer alternative:
--force-with-lease

It checks that the remote branch hasn't been updated before pushing your changes, reducing the risk of disrupting the work of others.

These commands are invaluable for maintaining a clean and efficient Git repository, ensuring that your project's history is both manageable and understandable.