1

Edit: Regarding This question may already have an answer here:, note that the title of that question refers to undoing a commit, not a push. I don't think SO should be reinforcing the frequent confusion between the two within git. Also is a question with 22 different answers going to be the best reference?

I created a new project lambda in a repository X, under the dev branch, then did git add, git commit and git push.

Turns out it doesn't belong in that repository, but in repository Y, under its dev branch. Now people looking at X are seeing a bunch of stuff that doesn't belong.

How do I remove that push from X dev? Note that I don't want to remove the whole branch from X, just the bad push I did.

If it helps, everything is self contained in that project, no other projects were altered. Just deleting lambda project and any history of it would be okay too.

After searching, I think what I want to do is undo a git push. But I'm not sure I understand the solution.

git log

This is what git log is telling me.

$ git log
commit 0c9ac8b157222995d3414b84e7ce1b3e1adf5560
Author: Clueless <clueless@initech.com>
Date:   Fri Feb 14 10:01:38 2014 -0400

Initial commit of lambda.

commit bdd142c778f0c43141cf48d60e4ee0bef018e1c0
Author: Not Clueless <notclueless@initech.com>
Date:   Thu Feb 13 12:50:39 2014 -0400

update red with latest and greatest

commit f1047816fa7b201de7f798e6026b8b29a1bf8f75
Author: Also Not Clueless <alsonotclueless@initech.com>
Date:   Wed Feb 5 15:48:07 2014 -0400

update green with latest and greatest

etc...

So do I want to use?

git push -f origin 0c9ac8...:<???>

But don't I want to make bdd142... be the origin? Also what do I specify for ??? ?

Diagrams

Perhaps a couple of diagrams might help:

What it looks like now:

X-repo
+---master-branch
|
+---dev-branch---+---red-project
|                |
|                +---green-project
|                |
|                +---blue-project
|                |
|                +---lambda-project <-- doesn't belong
|
+---other-branches

Y-repo
+---master-branch
|
+---dev-branch---+---alpha-project
                 |
                 +---beta-project
                 |
                 +---gamma-project

What it should look like:

X-repo
+---master-branch
|
+---dev-branch---+---red-project
|                |
|                +---green-project
|                |
|                +---blue-project
|
+---other-branches

Y-repo
+---master-branch
|
+---dev-branch---+---alpha-project
                 |
                 +---beta-project
                 |
                 +---gamma-project
                 |
                 +---lambda-project <-- belongs here
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
  • 1
    This is almost certainly a duplicate of [How to undo the last Git commit?](http://stackoverflow.com/questions/927358/) – Ajedi32 Feb 14 '14 at 19:56
  • @Ajedi32 - In that question, it isn't mentioned that a push was made to a remote repository. – Yimin Rong Feb 14 '14 at 20:04
  • 1
    Yeah, I guess you're right. There are several answers to that question that do address that situation though. There's also these: [How can I remove 'git commit' after 'git push'](http://stackoverflow.com/questions/6459080), [Git: Rolling back a remote repository](http://stackoverflow.com/questions/588414/) – Ajedi32 Feb 14 '14 at 20:23
  • Does this answer your question? [How can I undo a \`git commit\` locally and on a remote after \`git push\`](https://stackoverflow.com/questions/6459080/how-can-i-undo-a-git-commit-locally-and-on-a-remote-after-git-push) – Gino Mempin Aug 20 '22 at 10:56

1 Answers1

1

The safest is to do:

git revert 0c9ac8b157222995d3414b84e7ce1b3e1adf5560

This will undo the commit locally. Then:

git push

This will update the remote repository. The commit will still be in remote, just not in the way any more.