Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MadinaB/2b0b39a81475f009e60074a9ab6f5173 to your computer and use it in GitHub Desktop.
Save MadinaB/2b0b39a81475f009e60074a9ab6f5173 to your computer and use it in GitHub Desktop.
Question:
I have to delete 2 last commits pushed to original repo, one of which is a merge commit. I want to delete changes in repo and keep them on my computer.
When I do
git revert -m 1 075cc2f
It adds a revert commit to my history of commits and my 075cc2f commit is still present there.
When I do
git push origin +HEAD^:master
commit is fully deleted from history.
However, when things come to merged second commit, it is being deleted only with commits of a person who pushed before me. We both had some version "v" at moment of cloning a repo. He pushed his commit to original repo and version of a project became "v+1". I still had version "v" and merged a project by:
➜git add .
➜git commit -am "My commit»
➜git pull
subl -w: subl: command not found
error: There was a problem with the editor 'subl -w'.
Not committing merge; use 'git commit' to complete the merge.
➜git commit -am "My commit»
➜git pull
Already up-to-date.
➜git push -u origin master
When I use to a merged commit:
git push origin +HEAD^:master
It is being deleted only with commits of a person who pushed before me.
Now I want to delete that merge commit from history fully without deleting other people's pushes.
[There is another way to do this][1]
> You could follow these steps to revert the incorrect commit(s) or to reset your remote branch back to correct HEAD/state.
> 1. checkout the remote branch to local repo.<br/>
> `git checkout development`
> 2. copy the commit hash (i.e. id of the commit immediately before the wrong commit) from git log
> `git log -n5`
> > output: <br/>
> >
> commit 7cd42475d6f95f5896b6f02e902efab0b70e8038 "Merge branch 'wrong-> > > commit' into 'development'" <br/>
> commit f9a734f8f44b0b37ccea769b9a2fd774c0f0c012 "this is a wrong commit" > <br/>
> commit 3779ab50e72908da92d2cfcd72256d7a09f446ba "this is the correct > > > commit"
>
> 3. reset the branch to the commit hash copied in the previous step <br/>
> `git reset <commit-hash> (i.e. 3779ab50e72908da92d2cfcd72256d7a09f446ba)`
> 4. run the `git status` to show all the changes that were part of the > > wrong commit.
> 5. simply run `git reset --hard` to revert all those changes.
> 6. force-push your local branch to remote and notice that your commit > > history is clean as it was before it got polluted.<br/>
> `git push -f origin development`
[process][2]
But again it deletes not only merged commit, but also his parent
[before][3]
[after][4]
commit b142e1ed9 has 2 parents f4cc23a + 2537c51
[1]: https://i.stack.imgur.com/G4T7O.png
[2]: https://i.stack.imgur.com/3lYvR.png
[3]: https://i.stack.imgur.com/wJHyJ.png
[4]: https://i.stack.imgur.com/kmpin.png
Solution:
The question is solved after some practice, so here is summary what I got so far.
1. Back up original files somewhere since things we gonna use will rewrite the history.
2. Visualize commit structure of the project.
merged
^ ^
/ \
mycommit his commit 2
^ ^
| |
repo --> his commit 1
3. Select a fork from which select a commit on which top you will reapply all commits. Thus, all changes after fork was created will reapply like inline process and not a merge.
4. Use git rebase -i -m <specific commit> // in my case it was <another person’s last commit>
5. git push -u origin master --force
6. Check on github, whether you correctly unmerged a fork. If failed, upload previous version from backup folder using git push -u origin master --force.
mycommit
^
|
his commit 2
^
|
his commit 1
^
|
repo
1. Previous step did an unmerge. If you still have your commit, but inline and still want to delete it from history. You can do git push origin +HEAD^:master. This will rewrite history one step back from last on push. Consecutively git push origin +HEAD:master will undo rewrite on push. git push origin +HEAD^^:master will rewrite history two steps back from last on push. git push origin +HEAD:master will undo rewrite.
2. However, if you did this changes to a project and want to save them: clone repo in new folder and push only from new clones. Since if you will make git push origin -u master after git push origin +HEAD^:master this will undo your rewrite since git push origin +HEAD^:master does not change commits inside your computer (logs, history), but only on a push writes everything like last commits does not exits. But they do not stop to exist in your folder on computer.
his commit 2
^
|
his commit 1
^
|
repo
Overall:
git rebase -i -m <another person’s last commit>
git push -u origin master --force
git push origin +HEAD^:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment