Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I go to a specific commit using git-subtree?

I am using Avery Pennarun's git-subtree which is an extension to git.

How do I cherry-pick a commit from a sub-repo into my main repo using git subtree? Also how do I go to a specific commit in the history of the sub-repo after I have already done a git subtree pull on that prefix?

I am primarily running this in the squash commits mode.

like image 890
owagh Avatar asked Oct 19 '12 16:10

owagh


1 Answers

how do I go to a specific commit in the history of the sub-repo?

If you have squashed the commits, then there is no way, since the squash looses the different commits.

Otherwise, with a un-squashed subtree, you can navigate to any commit of the subtree with just the same hash that it has in the original repository from which the subtree was created.

git subtree (without the squash) actually adds into your repository all the relevant commits from the external repository as an independent tree within your current repo. When you do a git subtree add you can notice that several commits are added (all the original commits from the external repo) and a last merge commit that moves the content from that subtree to the given directory specified with the --prefix option. In a nutshell, it checks out a branch from another unrelated repository and then merges it into your current branch by moving all that content into a given subfolder.

All this means is that the history of the external repo is available to you, you can checkout to it just like that, having in mind that by being a completely different tree this checkout will probably modify ALL the contents of your workspace, which on big projects can be lengthy.

This moves us to the second:

How do I cherry-pick a commit from a sub-repo into my main repo using git subtree?

Seems like there is not current "cherrypicking" supported by git subtree on itself. But given the implications of all the above the following can be done.

# move yourself to the subtree commit of your choice
git checkout <subtree-hash>

# fetch commits from the subtree repository, to have available the commit you want
# to cherry pick.
git fetch <path-to-remote>

# cherry pick the hash you want
git cherry-pick <cherry-hash>

# move back to your original branch
git checkout <your-branch>

# subtree merge your cherry pick (using the previous HEAD),
# so that it gets moved to the correct location specified by prefix.
git subtree merge --prefix <subtree-prefix> HEAD@{1}

# Since you probably fetched more commits that you needed from
# the remote, you might want to clean those that where not needed
git gc

Take into consideration that after doing this if you try to git subtree pull into your subtree an update, and it includes the commit you cherry picked, you will end with a conflict since you will have two changes in the same place.

Another more simple option, if you have access to the original subtree repository, is to make the cherry pick there in a branch and then just git subtree pull that specific branch.

like image 57
Maic López Sáenz Avatar answered Sep 30 '22 00:09

Maic López Sáenz