git cleanup

This commit is contained in:
2026-04-20 05:33:50 +02:00
parent c8acd558a6
commit 4a352f89ef
9 changed files with 160 additions and 265 deletions

View File

@@ -1,202 +0,0 @@
---
title: "git"
tags: [ "data" ]
---
# Starting
## New Machines
```sh
git config --global user.email "$YOUR_EMAIL"
```
```sh
git config --global user.name "$YOUR_NAME"
```
# New Git
Decide on algorithm:
- If you're scared of insecure hash-sums, go with `hash=sha256`.
- If you don't know what a hash sum is, go with `hash=sha1`.
## Init the Git
Start a git in directory `${DIR}`:
```sh
git init --object-format=${hash} ${DIR}
cd ${DIR}
```
Make a file explaining what the project does, and tell `git` to track it:
```sh
echo "I hereby solemnly swear never to commit a binary file." > README.md
git add README.md
```
Then make the initial commit, explaining the change you just made:
```sh
git commit
```
# Working
Once you make a change to some file, add it and make a commit explaining it.
```sh
git add $FILE
```
```sh
git commit -m"change $FILE"
```
Check your history:
```sh
git log
```
# Remotes
If you want to keep a copy on a public site such as Gitlab, so others can see it, then go there and create a blank project (no readme, nothing).
Give it the same name as the `$DIR` directory, above.
Add this as a remote:
```sh
REMOTE=gitlab
git remote add $REMOTE https://gitlab.com/$USERNAME/$DIR
```
Tell git you're pushing the branch "master" to the remote repo "origin":
```sh
git push -u master origin
```
If someone makes a change on the remote, pull it down with:
```sh
git pull
```
# Branches
A branch is a full copy of the project to test additional ideas.
You can make a new branch called 'featurez' like this:
```sh
git branch $FEATURE_BRANCH
```
Have a look at all your branches:
```sh
git branch
```
Switch to your new branch:
```sh
git checkout $FEATURE_BRANCH
```
And if your changes are rubbish, checkout the "master" branch again, then delete "featurez":
```sh
git branch -D $FEATURE_BRANCH
```
Or if it's a good branch, push it to the remote:
```sh
remote=origin
git push $remote $FEATURE_BRANCH
```
## Merging
Once you like the feature, merge it into the main branch. Switch to master then merge it:
```sh
git merge $FEATURE_BRANCH
```
And delete the branch, as you've already merged it:
```sh
git branch -d $FEATURE_BRANCH
```
# Subtree
## Pulling another git repo into a subtree
```sh
git subtree add -P config git@gitlab.com:bindrpg/config.git master
```
# Tricks
## Delete All History
```sh
git checkout --orphan temp
```
```sh
git add -A
```
```sh
git commit -am "release the commits!"
```
```sh
git branch -D master
```
```sh
git branch -m master
```
```sh
git push -f origin master
```
Gitlab requires more changes, such as going to `settings > repository` and switching the main branch, then stripping protection.
## Clean up Bloated Repo
```sh
git fsck --full
```
```sh
git gc --prune=now --aggressive
```
```sh
git repack
```
## Find Binary Blobs
```sh
git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| sed -n 's/^blob //p' \
| sort --numeric-sort --key=2 \
| cut -c 1-12,41- \
| $(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest
```
# More
For big binary files (like images), see [git large-file-storage](git-lfs.md)

19
data/git/cleanup.md Normal file
View File

@@ -0,0 +1,19 @@
---
title: "Clean up a bloated git repo"
tags: [ "data", "setup" ]
requires: [ "git" ]
---
```sh
git fsck --full
```
```sh
git gc --prune=now --aggressive
```
```sh
git repack
```

View File

@@ -1,6 +1,7 @@
---
title: "Commit for Another"
tags: [ "data", "git" ]
requires: [ "git" ]
---
You can make Alice the author, while you are still the commiter:

View File

@@ -1,6 +1,6 @@
---
title: "git-secret"
tags: [ "data", "git" ]
tags: [ "data", "git", "review" ]
---
This utility is largely useless, as it can only identify people by their email.

View File

@@ -6,7 +6,7 @@ tags: [ "data", "git" ]
Check out the sample hooks:
```sh
cd $GIT_REPO
cd ${git_repo}
ls .git/hooks
head .git/hooks/pre-commit.sample
```
@@ -21,7 +21,9 @@ chmod u+x .git/hooks/commit-msg
## Committing
Your `git hooks` will not enter the repository, but you can commit them to a repository, then request others add these git hooks to their own branch, by putting a note in the project's `README.md`.
The `git hooks` will not work on other people who use the repository, but you
can commit them to a repository, then request others add these git hooks to
their own branch, by putting a note in the project's `README.md`.
```markdown
The project comes with recommended git hooks.

View File

@@ -1,34 +0,0 @@
---
title: "git subtree"
tags: [ "data", "git", "subtree" ]
---
## Pulling a Subtree from an existing git
The project has subdirectories `sub-1`, `sub-2`, `sub-3`.
The first should be its own repository, but should also retain its own history.
First, we extract its history as an independent item, and make that into a seprate branch.
```sh
git subtree split --prefix=sub-1 -b sub
```
If you want something a few directories deep, you can use `--prefix=sub-1/dir-2/dir-3
Then go and create a new git somewhere else:
```sh
cd ..;mkdir sub-1;cd sub-1;git init --bare
```
Then go back to your initial git repo, and do the following:
git push ../subtest sub:master
Finally, you can clone this repo from your original.
```sh
git clone ../subtest
```