简体   繁体   English

如何将本地目录转换为Git,与远程仓库比较差异,然后提交?

[英]How to convert a local directory into Git, compare differences with remote repository, and then commit?

I've erroneously written changes into a copy of a remote repository that is no longer version controlled by Git.我错误地将更改写入了不再受 Git 版本控制的远程存储库的副本。 I need to convert this local directory back into a Git repository, compare differences with the remote branch, and then commit only those changes on top as you would normally.我需要将此本地目录转换回 Git 存储库,比较与远程分支的差异,然后像往常一样仅在顶部提交这些更改。

What is the best way of doing this?这样做的最佳方法是什么?

The easiest solution I would see is to use git diff by specified the actual files who changed.我看到的最简单的解决方案是通过指定更改的实际文件来使用git diff

Something along the line of:沿线的东西:

 git diff non_git_dir/the-files-*.js git-dir/the-files-*.js > patch.diff

If you have both directory on your disk and can relatively easily point the files you know have changed, it would generate a reasonably good diff file.如果您的磁盘上有这两个目录并且可以相对轻松地指出您知道已更改的文件,它将生成一个相当不错的差异文件。

Then you would just have to apply the diff on you git managed directory:然后你只需要在你的 git 管理目录上应用差异:

  git apply patch.diff

And from there you can commit or do what you want.从那里你可以提交或做你想做的事。


If however the changes are to broad or you can't have the files on your disk for any reason.但是,如果更改范围很广,或者由于任何原因您无法将文件保存在磁盘上。 Maybe you can try to:也许你可以尝试:

  • Make your old stuff managed by Git again: git init让你的旧东西再次由 Git 管理: git init
  • Commit everything: git add .提交一切: git add . git commit -m"ALL THE THINGS"
  • Add your remote repo: git remote add new git@github:newrepo.git添加你的远程git remote add new git@github:newrepo.gitgit remote add new git@github:newrepo.git
  • And then try to generate an acceptable diff: git diff master new/master然后尝试生成可接受的差异: git diff master new/master
  • Apply the diff is probably the cleanest solution anyway, as a merge would look very suspicious on the history无论如何,应用差异可能是最干净的解决方案,因为合并在历史上看起来非常可疑

Of course there is also the possibility to just copying the files over and check any potential code lost before committing.当然,也有可能只是复制文件并在提交之前检查任何潜在的代码丢失。

But it sounds like cheating但听起来像作弊


My two cents...我的两分钱...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM