How to commit after removing a directory from GIT

2019-09-02 08:36发布

Several posts talk about removing a file or directory from GIT - but they don't go as far as explaining how to PUSH those changes into the main repository.

For example, this works just fine to remove the directory but when I try to PUSH this change to the main Git repo, I am denied:

! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '/repo/project.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.

What is the last step to actually push this to the main repo and actually remove that directory?

2条回答
不美不萌又怎样
2楼-- · 2019-09-02 09:09

Nick Lewis' answer is sufficient, but I just want to add some emphasis, more than will fit in a comment. Here's a quote from the git filter-branch man page:

WARNING! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for further information about rewriting published history.)

Italics emphasis added - that's pretty important. Filter-branch really does rewrite history, it'll screw up anyone who's cloned/pulled from your repo, and it's scary. You should really know all that before you give it a go. Most people here are pretty good about providing that reminder whenever they recommend filter-branch or rebase; I think it's appropriate to have a pretty strong repetition of it here, on a question where the answer is push --force in order to completely rewrite the history in your public repo. That's a scary operation.

查看更多
Root(大扎)
3楼-- · 2019-09-02 09:24

When you remove something so aggressively, you rewrite history. Therefore, the upstream repository is rejecting your change because it would result in a loss of history. In this case, you will need to use

git push --force
查看更多
登录 后发表回答