{"id":9756,"date":"2024-07-04T21:08:29","date_gmt":"2024-07-04T19:08:29","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=9756"},"modified":"2024-07-04T21:09:50","modified_gmt":"2024-07-04T19:09:50","slug":"git-delete-last-x-commits-on-github-gitlab","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/git-delete-last-x-commits-on-github-gitlab\/","title":{"rendered":"Git: Delete last x commits on github\/gitlab"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_9756 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_9756')){$('.twoclick_social_bookmarks_post_9756').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Git%3A%20Delete%20last%20x%20commits%20on%20github%2Fgitlab\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Athis%20post%20describes%20if%20something%20went%20wrong%20%20and%20you%20want%20to%20delete%20the%20last%20n%20commits%20on%20gitlab.%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/git-delete-last-x-commits-on-github-gitlab\\\/\",\"post_id\":9756,\"post_title_referrer_track\":\"Git%3A+Delete+last+x+commits+on+github%2Fgitlab\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>this post describes if something went wrong  and you want to delete the last n commits on gitlab.<br \/>\n<!--more--><\/p>\n<p>Make a fresh clone of your repository<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmichael@debdev ~ # git clone git@gitlab.com:MyUser\/MyProject\r\nmichael@debdev ~ # cd MyProject\r\n<\/pre>\n<p>Determine the last commit you want to preserve\/your HEAD should be<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmichael@debdev ~\/MyProject # git log \r\ncommit f526d79a10fbdc702e3c7c6b148a53289868731b (HEAD -&gt; main, origin\/main, origin\/HEAD)\r\nAuthor: michael@debdev\r\nDate:   Thu Jul 4 18:48:38 2024 +0200\r\n\r\n    Doc update\r\n\r\ncommit dc67e928afc7092daa21b33fa8193a96b76375fe\r\nAuthor: michael@debdev\r\nDate:   Thu Jul 4 18:40:10 2024 +0200\r\n\r\n    New File\r\n\r\ncommit 3023fb10bcde2faf390eb9b4f69b538711daba40\r\nAuthor: michael@debdev\r\nDate:   Thu Jul 4 18:24:58 2024 +0200\r\n    Fixes\r\n\r\n....\r\n....\r\n<\/pre>\n<p>In the case commit 3023fb10bcde2faf390eb9b4f69b538711daba40<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmichael@debdev ~\/MyProject # git reset --hard dc67e928afc7092daa21b33fa8193a96b76375fe\r\n<\/pre>\n<p>Then push it pack with -f switch<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmichael@debdev ~\/MyProject # git push -f origin main\r\n<\/pre>\n<p>If an error <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nremote: GitLab: You are not allowed to force push code to a protected branch on this project.\r\n<\/pre>\n<p>occure then your respository is protected. Unprotect it<br \/>\n<figure id=\"attachment_9759\" aria-describedby=\"caption-attachment-9759\" style=\"width: 300px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2024\/07\/gitlab-unprotect.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2024\/07\/gitlab-unprotect-300x116.png\" alt=\"gitlab unprotect repository\" width=\"300\" height=\"116\" class=\"size-medium wp-image-9759\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2024\/07\/gitlab-unprotect-300x116.png 300w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2024\/07\/gitlab-unprotect-1024x397.png 1024w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2024\/07\/gitlab-unprotect-768x298.png 768w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2024\/07\/gitlab-unprotect.png 1226w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-9759\" class=\"wp-caption-text\">gitlab unprotect repository<\/figcaption><\/figure><br \/>\npush the repository and reprotect the repository.<\/p>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, this post describes if something went wrong and you want to delete the last n commits on gitlab.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[805],"tags":[2050,89,505,2049,2048],"class_list":["post-9756","post","type-post","status-publish","format-standard","hentry","category-git","tag-delete-last-commit","tag-error","tag-git","tag-github","tag-gitlab"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/9756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=9756"}],"version-history":[{"count":6,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/9756\/revisions"}],"predecessor-version":[{"id":9763,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/9756\/revisions\/9763"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=9756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=9756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=9756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}