2

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

user24
  • 119
  • 2
  • 9
  • -f [force push] will create problems if your repo is public i.e shared with other peoples. resets all commit ids. Not a good practice. – mrutyunjay Dec 11 '13 at 07:55

2 Answers2

1

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • others have already pulled from my repo. what can i do to fix it. they will be angry if tell them i did this. is there any way now to do something so that their git pull works fine without problem – user24 Dec 11 '13 at 07:22
  • The other thing is how will commit --amend work because in previous commit i overwrote the 20 files , which i should not have overwriiten , so i had to go back to that commit and then make chnages – user24 Dec 11 '13 at 07:25
  • @user24 if they don't have pushed yet, you can reset to 12345, and `git push -f` that (to restore the repo to what it was), and then do a `git revert 12345`, to push a new commit cancelling the previous one (that push won't be forced). But first, did they pushed since your forced push? – VonC Dec 11 '13 at 07:25
  • @user24 the commit --amend was only if a `git push -f` was ok. If it isn't, `git revert` is the way to go, after you restored the repo the way it was (meaning with your incorrect commit pushed, since other already pulled it) – VonC Dec 11 '13 at 07:26
  • yes they have already pushed 3 commits after that . this is the tree of that http://postimg.org/image/motdhk2jz/ – user24 Dec 11 '13 at 07:35
  • @user24 You need to be in contact with your team to make sure they reset their `master` branch. And then you need to do a `git revert` of the faulty commit, and push that (non-forced push) – VonC Dec 11 '13 at 07:38
  • @user24 I don't have access to http://postimg.org/image/motdhk2jz/: can you post the url of the picture? (something finishing by `.png` or `.gif` or `.jpg`) – VonC Dec 11 '13 at 07:38
  • @user24 is your left-side push between ME and the main green branch your `git push -f`? – VonC Dec 11 '13 at 07:43
  • ME is my git push -f on master , but green dots are also on main branch from other guy – user24 Dec 11 '13 at 07:51
0

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.)

michas
  • 25,361
  • 15
  • 76
  • 121