Git: overwriting code of one branch to another

I have 2 branches A and B. Both have diverged from master with lots of different commits, and the master has moved ahead.

What i want to do is replace all changes of B with A.
i.e., B should have exactly the same code as A.

  • How can a clean/smudge filter determine its data stream's file name?
  • Git always pulls files that I cant edit
  • Can I ignore directories from within submodule before submodule update?
  • How to Clone a private repository of github organization in a Jenkins Job
  • GET pull request merge commit sha from pull request number using github api
  • Git: How to check which files exist (and their content) in a shared (bare) repository?
  • I tried using rebase/merge. Even -Xtheirs while rebasing B, but always running into loads of conflicts.

    i tried

    git checkout B
    git reset --hard A

    as given in How to copy one branch to another regardless changes? but this gave this-

    Your branch and 'origin/B' have diverged,
    and have 6603 and 1823 different commits each, respectively.

    Is there a way i can get the code/commits of A copied to B, with no conflicts? Please help.

  • gitignore to allow obj models but not compiled obj files
  • Git “does not appear to be a git repository” ip and port instead of domain?
  • How to insert SSH key on a Git server (git-shell access only)
  • How to undo misoperation of Reinitialized existing Git repository
  • GIT isn't fetching my new branch
  • Why is the following command inconsistent in my repository?
  • 3 Solutions collect form web for “Git: overwriting code of one branch to another”

    If you’re really trying to discard all your changes from B, which seems like a waste, then you can force update the branch pointer.

    git branch -f A B

    Your command to reset your local branch B to A was all right.
    The message you get just informs you that your local branch B is different from the remote branch B.
    If this is really what you want to do, just now do a force push of B to origin/B with git push -f origin B if the remote allows force-pushes.
    But be aware that you are rewriting published history and you will upset any other developers that based work on B as they have to recover from this manually by rebases of their work.

    git checkout A -B B

    Checkout the working tree of A, while giving it the branch name B. (Since B already exists, -B (big B) is required to override.)

    This is effectively the same as this:

    # Checkout the branch 'A' and make the working tree reflect this
    git checkout A
    # Create/override a branch 'B' at this current position
    git checkout -B B

    The end result is that the branches (which are practically just pointers) ‘A’ and ‘B’ point the the exact same commit.

    N.B. The end result of this is pretty much the same as what you had with git checkout B && git reset --hard A.

    Now, since you seem to be concerned with the origin/B has diversed message, there’re two things you can do at this point:

    1. You can update origin/B to reflect your new B by force pushing: (This is provided you aren’t worried that commits that were originally in origin/B will cease to exist.)

      push origin B --force
    2. For some reason, you’re unable to force push (Fast-forward only server?) Or you want to preserve the commit history that was under B–just not their changes. In this case, it’s a little more involved: (This use case is quite rare so I’m not entirely certain this is what you’re after):

      git checkout A --detach
      git merge -s ours B
      git checkout -B B

      This makes a merging commit that proceeds A, that merges B into A without actually changing any files. This new B can be pushed normally as it’s not a divergence from origin/B.

    Git Baby is a git and github fan, let's start git clone.