Search code examples
gitgit-mergegit-remote

Git: Discard all changes on remote branch and push local changes without force push


The case is similar to : Git: Discard all changes on a diverged local branch.

Differences:

  • I want to discard changes which have been pushed to remote repository,
  • git push origin foo:foo --force is rejected (origin doesn't allow force pushing).

Given commit graph:

A--B--C--O1--O2--O3 (foo)
       \
         L1--L2--L3 (origin/foo)

I want to achieve this if possible:

A--B--C--O1--O2--O3 (foo origin/foo)

If above is impossible then following is ok for me:

A--B--C--O1--O2--O3--M1 (foo)
       \            /
         L1--L2--L3 (origin/foo)

but I want git diff M1..03 be empty i.e. discard all changes introduced in origin/foo. And -s ours strategy doesn't help because this only works for conflicts and non-conflicting changes will still be added to M1.

How can I do this?

SOLUTION (by Mohan Kumar P)

Delete remote branch and push. git should create remote branch automatically if it's tracked (check git branch -vv).

To achieve this:

A--B--C--O1--O2--O3 (foo origin/foo)
       \
         L1--L2--L3

type:

git push origin --delete foo
git checkout foo  # Git ≥ 1.6.6.
git push origin foo

Solution

  • You can simply delete the remote branch and then push your local branch. I assume it is not restricted.