Skip to content

Commit e50d785

Browse files
committed
wip
1 parent 9ae4ae6 commit e50d785

File tree

1 file changed

+231
-0
lines changed

1 file changed

+231
-0
lines changed
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# Curated examples from issues
2+
3+
Lots of people have filed issues against git-filter-repo, and many times it
4+
boils down into questions of "How do I?" or "Why doesn't this work?"
5+
6+
I thought I'd collect a bunch of these as example repository filterings
7+
that others may be interested in.
8+
9+
## Table of Contents
10+
11+
* [Adding files to root commits](#adding-files-to-root-commits)
12+
* [Purge a large list of files](#purge-a-large-list-of-files)
13+
14+
## Adding files to root commits
15+
16+
<!-- https://github.com/newren/git-filter-repo/issues/21 -->
17+
18+
Here's an example that will take `/path/to/existing/README.md` and
19+
store it as `README.md` in the repository, and take
20+
`/home/myusers/mymodule.gitignore` and store it as `src/.gitignore` in
21+
the repository:
22+
23+
```
24+
git filter-repo --commit-callback "if not commit.parents: commit.file_changes += [
25+
FileChange(b'M', b'README.md', b'$(git hash-object -w '/path/to/existing/README.md')', b'100644'),
26+
FileChange(b'M', b'src/.gitignore', b'$(git hash-object -w '/home/myusers/mymodule.gitignore')', b'100644')]"
27+
```
28+
29+
Alternatively, you could also use the [insert-beginning contrib script](../contrib/filter-repo-demos/insert-beginning).
30+
31+
## Purge a large list of files
32+
33+
<!-- https://github.com/newren/git-filter-repo/issues/63 -->
34+
35+
Stick all the files in some file (one per line),
36+
e.g. ../DELETED_FILENAMES.txt, and then run
37+
38+
```
39+
git filter-repo --invert-paths --paths-from-file ../DELETED_FILENAMES.txt
40+
```
41+
42+
## Extracting a libary to a separate repo
43+
44+
<!-- https://github.com/newren/git-filter-repo/issues/80 -->
45+
46+
```
47+
git filter-repo \
48+
--path src/some-folder/some-feature \
49+
--path-rename src/some-folder/some-feature/:src/
50+
```
51+
52+
## Replace words in all commit messages
53+
54+
<!-- https://github.com/newren/git-filter-repo/issues/83 -->
55+
56+
```
57+
git-filter-repo --message-callback 'return message.replace(b"stuff", b"task")'
58+
```
59+
60+
## Only keep files from two branches
61+
62+
<!-- https://github.com/newren/git-filter-repo/issues/91 -->
63+
64+
Let's say you know that the files currently present on two branches
65+
are the only files that matter. Files that used to exist in either of
66+
these branches, or files that only exist on some other branch, should
67+
all be deleted from all versions of history. This can be accomplished
68+
by getting a list of files from each branch, combining them, sorting
69+
the list and picking out just the unique entries, then passing to
70+
`--paths-from-file`:
71+
72+
```
73+
git ls-tree -r ${BRANCH1} >../my-files
74+
git ls-tree -r ${BRANCH2} >>../my-files
75+
sort ../my-files | uniq >../my-relevant-files
76+
git filter-repo --paths-from-file ../my-relevant-files
77+
```
78+
79+
## Renormalize end-of-line characters and add a .gitattributes
80+
81+
<!-- https://github.com/newren/git-filter-repo/issues/122 -->
82+
83+
```
84+
contrib/filter-repo-demos/lint-history dos2unix
85+
[edit .gitattributes]
86+
contrib/filter-repo-demos/insert-beginning .gitattributes
87+
```
88+
89+
## Remove spaces at the end of lines
90+
91+
<!-- https://github.com/newren/git-filter-repo/issues/145 -->
92+
93+
Removing all spaces at the end of lines of non-binary files, including
94+
stripping trailing carriage returns:
95+
96+
```
97+
git filter-repo --replace-text <(echo 'regex:[\r\t ]+(\n|$)==>\n')
98+
```
99+
100+
## Having both exclude and include rules for filenames
101+
102+
<!-- https://github.com/newren/git-filter-repo/issues/230 -->
103+
104+
If you want to have rules to both include and exclude filenames, you
105+
can simply invoke `git filter-repo` multiple times. Alternatively,
106+
you can dispense with `--path` arguments and instead use the more
107+
generic `--filename-callback`. For example to include all files under
108+
`src/` except for `src/README.md`:
109+
110+
```
111+
git filter-repo --filename-callback '
112+
if filename == b"src/README.md":
113+
return None
114+
if filename.startswith(b"src/"):
115+
return filename
116+
return None'
117+
```
118+
119+
## Removing paths with a certain extension
120+
121+
<!-- https://github.com/newren/git-filter-repo/issues/274 -->
122+
123+
```
124+
git filter-repo --invert-paths --path-glob '*.xsa'
125+
```
126+
127+
or
128+
129+
```
130+
git filter-repo --filename-callback '
131+
if filename.endswith(b".xsa"):
132+
return None
133+
return filename'
134+
```
135+
136+
## Removing a directory
137+
138+
<!-- https://github.com/newren/git-filter-repo/issues/278 -->
139+
140+
```
141+
git filter-repo --path node_modules/electron/dist/ --invert-paths
142+
```
143+
144+
## Convert from NFD filenames to NFC
145+
146+
<!-- https://github.com/newren/git-filter-repo/issues/296 -->
147+
148+
Given that Mac does utf-8 normalization of filenames, and has
149+
historically switched which kind of normalization it does, users may
150+
have committed files with alternative normalizations to their
151+
repository. If someone wants to convert filenames in NFD form to NFC,
152+
they could run
153+
154+
```
155+
git filter-repo --filename-callback '
156+
try:
157+
return subprocess.check_output("iconv -f utf-8-mac -t utf-8".split(),
158+
input=filename)
159+
except:
160+
return filename
161+
'
162+
```
163+
164+
or
165+
166+
```
167+
git filter-repo --filename-callback '
168+
import unicodedata
169+
try:
170+
return bytearray(unicodedata.normalize('NFC', filename.decode('utf-8')), 'utf-8')
171+
except:
172+
return filename
173+
'
174+
```
175+
176+
## Set the committer of the last few commits to myself
177+
178+
<!-- https://github.com/newren/git-filter-repo/issues/379 -->
179+
180+
```
181+
git filter-repo --refs main~5..main --commit-callback '
182+
commit.commiter_name = b"My Wonderful Self"
183+
commit.committer_email = b"[email protected]"
184+
'
185+
186+
## Handling special characters, e.g. accents in names
187+
188+
<!-- https://github.com/newren/git-filter-repo/issues/383 -->
189+
190+
Since characters like ë and á are multi-byte characters and python
191+
won't allow you to directly place those in a bytestring
192+
(e.g. b"Raphaël González"), you just need to make a normal string and
193+
then convert to a bytestring to handle these. For example, changing
194+
the author name and email where the author email is currently
195+
196+
197+
```
198+
git filter-repo --refs main~5..main --commit-callback '
199+
if commit.author_email = b"[email protected]":
200+
commit.author_name = "Raphaël González".encode()
201+
commit.author_email = b"[email protected]"
202+
'
203+
```
204+
205+
<!-- https://github.com/newren/git-filter-repo/issues/420 -->
206+
handling repository corruption (old original objects are corrupt)
207+
208+
<!-- https://github.com/newren/git-filter-repo/issues/427 -->
209+
removing all files with a backslash in them (final example is best)
210+
211+
<!-- https://github.com/newren/git-filter-repo/issues/436 -->
212+
replace a binary blob in history
213+
214+
<!-- https://github.com/newren/git-filter-repo/pull/542 -->
215+
callback for lint-history
216+
217+
<!-- https://github.com/newren/git-filter-repo/issues/300 -->
218+
using replace refs to delete old history
219+
220+
<!-- https://github.com/newren/git-filter-repo/issues/492 -->
221+
replacing pngs with compressed alternative
222+
(#537 also used a change.blob_id thingy)
223+
224+
<!-- https://github.com/newren/git-filter-repo/issues/490 -->
225+
<!-- https://github.com/newren/git-filter-repo/issues/504 -->
226+
need for a multi-step filtering to avoid path collisions or ordering issues
227+
228+
<!-- https://lore.kernel.org/git/CABPp-BFqbiS8xsbLouNB41QTc5p0hEOy-EoV0Sjnp=xJEShkTw@mail.gmail.com/ -->
229+
Two things:
230+
textwrap.dedent
231+
easier example of using git-filter-repo as a library

0 commit comments

Comments
 (0)