descriptionText. How to do nonobvious (to me) stuff with git
last changeWed, 31 Aug 2022 12:48:15 +0000 (08:48 -0400)


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
    ' \
$ 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/
$ git filter-branch \
    --prune-empty \
    --index-filter '
        grep -Fvxf /tmp/ /tmp/tree.old \
        | xargs git rm --cached -r --ignore-unmatch
    ' \

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`;
    git tag -d $tag
    git push $REMOTE_NAME :refs/tags/$tag

Get an older version of a file

git cat-file -p $COMMIT_DIGEST:$FILE_PATH

Splitting a commit

From docs:

2022-08-31  Siraaj KhandkarAdd git add -p master
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
5 weeks ago master
This page took 0.036353 seconds and 7 git commands to generate.