简体   繁体   中英

Make the tip of my development branch the new tip of my master branch

I found this and this . But it seems that both these turn all the commits in the development branch into commits on the master branch.

What if I just want to say "what is now the tip of my development branch should become the new tip of the master branch, ie the next master branch commit from the last common commit between the two branches".

So basically an unconditional merge, where all conflicts are disregarded in favour of the tip development commit.

Bearing in mind that this is my own project, and no-one else is involved.

Edit

I tried the solution from chepner and got this

(doc_indexer) mike@M17A:/.../doc_indexer$ git cherry-pick my_dev_branch
Auto-merging tests/basic_tests/test_indexing_task.py
CONFLICT (content): Merge conflict in tests/basic_tests/test_indexing_task.py
Auto-merging src/core/visual_log_table_classes.py
Auto-merging src/core/main_window_class.py
CONFLICT (content): Merge conflict in src/core/main_window_class.py
Auto-merging src/core/indexing_task_class.py
CONFLICT (content): Merge conflict in src/core/indexing_task_class.py
error: could not apply d777951... ATG. [... my commit messaage for the tip commit on the dev branch]
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

... then cancelled the cherry-pick operation.

There's got to be a simple answer to this, and I can't believe I'm the first person to have wanted to do this!

I think what you're trying to describe is this. You branched development (which I will call dev ) from master (which I will call main ). Okay, then you continued to work on both branches. But after a while you realized that all your work on main was wrong, that dev was right, and you just want to merge dev into main as if nothing had happened on main since the point of divergence. In other words, starting with this:

A -- B -- C -- D -- E -- F (main)
           \
            X -- Y -- Z (dev)

You want this:

A -- B -- C ---------- MERGE (main)
           \           /
            X -- Y -- Z (dev)

And not only that, you want MERGE to put main in exactly the same state as dev .

There are two parts to the answer. First, you need to erase everything on main back down to the point of divergence:

% git switch main 
% git reset --hard $(git merge-base main dev)

Now do the merge. Git will try to perform a fast-forward, which is not what you want (you have said you don't want main to become identical to dev ); so prevent it:

% git merge --no-ff dev

Presto! The last commit on main , the merge commit that you have just created, is identical to the last commit on dev . The reason is that a merge is combination of all changes on both branches since the point of divergence. But there are no changes on main since the point of divergence — because we erased them in the first step. Thus, this merge commit enacts all and only the changes made on dev since the point of divergence, which is exactly what you are asking for (if I understand correctly).

I think you just want

git checkout master
git cherry-pick dev

where dev is the name of your development branch. This makes the changes introduced by the commit at the tip of dev (and not changes from any of the preceding commits in the same branch) a new commit at the tip of master .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM