GuLearn
GuLearn

Reputation: 1864

Completely overwrite a remote branch by the content of a local branch

I made a mistake when using git:

#In master, which has commits like a-b-c#
git push origin master
git checkout -b branch2
# Now I'm in the branch2, and I did:
git reset --hard a
# work...
git commit ...
# work...
git commit ...
# so, now I have commits in branch2: a-d-e

If I go back to master and merge branch2, it will be something like a-b-c-d-e. However, any thing in commits b and c are completely garbage that I don't want.

How can I make remote repo to be a-d-e? (The remote's master already has a-b-c since I did a push before branching to branch2) I really don't want to mess up the repo, will git push origin master --force be a good option?

Edit: After goolging git revert, I found this question worth reading

Upvotes: 0

Views: 197

Answers (1)

Schleis
Schleis

Reputation: 43790

git push origin master --force would rewrite the history as you intend. However, if there are other developers pulling from the repo it will cause them trouble.

As a rule if you have pushed the commits rather than rewriting history you should do git revert <sha> This will make a new commit reversing the changes of the sha provided. This doesn't erase the problem from the history, but will cause the least amount of trouble.

Upvotes: 1

Related Questions