user24
user24

Reputation: 119

how to git push without conflict on diverge branch

I did the following

  1. Pull the master with git pull and suppose commit is 12345
  2. did some changes and commit and push it , commit is 6789
  3. Then i realised i did something wrong , then i did git reset --hard 12345
  4. Then i did git commit and commit is 99999

Now i got the error of diverge branch master.

i want to know what option i have after that.

i did this git push --f and it worked but don't know if it was bad or not

Upvotes: 2

Views: 2832

Answers (2)

VonC
VonC

Reputation: 1327144

It is bad only if others have already pulled from your repo.

They would need to fetch, and reset their master branch to yours, or their own master branch would diverge as well, and they would be tempted to do a git push -f, effectively erasing what you just pushed!
They wouldn't necessarily think to do a git pull --rebase, as I describe in "master branch and 'origin/master' have diverged, how to 'undiverge' branches'?".
So it is a communication issue (with the rest of the team).

But if you are the only one working one master, then git push -f is fine.

Instead of a reset --hard, you could simply have done and added to the index your modifications, and then do a:

git commit --amend
git push -f

(That avoid the "reset the working tree" effect of the "reset --hard", which can be a bit dangerous if you had any work in progress)


The following diagram representing the OP's situation shows:

http://s27.postimg.org/5bj32p78z/image.png

The forced pushed (ME) erased at least one commit of the "other guy".
It is important to communicate with them in order to check if they did a pull --rebase, or if they want to restore the repo as it was before your forced push.

for your next commit, git revert remains the way to go (in order to push a new commit cancelling your bad commit): no forced push there.

Upvotes: 1

michas
michas

Reputation: 26555

You pushed a bad commit and someone else already pulled that commit. Therefore that bad commit is already contained it his local repository.

If he does any work on top of that commit and pushes his work, he will reintroduce your bad commit to the main repository. (And of course that bad commit will still have your name on it.)

Therefore your commit will get noticed soon. Better tell them now, to give them a chance to fix their repositories.

If they do a git pull --rebase instead of git pull before pushing their changes everything would probably be fine.

If they already merged your bad commit and pushed the result (and this is the case, if I understand you picture correctly,) they thereby reintroduced you bad commit. You can do git revert to add another commit which reverts the bad commit. (That's also what you should have done from the beginning.)

Upvotes: 0

Related Questions