git “重新初始化”存储库的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17357369/
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 16:26:42  来源:igfitidea点击:

The way to "reinit" repository

git

提问by Alan Coromano

I've made kind of a big refactoring in my project: I renamed files, removed, added... Besides, I added some folders in .gitignore. However, I've already made a commit to a remote repository before refactoring.

我在我的项目中进行了一次大的重构:我重命名了文件,删除了,添加了......此外,我在 .gitignore 中添加了一些文件夹。但是,在重构之前,我已经提交到远程存储库。

Is there any to make git "reinit" for my repository? If there is not, what should I do?

有没有可以为我的存储库制作 git“reinit”?如果没有,我该怎么办?

Update:

更新

I have deleted the folder .git and now I have an error of

我已经删除了文件夹 .git,现在我有一个错误

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.

提示:更新被拒绝,因为您当前分支的提示在提示后面:它的远程副本。合并远程更改(例如“git pull”)提示:再次推送之前。提示:有关详细信息,请参阅“git push --help”中的“关于快进的注意事项”。

I don't want to merge the local changes with the remote repo, I just want to push there, meaning completely replace the remote repository with the local one.

我不想将本地更改与远程存储库合并,我只想推送到那里,这意味着将远程存储库完全替换为本地存储库。

采纳答案by Klas Mellbourn

UPDATE

更新

If you have deleted the .gitfolder (not a great idea), you can create a new clone of the repo and move your stuff to that, and continue there. Something like this

如果您删除了该.git文件夹(不是一个好主意),您可以创建一个新的 repo 克隆并将您的内容移到该文件夹中,然后在那里继续。像这样的东西

cd ..
git clone <remote-repo-url> new-repo
rm -rf new-repo/*                          // this will not remove new-repo/.git
cp -f <original-local-repo> new-repo
cd new-repo

Then continue as below.

然后继续如下。

Note that it is better if you can restore the .gitfolder. Creating a new repo will loose all local repo information you had in the original local repo, like local branches that were never pushed.

请注意,如果您可以恢复.git文件夹,则更好。创建新的 repo 将丢失您在原始本地 repo 中拥有的所有本地 repo 信息,例如从未推送的本地分支。

END UPDATE

结束更新

You could

你可以

git reset --soft HEAD^
git add -A .
git commit -m "rewriting history"
git push --force origin master

This will back up to the previous commit (while preserving the working tree and index), commit your changes, and then force push that rewritten history to the remote.

这将备份到之前的提交(同时保留工作树和索引),提交您的更改,然后强制将重写的历史推送到远程。

push --forceis dangerous stuff. It will disturb others who have already pulled, and is considered very rude, but if no one else have started work on it, that is not a problem.

push --force是危险的东西。会打扰已经拉过的人,被认为很不礼貌,但如果没有其他人动手,那也不是问题。

This is what is happening:

这是正在发生的事情:

--- A -- B  master

    ^
    |     
    origin/master

git push

--- A -- B master, origin/master

git reset HEAD^
git commit -am "rewriting"

--- A -- B origin/master
     \
      \
       B' master

git push --force

--- A -- B
     \
      \
       B' master, origin/master

回答by Khachornchit Songsaen

Here are steps to re-init repository.

以下是重新初始化存储库的步骤。

Scenario:I have deleted a locally project folder that I have made git init previously. After that, I would like to update to the existing git repository with a new folder of same project. Then I solved it by following procedure.

场景:我已经删除了我之前创建的 git init 的本地项目文件夹。之后,我想使用同一项目的新文件夹更新到现有的 git 存储库。然后我通过以下程序解决了它。

Procedure

程序

git init
git add --all
git commit -am "re-init"
git remote add origin https://github.com/{yourgit}/{yourproject}.git
git push --force origin master

回答by tadman

The lazy way is to just add all the new files and changes, and deletions:

懒惰的方法是只添加所有新文件和更改,以及删除:

git add -A .

So long as you don't have any conflicts, this should merge in fine.

只要您没有任何冲突,这应该可以很好地合并。