开发者

Deleting local commit when is calling git pull

开发者 https://www.devze.com 2023-04-07 17:30 出处:网络
I\'ve made a commit with an error (program isn\'t compiled) and after made: g开发者_开发知识库it push (I).

I've made a commit with an error (program isn't compiled) and after made: g开发者_开发知识库it push (I).

My colleague done: git pull (She) and got uncompiled state of repository. After that added some commits (about project documentation - for the compiling wasn't critical) and done again: git push (She)

After that we've got following state of repository:

  1. Some her commit
  2. Some her commit
  3. My commit with error
  4. My another commit

    I wanted exactly to delete commit 3. And for that I've made

    git-rebase --onto <sha of commit 4> <sha commit 3> master

    git push --force

Now we have correct state of repository (without commit 3), but with all another changes. But, if she do

git pull
git push

she will make merge with her local commit #3 and then pull it to repository. How can I make that someone (not only she) after git pull will correct state of repository - with all changes, but without commit #3?

Notes: Probably she added (in another case - feature) local commits above last. And her local repository newer than server's repository.


You have discovered the problem with changing public history: everyone who has seen the change must agree to change it the same way. In this simple case, they can likely get the new proper version by also doing the same rebase command that you did:

git-rebase --onto <sha of commit 4> <sha commit 3> master

This will update their local master to agree with the remote master branch again and they can continue as normal.

There is no way to automatically cause this to happen. The easiest way for people who were simply tracking master, or had pushed all changes they made when you did this rebase, may be to execute git reset --hard origin/master but this will erase any local commits made on the current branch that weren't in master when you did your rebase.

An alternate way to achieve the same thing is with git revert. This will simply add a new commit which is the inverse of a different commitish. This doesn't edit history, it simply adds to it in such a way as to undo it. This causes the problem commit to still show up in git log and related commands, but doesn't cause issues for everyone else.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号