问题
I have a repository that I created early in the learning curve of a project. As I've learned more, I've realized that this is creating problems because some of the files I want tracked aren't required in the compiled source. What I'd like to do is create a new "project" parent directory, move the existing source directory into that parent directory and the migrate the files that don't need to be compiled into that parent directory.
Is that possible with git? Will I completely destroy all of the references since I'll want to move the .git/ directory up to the parent level as well?
Thanks.
回答1:
One option would be to just move things around within your current repository.
For example, assuming something like:
/.git
/lib/lib.c
/lib/lib.h
/test.c
/readme.txt
You could create a new folder (src below) and move the lib folder and the test.c folder into the new one (using git mv). Then the current directory will be your "project" parent directory that you want, and all the code will be in the new folder.
/.git
/src/lib/lib.c
/src/lib/lib.h
/src/test.c
/readme.txt
An alternative would be git filter-branch, as pointed out by VonC in the comments. Using that I believe you could build a restructured repository that would not have a commit for the moves, the files would just always have existed in the new structure.
回答2:
In agreement with Chris Shaffer, you can use the following script to accomplish those steps:
fullpath=`pwd`
dirname=`basename $fullpath`
if [ ! -d ./.git ]; then
echo 'Working directory contains no .git repository. There is nothing to move.'
elif [ -d ../.git ]; then
echo 'Parent directory already contains a .git repository. Aborting.'
elif [ -d $dirname ]; then
echo "There's already a $dirname directory. Aborting to be safe."
else # Enough checking; let's get some work done...
mkdir $dirname
shopt -s extglob
for file in `ls -a | egrep -v [.]git\|^[.]+\$\|^$dirname\$`; do
git mv $file $dirname/
done
mv .git* ..
mv $dirname/* .
mv html/.[a-z]* .
git commit -m "Moved Git repository up one directory"
rmdir $dirname
fi
This might be a little more checking than is needed for a one-off situation, but it should do the job. You can adapt it by hand, or save it as an executable script and then call it from the directory that contains .git.
Apparently others have been able to just mv .git .., but that did not work for me. I have to first perform all the git mv file operations, and then move everything up one.
If I make this any more robust in the future, I'll post updates at https://github.com/jcamenisch/dotfiles/blob/master/bin/mv-git-up .
来源:https://stackoverflow.com/questions/3152550/moving-a-git-directory