Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Sunday, February 12, 2012

Reverting With git Subversion Style

I was already familiar how to reset the entire head of my git repositories to a desired state, but what if I needed to revert the changes in one specific file which I had managed to bugger up while keeping all my other changes.  The only reason this was hard because my brain is still slightly working in subversion language:

git checkout filename

so just check out the version of the file you want.  This will land you in trouble if you have a banch with the same name as the file, in which case you can do:

git checkout -- filename

Tuesday, January 10, 2012

Git: New Repository From A Subdirectory

Suppose I have this repository layout to start with
XYZ/
    .git/
    XY1/
    ABC/
    XY2/
And I want a new repository for ABS only, here is the excellent answer from over at stack overflow.

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 -- --all
Then delete the backup reflogs so the space can be truly reclaimed (although now the operation is destructive). The -- --all keeps the project branches and tags included in the the new repo.
 $ git reset --hard
 $ rm -rf .git/refs/original/
 $ git reflog expire --expire=now --all
 $ git gc --aggressive --prune=now
and now you have a local git repository of the ABC sub-directory with all its history preserved.