git 删除所有内容并上传新版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11071420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:56:54  来源:igfitidea点击:

Delete everything and upload new version

gitversion-control

提问by AturSams

We are using git for version control and right now we are getting a lot of warnings when trying to upload the most recent version. I am new to git and don't have patience for the restrictions, is there any way to delete everything and upload the current version?

我们正在使用 git 进行版本控制,现在我们在尝试上传最新版本时收到很多警告。我是 git 新手,对限制没有耐心,有什么办法可以删除所有内容并上传当前版本?

This is what I get now when trying to upload.

这就是我现在尝试上传时得到的。

$ git push origin master
Username for 'https://code.google.com':
Password for 'https://<removed>@code.google.com':
To https://code.google.com/p/<removed>/
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://code.google.com/p/<removed>/'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

回答by Michael Kohler

1.) Delete everything in your repository folder

1.) 删除存储库文件夹中的所有内容

2.) run git rm *to tell git to remove all files (check with git statuswhether there are any unstaged files left)

2.) 运行git rm *告诉 git 删除所有文件(检查git status是否有任何未暂存的文件)

3.) do a git commit -a -m "commitmessage"a git push origin masterto delete all files on the remote git server

3)做了git commit -a -m "commitmessage"一个git push origin master要删除的远程服务器的Git上的所有文件

4.) check if you have an empty remote repository now

4.) 检查您现在是否有一个空的远程存储库

5.) copy all new files to the local repository folder

5.) 将所有新文件复制到本地存储库文件夹

6.) run git add *to tell git to add all new files (check with git statuswhether there are any unstaged files left)

6.) 运行git add *告诉 git 添加所有新文件(检查git status是否还有任何未暂存的文件)

7.) commit and push the new files

7.) 提交并推送新文件

Now you should have the version on your remote git repository.

现在您应该在远程 git 存储库中拥有该版本。

回答by CB Bailey

You are unable to push because there are commit on the remote that aren't merge in to the commit that you are trying to push. If you want to discard all of the changes that are in the currently in the remote you can do something like this.

您无法推送,因为远程上的提交未合并到您尝试推送的提交中。如果您想放弃当前远程中的所有更改,您可以执行以下操作。

git fetch
git merge -s ours origin/master
git push origin master

Note that this throws away changes that have been introduced in the remote that you don't have in your local repository.

请注意,这会丢弃您在本地存储库中没有的远程中引入的更改。

回答by Korayem

If you want to permanently delete the files from the remote repository and in retrospect as if they weren't pushed in the first place to start fresh, then base on this answeryou could do the following

如果您想从远程存储库中永久删除文件,并且回想起来就好像它们没有被首先推送以重新开始,那么根据这个答案,您可以执行以下操作

Run the following locally (Notice the *deletes every folder/file):

在本地运行以下命令(注意*删除每个文件夹/文件):

git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch *" -- --all

Now it's time to update git references locally to enforce it to forget all the history it had of those files

现在是时候在本地更新 git 引用以强制它忘记这些文件的所有历史记录

rm -rf .git/refs/original/

git reflog expire --expire=now --all

git gc --prune=now

git gc --aggressive --prune=now

Now push all the changes to the remote repository with a FORCE to ensure the remote repo also clears all those files

现在使用 FORCE 将所有更改推送到远程存储库,以确保远程存储库也清除所有这些文件

git push --all --force

This would clean up the remote repository as if no file ever existed.

这将清理远程存储库,就好像没有文件存在一样。