You might use this technique if you have rearranged the history of a repository, but want to keep the original history around for reference, and itâs more convenient to have it both in one repository than split over two. This is because git merge looks for a âmerge baseâ: a common ancestor commit of the branches being mergedâand in this case, there is no such commit. This demonstrates a general way of doing it you can of course just run the appropriate git branch commands yourself to name the new branches as you wish, if there are few of them, or rename them afterward with git branch -m old new.Īlthough this is the easiest method of combining two histories, it is also not usually what you want to do, because you canât use the Git merge mechanism on branches that were originally part of distinct histories. Finally, when done, we remove the remote B, since we do not intend to continue tracking the other repository the remote was just a mechanism to perform the import. We prefix the new branch names with B-, since there may be clashes (as here, where there is a master branch in each repository). The -no-track option avoids creating unnecessary tracking relationships that would just be removed later. The git for-each-ref incantation prepares and runs a set of Git commands that create a local branch named B-x for each remote branch B/x, by running git branch -no-track B-x B/x. To finish incorporating B into A, we need to make local branches for these. The case of pulling in a completely disconnected commit graph is sufficiently unusual that Git warns you about it, saying that the repository youâre fetching has âno common commitsâ with this one.Īfter the fetch command, Git has copied the entire commit graph of B into the object database of A, but the only references in A to the new branches are remote-tracking ones: B/master and B/zorro. (See git-for-each-ref(1) for more detail on this handy command for Git automation.) This series of commands then goes through the Unix command sed rewriting B/foo to B-foo, and finally, the commands are run by feeding them into the Bourne shell ( sh). For each named ref, here selected by the pattern refs/remotes/B/* (all tracking branches for the remote B), it generates a separate git branch command substituting the names marked with %(â¦) to reflect that ref. This recipe uses git for-each-ref, a versatile tool for generating scripts on the fly that apply to a given set of refs (in this case, branches). 'refs/remotes/B/*' | sed -e 's:/:-:' | sh -x $ git branch format='git branch -no-track %(refname:short) Hereâs how to import repository B into repository A in this fashion: $ cd A $ git remote add B URL $ git fetch B The original tip commit might not be at the same spot as shown here, since that depends on the exact sequence of commands you used, but it will show up somewhere earlier in the reflog. To return to that state, all you need to do is: $ git reset -hard e3a1d5b0 In the earliest reflog entry, you can see when you made your original version of commit 3, with commit ID e3a1d5b0. The rebase steps replay commits 1, 2, and 3 at the new location, and in the final step, the local master branch (full ref name refs/heads/master) is reset to the new tip commit. The checkout step is the beginning of the rebase as Git moves HEAD to the new base, the tip of the upstream origin/master (here at commit baa5d906). After the git rebase, for example, your reflog would look something like this : $ git log -gīaa5d906 checkout: moving from master to To undo the rebase operation, then, all you need to do is move the branch ref back to its original spot, which you can discover using the reflog. The original commits are not immediately expunged in any way, but merely abandoned: they are no longer reachable in the commit graph from any branch, and will eventually be garbage collected by Git if they remain that way for some time. The final step of a successful rebase is to repoint the ref of the branch being moved, from the old tip commit to the new one.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |