lk/data/git/basics.md

204 lines
3.0 KiB
Markdown
Raw Permalink Normal View History

2022-01-16 18:20:39 +00:00
---
title: "git"
tags: [ "Documentation", "data" ]
---
2020-01-02 03:12:18 +00:00
# Starting
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
## New Machines
2020-01-02 00:04:35 +00:00
```bash
git config --global user.email "$YOUR_EMAIL"
```
2020-01-02 00:04:35 +00:00
```bash
git config --global user.name "$YOUR_NAME"
```
2020-01-02 00:04:35 +00:00
2022-01-26 22:35:07 +00:00
# New Git
2020-01-02 00:04:35 +00:00
Start a git in directory `$DIR`:
2022-02-03 19:01:44 +00:00
```bash
mkdir $DIR && cd $DIR
```
2020-01-02 00:04:35 +00:00
```bash
git init
```
2020-01-02 00:04:35 +00:00
2022-02-03 19:01:44 +00:00
Make a file explaining what the project does:
```bash
vim README.md
```
Add this to the git:
2020-01-02 00:04:35 +00:00
```bash
git add README.md
```
2020-01-02 00:04:35 +00:00
2022-02-03 19:01:44 +00:00
Then make the initial commit, explaining the change you just made:
2020-01-02 00:04:35 +00:00
```bash
git commit
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
# Working
2020-01-02 00:04:35 +00:00
Once you make a change to some file, add it and make a commit explaining it.
2020-01-02 00:04:35 +00:00
```bash
git add $FILE
```
2020-01-02 00:04:35 +00:00
```bash
git commit -m"change $FILE"
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
Check your history:
2020-01-02 00:04:35 +00:00
```bash
git log
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
# Remotes
2020-01-02 00:04:35 +00:00
2022-02-03 19:01:44 +00:00
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:
2020-01-02 00:04:35 +00:00
```bash
REMOTE=gitlab
git remote add $REMOTE https://gitlab.com/$USERNAME/$DIR
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
Tell git you're pushing the branch "master" to the remote repo "origin":
2020-01-02 00:04:35 +00:00
```bash
git push -u master origin
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
If someone makes a change on the remote, pull it down with:
2020-01-02 00:04:35 +00:00
```bash
git pull
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
# Branches
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
A branch is a full copy of the project to test additional ideas.
You can make a new branch called 'featurez' like this:
2020-01-02 00:04:35 +00:00
```bash
2024-08-21 15:08:33 +00:00
git branch $FEATURE_BRANCH
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
Have a look at all your branches:
2020-01-02 00:04:35 +00:00
```bash
git branch
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
Switch to your new branch:
2020-01-02 00:04:35 +00:00
```bash
2024-08-21 15:08:33 +00:00
git checkout $FEATURE_BRANCH
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
And if your changes are rubbish, checkout the "master" branch again, then delete "featurez":
2020-01-02 00:04:35 +00:00
```bash
2024-08-21 15:08:33 +00:00
git branch -D $FEATURE_BRANCH
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
Or if it's a good branch, push it to the remote:
2020-01-02 00:04:35 +00:00
```bash
2024-08-21 15:08:33 +00:00
remote=origin
git push $remote $FEATURE_BRANCH
```
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
## Merging
2020-01-02 00:04:35 +00:00
2020-01-02 03:12:18 +00:00
Once you like the feature, merge it into the main branch. Switch to master then merge it:
2020-01-02 00:04:35 +00:00
```bash
2024-08-21 15:08:33 +00:00
git merge $FEATURE_BRANCH
```
2020-01-02 00:04:35 +00:00
2024-08-21 15:08:33 +00:00
And delete the branch, as you've already merged it:
2020-02-26 09:31:17 +00:00
```bash
2024-08-21 15:08:33 +00:00
git branch -d $FEATURE_BRANCH
```
2020-02-26 09:31:17 +00:00
2020-01-02 00:04:35 +00:00
# Subtree
2020-03-08 12:07:40 +00:00
## Pulling another git repo into a subtree
```bash
git subtree add -P config git@gitlab.com:bindrpg/config.git master
```
2020-03-08 12:07:40 +00:00
2020-01-02 03:12:18 +00:00
# Tricks
## Delete All History
```bash
git checkout --orphan temp
```
2020-01-02 03:12:18 +00:00
```bash
git add -A
```
2020-01-02 03:12:18 +00:00
```bash
git commit -am "release the commits!"
```
2020-01-02 03:12:18 +00:00
```bash
git branch -D master
```
2020-01-02 03:12:18 +00:00
```bash
git branch -m master
```
2020-01-02 03:12:18 +00:00
```bash
git push -f origin master
```
2020-01-02 03:12:18 +00:00
Gitlab requires more changes, such as going to `settings > repository` and switching the main branch, then stripping protection.
2020-01-02 00:04:35 +00:00
2020-01-11 13:29:06 +00:00
## Clean up Bloated Repo
```bash
git fsck --full
```
2020-01-11 13:29:06 +00:00
```bash
git gc --prune=now --aggressive
```
2020-01-11 13:29:06 +00:00
```bash
git repack
```
2020-01-11 13:29:06 +00:00
2020-01-11 13:07:52 +00:00
## Find Binary Blobs
```bash
2020-01-11 13:07:52 +00:00
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
```
2022-11-12 18:14:33 +00:00
# More
For big binary files (like images), see [git large-file-storage](git-lfs.md)