Here is a random (and definitely incomplete) collection of git commands, mostly for my own reference, as I keep looking them up over and over again when I need them.
1. To switch temporarily to an old commit, use
git checkout <commit-id>
To return to the head, do
git checkout master
2. To restore a specific file that may have ben deleted, from the upstream repo:
git checkout origin/master -- <path/to/file>
3. To see what a specific commit did:
git show <commit-id>
4. To see the history of a specific file:
git log -p -- <filename>
5. Revert a specific commit (not tested lately):
git revert --no-commit <commid-id>
This reverts all files affected by the specified commit. If there was a file we'd rather NOT revert, we can discard the revert by
git checkout <filename>
The --no-commit option ensured that the revert was not committed yet, so we can do our own edits if needed, followed by, e.g.,
git commit -a -m "Revert commit ..."
Retroactive changes
Here is a not so uncommon problem: You have a project. It includes a configuration file that contains your private key to a service. You wish to share the project via a public repo. You don't want to erase the project history, but you must change that file, retroactively, to ensure that you do not leak secure information.
The goal: change the file name (e.g., config.py to config.py.dist) and change the file content (replace the actual key with a dummy string.)
Here's one way to do this.
1. Make sure you have git-filter-repo installed
python3 -m pip install --user git-filter-repo
2. Create a clone of your project
git clone OLD-PATH NEW-PATH
cd NEW-PATH
3. Create a file that includes the old string and its replacement
echo 'OLD_STRING==>NEW_STRING' > ../repl.txt
4. Finally, run filter-repo
git filter-repo --path-rename path/to/OLD-FILE:path/to/NEW-FILE --replace-text ../repl.txt
Note that the file paths are relative to the repo root directory.
