To delete a file from Git submission

This translation from: the Remove Files from the commit Git

I am using Git and I have committed few files using I am using Git, I have submitted several documents

git commit -a

Later, I found that a file had mistakenly been added to the commit. Later, I found a mistake to add files to the submission.

How can I remove a file from the last commit? How to delete a file from the last submission?


#1st Floor

Reference: https://stackoom.com/question/qN2l/ delete a file from Git submission


#2nd Floor

ATTENTION ! Note ! You only want to the Remove IF A File from your Previous the commit, and the Keep IT ON Disk , the Read juzzlin's answer the Just above. If you want to delete the file from the previously submitted and stored on disk , please read the above juzzlin of answer .

Last the commit the this IS your IF and you want to Completely the Delete File from your local and at The Remote at The Repository , you CAN: If this is your last commit, and you want to completely remove the file from the local and remote repository , the can:

  1. remove the file git rm <file> deleted the filegit rm <file>
  2. commit with amend flag: git commit --amend submit the modified flags:git commit --amend

The amend flag tells git to commit again , but "merge" (not in the sense of merging two branches) this commit with the last commit. Correction flag tells git submit again, but "merge" (not in the sense of merging two branches a) the submission of the final submission.

Stated in at The Comments AS, a using git rmhere Wallpaper at The IS like a using rmthe Command Itself! As the review, is used here git rmjust like using rmthe command itself!


#3rd floor

If you have not pushed the changes on the server you can use if you do not have to push changes on the server, you can use

git reset --soft HEAD~1

It will reset all the changes and revert to one commit back it will reset all changes and revert to commit

If you have pushed your changes then follow steps as answered by @CharlesB If you have to push the changes, please follow the steps @CharlesB


#4th floor

Removing the file using rm will delete it ! Use rm to delete the file will delete it!

You're always adding to a commit in git rather than removing, so in this instance return the file to the state it was in prior to the first commit (this may be a delete 'rm' action if the file is new) and then re-commit and the file will go . you always add submitted instead of deleting the git in, so this example will file returns to the state before the first submission (if the file is new, it may be deleted 'rm 'action) then resubmit the file will continue.

To return the file to some previous state : you want the file to return to a previous state:

    git checkout <commit_id> <path_to_file>

or to return it to the state at the remote HEAD: or return it to the state of the remote HEAD:

    git checkout origin/master <path_to_file>

then amend the commit and you should find the file has disappeared from the list (and not deleted from your disk!) and then modify commit, you should find the file disappears from the list (and not deleted from the disk!)


#5th Floor

I think other answers here are wrong, because this is a question of moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them. I think the other answers here is wrong, because this is the document submitted by a mistake in the past to submit questions moved back to the staging area for the primary, but not cancel the changes made to them. : This can be done like Paritosh Singh suggested that this can be completed as Paritosh Singh suggested:

git reset --soft HEAD^ 

or either

git reset --soft HEAD~1

Then reset the unwanted files in order to leave them out from the commit: then reset the unwanted files so that they can be removed from the submission:

git reset HEAD path/to/unwanted_file

Now commit again, you can even re -use the same commit message: now submit again, you can even reuse the same commit message:

git commit -c ORIG_HEAD  

#6th floor

If you want to preserve your commit ( maybe you already spent some time writing a detailed commit message and do not want to lose it), and you only want to remove the file from the commit, but not from the repository entirely: if you want to keep your submission (perhaps you've spent some time writing a detailed submission information and do not want to lose it), you just want to delete the file from the submission, rather than completely removed from the repository:

git checkout origin/<remote-branch> <filename>
git commit --amend
Original articles published 0 · won praise 136 · views 830 000 +

Guess you like

Origin blog.csdn.net/xfxf996/article/details/105223841