Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

Git - Overiding remote branch with local branch

I have 2 commits on my local master branch, ahead of the remote branch. And the remote master branch is 4 commits ahead of my local branch, 4 commits pushed by my coworker.

The 4 commits of my coworker are only causing issues, he had issues during commit & push. Luckily my local branch is in perfect working order and he saved his work in a temp folder.

What is the best way to get :

A remote branch with my 2 commit pushed and the 4 commits of my coworker deleted.

In other word, I want to push my local branch to the remote and override any changes my coworker made.

question from:https://stackoverflow.com/questions/65907124/git-overiding-remote-branch-with-local-branch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solved it, here is how. For reference, the last commit in common between the remote master and my local master will be named A.

First reset the local commits and stash them :

git reset --mixed A
git add .
git stash

Then pull the remote master : This causes no merge conflict as the local branch is now only behind the remote master.

git pull

Revert the 4 unwanted commits : Multiples methods available, I used checkout to commit A.

git checkout -f A -- .

Commit & push to remote :

git commit -m "Revert to commit 'A'"
git push

Restore local commits stashed before the operation :

git stash apply
git add .
git commit -m "Local commits"
git push

Feel free to edit to improve this method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...