What's the best practice to “git clone” into an existing folder?

后端 未结 15 1734
情书的邮戳
情书的邮戳 2020-11-28 17:19

I have a working copy of the project, without any source control meta data. Now, I\'d like to do the equivalent of git-clone into this folder, and keep my local changes.

15条回答
  •  感动是毒
    2020-11-28 17:52

    There are two approaches to this. Where possible I would start with a clean folder for your new git working directory and then copy your version of things in later. This might look something like*:

    mv $dir $dir.orig
    git clone $url $dir
    rsync -av --delete --exclude '.git' $dir.orig/ $dir/
    rm -rf $dir.orig
    

    At this point you should have a pretty clean working copy with your previous working folder as the current working directory so any changes include file deletions will show up on the radar if you run git status.

    On the other hand if you really must do it the other way around, you can get the same result with something like this:

    cd $dir
    git clone --no-checkout $url tempdir
    mv tempdir/.git .
    rmdir tempdir
    git reset --mixed HEAD
    

    Either way, the first thing I would do is run something like git stash to get a copy of all your local changes set aside, then you can re-apply them and work through which ones you want to get committed.

    * Both examples assume you start out on the shell in the parent directory of your project.

提交回复
热议问题