Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Git Tips & Tricks»How to Delete Commit History from Github Repository

    How to Delete Commit History from Github Repository

    By RahulMay 9, 20233 Mins Read

    GitHub is an essential tool for developers and teams to manage, collaborate, and track their work. However, sometimes you might need to delete a commit history, either to clean up the repository or remove sensitive information. In this article, we’ll walk you through the process of deleting commit history from GitHub using Git commands

    Advertisement

    Delete Commit History from Github Repository

    Follow the below instruction to completely delete the commit history of the GitHub repository.

    Warning: Please note that this action is irreversible, so make sure you have a backup or you are absolutely certain about the changes you want to make.
    1. Backup your Repository: Before making any changes, it’s crucial to backup your repository. You can do this by cloning the repository to a separate directory on your local machine:
      git clone https://github.com/username/repo-name.git backup-repo 
      

      Replace “username” with your GitHub username and “repo-name” with the name of the repository you want to backup.

    2. Create a Orphan Branch: Create and checkout a new branch that will serve as the starting point for your cleaned-up history. It’s a good practice to use a descriptive name, such as “cleaned-history”:
      git checkout --orphan cleaned-history 
      
    3. Add and Commit Changes: Now, add all your files to the new branch and commit the changes. This will create a new commit with the current state of your files, but without the previous commit history:
      git add -A 
      git commit -am "the first commit" 
      
    4. Delete Old Branch: Delete the old branch that contains the history you want to remove. If your main branch is named “main”, you can delete it with the following command:
      git branch -D main
      
    5. Rename the New Branch: Rename the “cleaned-history” branch to “main” or the name of the original branch you deleted:
      git branch -m main
      
    6. Force Push the Changes to GitHub: Now you need to push the changes to GitHub, overwriting the remote branch. This action will permanently delete the commit history from the remote repository:
      git push -f origin main
      

    Keep in mind that deleting commit history is a destructive operation, as it permanently removes commits from the repository. It is generally not recommended to delete commit history unless it is absolutely necessary.

    Conclusion

    By following these steps, you’ve successfully deleted the commit history from your GitHub repository. Remember, this action is irreversible, so always backup your repository before making any changes. It’s essential to understand the implications of these actions to avoid potential issues in your development workflow.

    commit git remove Repository
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Git Restore: Functionality and Practical Examples

    Git Switch: Functionality and Practical Examples

    Git Reset: A Comprehensive Guide with Examples

    View 14 Comments

    14 Comments

    1. Liana on May 4, 2023 2:58 am

      thanks for info

      Reply
    2. Zack on April 20, 2022 9:54 pm

      Exactly what I needed!
      Thank you!

      Reply
    3. Felipe Sodre on June 11, 2020 6:22 pm

      Dear RAHUL, I have a branch on upstream that has already a open Pull Request with some comments.

      What happens to my PR if I execute this procedure you suggest. Does my PR get closed?

      Is there any other way that I can just remove my commits comments history, keeping just the latest one on my java files.

      I had to commit several versions as a test. I don’t want the next developer get the file with several comments as testing.

      Thanks.

      Reply
    4. Larik on March 1, 2020 11:26 am

      It’s helped, thanks!
      They say: “… please clear history and try again…”
      But they didn’t make any way to clear that from UI, what is the point…

      Reply
    5. Akash on October 26, 2019 11:15 pm

      Great! Thanks 🙂

      Reply
    6. Horatio on August 11, 2019 5:32 am

      Salute!

      Reply
    7. João on July 19, 2019 2:08 am

      Thank you!

      Reply
    8. rosen on July 16, 2019 11:00 am

      i love you. i couldnt remove sensitive data from the repo because i burried it with more commits and nothing else worked until i found this <3

      Reply
    9. Ondřej on June 10, 2019 10:14 am

      Thanks, this helped. However, the remote server still keeps the old commits and they may be accessible, e.g. from web GUI or by commit hash. This is a problem if you accidentally commited a password.

      The fix is to ssh into the bare repo and run `git gc –prune=now`. That should drop them.

      Reply
    10. Grateful on June 7, 2019 7:46 am

      Works like a charm. Thank you!

      Reply
    11. Jack on June 1, 2019 11:04 pm

      Thanks!

      Reply
    12. Bro on May 15, 2019 9:37 pm

      Excellent.

      Reply
    13. jason on April 13, 2019 1:52 am

      Thanks

      Reply
    14. Ralph on June 27, 2018 4:41 pm

      Thanks.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting and Getting the Default Timezone in Python
    • What is Media Access Control (MAC) Address?
    • What is Cross-Site Scripting (XSS)?
    • What is Content Security Policy (CSP)?
    • A User’s Guide to Understanding Redirection Operators in Bash
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.