Contents

howToEraseCommitLog

   Sep 16, 2023     2 min read

This page explains that erase git commit log.

related system

linux, git


How to rollback git commit log?

  1. clone your git repository
    git clone https://gitexample.com/exampleUser/exampleRepository.git
    
  2. change directory to download repository.
    cd exampleRepository.git
    
  3. edit and run this command. please only edit [‘integer_number’] in command.
    git reset --hard HEAD~[integer_number]
    

    If you want to rollback to the second previous commit from the last commit, type…

    git reset --hard HEAD~2
    
  4. push edited commit log to server. [branch-name] is branch name, such as main.
    git push origin [branch-name] --force
    
Warning!

edit commit log is very dangerous work. This work recommanded when other files are not uploaded since the file you want to delete.

This jobs moves head back as you want. So, you can keep “verified” sign on github.

How to erase specific git commit log?

  1. clone your git repository
    git clone https://gitexample.com/exampleUser/exampleRepository.git
    
  2. change directory to download repository.
    cd exampleRepository.git
    
  3. edit and run this command. please only edit [‘file_path/file_name’] or [‘folder_path’] in command.
    git filter-branch --force --index-filter "git rm --cached --ignore-unmatch 'file_path/file_name'" --prune-empty --tag-name-filter cat -- --all
    

    if you want to delete under certain directory, then use this command.

    git filter-branch --force --index-filter "git rm --cached --ignore-unmatch -r 'folder_path'" --prune-empty --tag-name-filter cat -- --all
    
  4. push edited commit log to server
    git push origin --force --all
    
Warning!

edit commit log is very dangerous work.

  1. --force options are forced overwriting existing commit log.
  2. This job erase “verified” sign on github.
  3. Particularly when collaborating, you need to be careful as this may affect other people’s work.
  4. when you erase the commit log, then existing files also deleted. so you reupload the file after erase commit.

###When This job recommend?

  1. when git upload files that includes your personal info, secret files, etc.
  2. when you needed to delete specific file completely.