Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge master into orphan without commit history

Tags:

git

Given an orphan branch without history, how would I incorporate changes done on master branch, since the moment of creation of the orphan, without copying all commits history when pushing the orphan remotely.

A <- B <- C <- D  

        orphan(created from state B) <-X <- Y

I would like to bring c and d to the orphan branch and then x and y to master.

If I do checkout orphan, merge master or rebase master orphan gets the commits, but also all history tree. such as when I push orphan to a remote server, everybody will be able to see all master's history as well.

Also later I would like to merge orphan back into master, bringing commits x and y there.

Edit:

Now merging orphan into master works ok with git merge

A <- B <- C <- D <------X <- Y
                       /  
        orphan <-X <- Y 

But, merging master back into orphan either puts the entire master history into orphan (such as becomes X preceded by B) or with cherry picking, but than I need to skip the merge-commits and also get a lot more conflicts

like image 235
Alex Avatar asked Oct 09 '17 16:10

Alex


People also ask

How do I merge without committing?

With --no-commit perform the merge and stop just before creating a merge commit, to give the user a chance to inspect and further tweak the merge result before committing. Note that fast-forward updates do not create a merge commit and therefore there is no way to stop those merges with --no-commit.

Does git merge keep history?

In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from main using either git merge or git rebase . Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of main .

How do I delete a merge commit in history?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

Does git merge bring all commits?

Git merging combines sequences of commits into one unified history of commits. There are two main ways Git will merge: Fast Forward and Three way. Git can automatically merge commits unless there are changes that conflict in both commit sequences.


1 Answers

Grafts are built for exactly this.

root=$(git rev-list my-orphan-branch --max-parents=0)  # get the orphaned-branch root
echo $(git rev-parse $root B) >.git/info/grafts        # locally remember its real parent

and now all the local commands will know about the ancestry but it will remain repo-local, push and fetch won't export it.

like image 82
jthill Avatar answered Sep 21 '22 19:09

jthill