gitgit-mergegit-checkout

git merge reverted but the changes needed


I have a feature branch, let's call it A, and a main line, let's call it B, where we deliver. The problem is the following:

  1. Branch A was many commits ahead of branch B, nothing extraordinary so far. So I did a merge from branch A to B.
  2. Then I noticed, this is not what I wanted as I suspect the automatic merges were not alright.
  3. I did a revert for the merge, which was ok, the changes merged disappeared.
  4. I only want to have the code state of branch A in branch B; consequently, I removed the directories and files locally in branch B and wanted to do the merge from branch A again, which does not work anymore as git perceives that the merge has already been done. My second idea is to check out branch A in branch B but I don't know how as normal checkouts from the remote repository will go into the corresponding branches: A in A and B in B.

Any idea how I could check out branch A in branch B? Remark: there are no changes in the main line since starting this feature branch A, so what I need is A in branch B. Thanks for any solutions in advance.

Would git fetch <remote> <remoteBranch>:<localBranch> do this?

git fetch origin A:B
git push origin B

Solution: See the answer from VonC and also my questions about the index. Thanks for all responses.

cd /path/to/repo
git switch B
git restore -s A -SW -- .
git commit -m "override B with A content"

Solution

  • But I do not want to investigate file by file where an error might be in the content, I would just like to remove the state in B and get the state of A in B. How could I do that?

    Keeping your history as is, you can restore A content on your current B

    cd /path/to/repo
    git switch B
    git restore -s A -SW -- .
    git commit -m "override B with A content"
    

    pepe-botika69 adds in the comments:

    If, after reverting, the B branch got new changes and you do not need them in your new commit (in this one git commit -m "override B with A content"), you should hard reset B to the point where you reverted A, and then apply the commands.