The meaning of life is to explore the world

GitHub tips on changing commit history

Posted on By Jason Liu

Tips

  1. Use git help <verb> to learn the Git commands.

  2. How to delete unpushed commits?
    git reset --hard origin/master

  3. How to delete orphan commits?
    git gc --prune=now --aggressive

  4. How to undo pushed commits?
    git log
    git checkout <older commit hash tag>
    git checkout master
    git revert <older commit hash tag>
    git commit -m "Undo <older commit hash tag>"
    git revert ...
    git commit ...
    git revert ...
    git commit ...
    

    BTW, We can use git log --oneline to view a short list of log history.

  5. How to remove big file in pushed GitHub history?
    https://help.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository
  • 5.1 Run in the repo
    git filter-branch --force --index-filter "git rm -r --cached --ignore-unmatch <unwanted_folder/[unwanted_file]>" --prune-empty -- --all
    Rewrite abcdefabcdedfefcfefafcfefd98738127382630 (24/29) (28 seconds passed, remaining 5 predicted)    rm 'XXXX/YYYY.ZZZ'
    Rewrite cfaecbacfdedfefc394893fefd987381273826ec (29/29) (34 seconds passed, remaining 0 predicted)
    Ref 'refs/heads/master' was rewritten
    WARNING: Ref 'refs/remotes/origin/Cathy' is unchanged
    Ref 'refs/remotes/origin/master' was rewritten
    WARNING: Ref 'refs/remotes/origin/master' is unchanged
    

    Possible error encountered:
    fatal: bad revision 'rm'
    Cause:
    Windows must use “ instead of ‘

  • 5.2 Success
    C:\XXXX\YYYY>git push origin --force --all
    Enumerating objects: 1744, done.
    Counting objects: 100% (1744/1744), done.
    Delta compression using up to 12 threads
    Compressing objects: 100% (488/488), done.
    Writing objects: 100% (1744/1744), 39.24 MiB | 378.00 KiB/s, done.
    Total 1744 (delta 645), reused 1738 (delta 642)
    remote: Resolving deltas: 100% (645/645), done.
    To https://github.com/XXXX/YYYY.git
     +03f9ee8...9085268 master -> master (forced update)
    

    Possible error encountered:

    C:\XXXX\YYYY>git push origin master --force
    Enumerating objects: 1744, done.
    Counting objects: 100% (1744/1744), done.
    Delta compression using up to 12 threads
    Compressing objects: 100% (488/488), done.
    error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054
    Writing objects: 100% (1744/1744), 39.24 MiB | 107.00 KiB/s, done.
    Total 1744 (delta 645), reused 1738 (delta 642)
    fatal: the remote end hung up unexpectedly
    Everything up-to-date
    

    Cause:
    Network slowness

  • Note!!!
    The current branch still have big files, even if you run git gc --prune=now --aggressive.
    But the big file will be only removed in remote and new-cloned repositories.