Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Git, is it possible to re-apply an ancestor revision?

Say I have revision A that's from a long time ago and made a desirable change.

Then, later on, I had revision B which made lots of changes to lots of files, including wiping out change A.

Now, much later, I want to re-apply revision A. Is there a good way to do this? The merge and cherry-pick commands seem to skip revisions that are ancestors, and I don't see any flags to ignore ancestry.

There's always diff/apply, but are those really the best way? It seems like this could be "lossy" (going through the intermediary patch format) and might not allow git to use all of the tools normally at its disposal... but this is an uninformed hunch on my part.

like image 664
mackstann Avatar asked Jul 20 '10 23:07

mackstann


People also ask

What is ancestor in git?

git merge-base finds best common ancestor(s) between two commits to use in a three-way merge. One common ancestor is better than another common ancestor if the latter is an ancestor of the former. A common ancestor that does not have any better common ancestor is a best common ancestor, i.e. a merge base.

Does rebase create new commits?

The Rebase Option But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.

Which git command applies commits in the current branch on the top of the provided branch?

In both cases, let's assume we have created a separate feature branch. Git rebase in standard mode will automatically take the commits in your current working branch and apply them to the head of the passed branch.


1 Answers

git cherry-pick A will do exactly what you want. It does not look at ancestry - it only looks to see what changes have already been applied.

Here's an example:

git cherry-pick A
git cherry-pick A
git cherry-pick A

will only create one new commit (at most). The second and third commands are no-ops, since the changes of A have already been applied. However,

git cherry-pick A
git cherry-pick B
git cherry-pick A
git cherry-pick B

will create four new commits. The first and third commits will both do the same thing, while the second and fourth will revert the first and third (even if commit B made other changes than reverting A.) In other words, this is the same as

git cherry-pick A
git revert --no-edit HEAD
git revert --no-edit HEAD
git revert --no-edit HEAD

Hope this helps.

like image 67
Mark Lodato Avatar answered Sep 19 '22 15:09

Mark Lodato