Remove file from amended commit

前端 未结 4 1671
北恋
北恋 2021-02-14 01:31

I pushed a commit to a repo where I accidentally added a file. Nobody else has merged from the remote repo so I can rewrite history. But when I remove file(unstage, not remove f

相关标签:
4条回答
  • 2021-02-14 01:36

    Here you go:

    git checkout HEAD~ -- path/to/your/file
    git add path/to/your/file
    git commit --amend -C HEAD
    

    git diff -p HEAD~ -- path/to/your/file | git apply -R
    git commit --amend -C HEAD
    

    git reset HEAD~ -- path/to/your/file
    git commit --amend -C HEAD
    
    0 讨论(0)
  • 2021-02-14 01:40

    Try:

    git rm --cached <yourfile>
    git commit --amend
    git push -f
    
    0 讨论(0)
  • 2021-02-14 01:46

    While I did something similar to what Colin and ydroneaud have suggested,

    The answer was to use

    git push +sa1:sa1
    

    where sa1 is my branch. This forces to push even 'nothing'.

    0 讨论(0)
  • 2021-02-14 01:52

    If you need to rewrite the full commit, try to use

    git reset HEAD^
    git add <files to be part of the commit>
    # or git add -pu
    git commit -C <previous commit number>
    

    Before doing this you will need to keep the last commit number to able to reuse the commit message/date/author.

    0 讨论(0)
提交回复
热议问题