0

I had a JSX component named AccountNode, and accidentally created a TS interface also named AccountNode, but the two are distinct. One is for a row in a table (ui), and the other is for a piece of data in a tree (a node).

How can I make the "AccountNode" name not appear anywhere in history?

Similarly, as mentioned in the comments, you may have a bad string constant bring passed around, a so-called "magic string". It may be hard to rewrite code to not use this magic string, but this git rebase recipe will help.

Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
  • Is this not the same as https://stackoverflow.com/q/73955372/112968? – knittl Jan 14 '23 at 10:30
  • No they are distinct. One is purely focused on doing a completely automated rename, and another helps you manually eliminate a string. Renaming is stylistic, where as eliminating a string could likely break your program. – Devin Rhode Jan 15 '23 at 02:17
  • In my case I needed to eliminate a "magic string" which was used likely due to not using typescript. (With typescript it's easy to forward a new parameter through any number of function calls, whereas without typescript, magic strings are more prevalent, because it's rather hard to add a new parameter to pass through N function calls) – Devin Rhode Jan 15 '23 at 02:19
  • I.e. The string "load_more" is not really a valid node id, it's just a hack. I needed the go through and rewrite code to not use this hack. – Devin Rhode Jan 15 '23 at 02:22

1 Answers1

0

Example, I don't want to hard-code the string "load_more" so am going to redo/tweak those commits:

git rebase --exec 'git difflastcommitadded | ag load_more && { echo "found load_more"; git redo; } || echo "ok"' --no-reschedule-failed-exec -i 315abbd5b

Setup:

  1. Install ag (brew install the_silver_searcher) (grep/ack alternative)
  2. Add this git difflastcommitadded alias:
git config --global --edit
brew install the_silver_searcher

Paste:

[alias]
  # From: https://stackoverflow.com/questions/73981158/git-rebase-make-bad-name-in-large-pr-stack-disappear/73981159#73981159
  difflastcommitadded = !git diff HEAD^..HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep '^\\+'

Alternatively, if you want to completely abolish a variable name from a folder in your repo, continue reading this less flexible example:

git rebase --exec 'ag -0 -l AccountNode ui/app && git redo || echo "ok"' -i 315abbd5b

This will rebase, but stop whenever there's a commit that introduced AccountNode inside the ui/app folder, undo that commit, and pop those changes into your working directory, so you can fix that in your ide.

Once you fix an instance of AccountNode you can proceed with nice safety checks using this script:

ag -0 -l AccountNode ui/app/pages/AccountManagement2 && echo "still contains AccountNode" || {
  yarn --cwd ui run jest -i && git add . && git commit && git rebase --continue
}

In my instance, git commit runs prettier+eslint, but not tests (those run in ci right now)

This depends on the git redo alias mentioned here: git rebase - pop each commit into working dir to easily edit, re-use commit msg

Devin Rhode
  • 23,026
  • 8
  • 58
  • 72