descriptionText. How to do nonobvious (to me) stuff with git
last changeFri, 9 Jun 2023 20:27:25 +0000 (16:27 -0400)
readme

git-cheat-sheet

How to do stuff with git

Splitting a repo

Remove history of all but select files in cloned repo
$ git clone old new
$ cd new
$ git clean -dfx
$ git gc --aggressive --prune=now
$ git remote rm origin
$ git filter-branch \
    --prune-empty \
    --index-filter '
        git ls-tree -r --name-only HEAD \
        | grep -v file_i_want_to_keep_1 \
        | grep -v file_i_want_to_keep_.. \
        | grep -v file_i_want_to_keep_n \
        | xargs git rm --cached -r --ignore-unmatch
    ' \
    HEAD
$ git gc --aggressive --prune=now
Remove history of previously-removed (not in current tree) files
$ git log --pretty=format: --name-status \
    | awk '$0 != "" {print $2}' \
    | sort -u > /tmp/tree.old
$ git ls-tree -r --name-only HEAD > /tmp/tree.new
$ git filter-branch \
    --prune-empty \
    --index-filter '
        grep -Fvxf /tmp/tree.new /tmp/tree.old \
        | xargs git rm --cached -r --ignore-unmatch
    ' \
    HEAD

Inserting a new root commit

git checkout --orphan $TEMP_BRANCH
git rm -rf .
git commit --allow-empty -m $INIT_COMMIT_MSG
git rebase --onto $TEMP_BRANCH --root $MAIN_BRANCH
git branch -d $TEMP_BRANCH

Deleting all tags, locally and remotely

for tag in `git tag`;
do
    git tag -d $tag
    git push $REMOTE_NAME :refs/tags/$tag
done

Get an older version of a file

git cat-file -p $COMMIT_DIGEST:$FILE_PATH

Splitting a commit

From docs:

shortlog
2023-06-09  Siraaj KhandkarAdd the --force flag to second filter-branch run master
2022-08-31  Siraaj KhandkarAdd git add -p
2020-01-05  Siraaj KhandkarFix unintended deletion of current directory
2020-01-04  Siraaj KhandkarAdd merging 2 repos
2020-01-04  Siraaj KhandkarAdd commit-splitting instructions from docs
2016-10-26  Siraaj KhandkarAdd getting an older version of a file
2014-10-07  Siraaj KhandkarReplace "origin" with $REMOTE_NAME
2014-10-07  Siraaj KhandkarAdd deleting all tags, local and remote.
2014-10-06  Siraaj KhandkarReplace 'cp' with 'git clone'
2014-10-06  Siraaj KhandkarDe-emphasise subsections.
2014-10-06  Siraaj KhandkarAdd steps to insert a new root commit.
2014-09-18  Siraaj KhandkarBreak-up long lines.
2014-09-18  Siraaj KhandkarAdd split repo cleaning steps.
2014-09-18  Siraaj KhandkarInitial commit
heads
17 months ago master
This page took 0.046201 seconds and 7 git commands to generate.