简体   繁体   中英

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. 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.

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.

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 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
  • Commit everything: git add . git commit -m"ALL THE THINGS"
  • Add your remote repo: git remote add new git@github:newrepo.git
  • And then try to generate an acceptable diff: 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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