Skip to content

Instantly share code, notes, and snippets.

@schmurfy
Created May 20, 2009 15:02
Show Gist options
  • Save schmurfy/114859 to your computer and use it in GitHub Desktop.
Save schmurfy/114859 to your computer and use it in GitHub Desktop.
Git related commands
detach subdirectory:
You want to clone your repository and then use git filter-branch to mark everything but the subdirectory you want in your new repo to be garbage-collected. To clone your local repository:
$ git clone --no-hardlinks /XYZ /ABC
The --no-hardlinks switch makes git use real file copies instead of hardlinking when cloning a local repository. The garbage collection and pruning actions will only work on blobs (file contents), not links.
Then just filter-branch and reset to exclude the other files, so they can be pruned:
$ git filter-branch --subdirectory-filter ABC HEAD
$ git reset --hard
$ git gc --aggressive
$ git prune
and now you have a local git repository of the ABC sub-directory with all its history preserved.
EDIT -- For most uses, git filter-branch should have the added parameter -- --all. (Yes that's really dash dash space dash dash all. This needs to be the last parameters for the command.) As Matli discovered, this keeps the project branches and tags included in the the new repo.
# initialize a new gitosis repository
# after the gitosis repository has been created:
mkdir <name>
cd <name>
git init
git remote add origin git@<server>:<name>.git
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
# commit some files
# and:
git push
recover lost commits (when commiting and not on any branches, usually in submodules):
$ git checkout master
$ git fsck --lost-found
$ git show <hash> # see blob content
$ git cherry-pick <hash>
git filter-branch --env-filter '
n="new author name"
m="new author email"
export GIT_AUTHOR_NAME="$n"
export GIT_AUTHOR_EMAIL="$m"
export GIT_COMMITTER_NAME="$n"
export GIT_COMMITTER_EMAIL="$m"
'
$ git checkout feature
$ git reset --soft HEAD^ (2)
$ git reset (3)
# 2. This removes the WIP commit from the commit history, and sets your working tree to the state just
# before you made that snapshot.
#
# 3. At this point the index file still has all the WIP changes you committed as snapshot WIP. This updates the index
# to show your WIP files as uncommitted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment