git

Starting

New Machines

1git config --global user.email "$YOUR_EMAIL"
1git config --global user.name "$YOUR_NAME"

New Git

Start a git in directory $DIR:

1mkdir $DIR && cd $DIR
1git init

Make a file explaining what the project does:

1vim README.md

Add this to the git:

1git add README.md

Then make the initial commit, explaining the change you just made:

1git commit

Working

Once you make a change to some file, add it and make a commit explaining it.

1git add $FILE
1git commit -m"change $FILE"

Check your history:

1git 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:

1REMOTE=gitlab
2git remote add $REMOTE  https://gitlab.com/$USERNAME/$DIR

Tell git you're pushing the branch "master" to the remote repo "origin":

1git push -u master origin

If someone makes a change on the remote, pull it down with:

1git 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:

1git branch $FEATURE_BRANCH

Have a look at all your branches:

1git branch

Switch to your new branch:

1git checkout $FEATURE_BRANCH

And if your changes are rubbish, checkout the "master" branch again, then delete "featurez":

1git branch -D $FEATURE_BRANCH

Or if it's a good branch, push it to the remote:

1remote=origin
2git push $remote $FEATURE_BRANCH

Merging

Once you like the feature, merge it into the main branch. Switch to master then merge it:

1git merge $FEATURE_BRANCH

And delete the branch, as you've already merged it:

1git branch -d $FEATURE_BRANCH

Subtree

Pulling another git repo into a subtree

1git subtree add -P config git@gitlab.com:bindrpg/config.git master

Tricks

Delete All History

1git checkout --orphan temp
1git add -A
1git commit -am "release the commits!"
1git branch -D master
1git branch -m master
1git 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

1git fsck --full
1git gc --prune=now --aggressive
1git repack

Find Binary Blobs

1git rev-list --objects --all \
2| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
3| sed -n 's/^blob //p' \
4| sort --numeric-sort --key=2 \
5| cut -c 1-12,41- \
6| $(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