git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Undo a commit that is already pushed to central server and merged to several branches

From
Johan Herland <johan@herland.net>
Date
Dec 7, 2011, 16:01 UTC
Message-ID
<CALKQrgcQ5jv+oDXxDoTGUhmP-Dg344-oSotb+q-4a3fnEBY1Zw@mail.gmail.com>
In-Reply-To
<4EDF87A6.4000703@fechner.net>
On Wed, Dec 7, 2011 at 16:35, Matthias Fechner <idefix@fechner.net> wrote:
Show 16 quoted lines
> Am 07.12.11 15:42, schrieb Ramkumar Ramachandra:
>>
>> Assuming that you actually want to rewrite the history, the situation
>> calls for a git-reset(1).  Just "git reset --hard HEAD~1" on each of
>> your branches (Caution: first understand what it does!) and you'll
>
>
> that will not work, because in the master branch I already have around 15
> commits after the bogus commit and I want to keep these commits.
>
> And in some other branches there are tons of commits (over 100) after the
> bogus commit with the merge and I only want to remove that bogus commit but
> keep all other commits after the bogus commit (all commits are not related
> to the bogus one, they are all in different files).
>
> It is even fine if I could remove the bogus commit from all branches.

Use "git revert $commit" to undo the effects of the given $commit. This must be applied to all affected branches (either by reverting in the master branch and remerging master to the other branches, or by using "git revert" in each individual branch).

Note that the "git revert" command will NOT remove the original bogus commit. Instead, it will create a new commit that undoes the changes introduced by the bogus commit. If the same lines/files have been touched by other commits in the meantime, you might get conflicts that you'll have to resolve, just like a regular merge.

If you'd rather want the bogus commit to disappear entirely (i.e. rewrite the history so that it looks like the bogus comit never happened in the first place), then things get more complicated. You will need to recreate the history of your master branch, skipping the bogus commit, and then recreate all the other branches in a similar manner. The "git rebase" tool will be helpful here, but I recommend that you understand and get used to how it works before applying it to this problem. Note that rewriting history is only a good idea when you haven't shared the history with anyone else. If other people are already using any of the branches with the bogus commit, then rewriting the history is a BIG NO-NO. In that case, "git revert" is the only sane approach.

Have fun! :)
...Johan
-- 
Johan Herland, <johan@herland.net>
www.herland.net
Previous: Ramkumar RamachandraNext: Matthias Fechner
Message 5 of 6 in “Undo a commit that is already pushed to central server and merged to several branches”
  1. Matthias FechnerDec 7, 2011
  2. Ramkumar RamachandraDec 7, 2011
  3. Matthias FechnerDec 7, 2011
  4. Ramkumar RamachandraDec 7, 2011
  5. Johan HerlandDec 7, 2011
  6. Matthias FechnerDec 8, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.