oliverdavies.uk/source/_daily_emails/2022-11-21.md

22 lines
1.6 KiB
Markdown
Raw Permalink Normal View History

2024-01-03 20:00:00 +00:00
---
title: >
2024-09-08 22:09:54 +00:00
Git tricks to avoid committing commented-out and other unneeded code
2024-01-03 20:00:00 +00:00
pubDate: 2022-11-21
permalink: >-
daily/2022/11/21/git-tricks-to-avoid-committing-commented-out-and-other-unneeded-code
2024-01-03 20:00:00 +00:00
tags:
2024-09-08 22:09:54 +00:00
- git
2024-01-03 20:00:00 +00:00
---
[Yesterday's email]({{site.url}}/daily/2022/11/20/version-controlled-commented-out-code) talked about whether commented-out code should be present if your code is version-controlled, but how do you avoid committing it in the first place?
2024-01-03 20:00:00 +00:00
You could make sure that you remove everything manually before you stage and commit your changes, or I like to use `git add --patch` (or `git add -p`) to interactively stage my changes, allowing me to select which parts of files I want to include in my commit and ignore anything else. The `--patch` option also works for other commands, including `checkout` and `reset`.
If you've already committed something like some debug code, you can use `git commit --amend` to amend the previous commit before pushing it, or if you have a separate clean-up commit, you can use `git rebase --interactive` and either the squash or fixup options to amend the original commit.
If some old code has been removed and you want to find it, you can use `git log -S` with a string to search for, and Git will show a list of commits where the specified string was changed.
If instead, you wanted to search for text within the commit message, you can use `git log --grep` with a string like an issue number to see a list of commits with that text in the commit message subject line or body.
I hope these tips help keep unwanted code out of your version-control repository.