Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git move all untracked files and folders to directory out of git? [duplicate]

Tags:

git

I got PC from previous developer. There are many untracked files and folders (>400) in git. Project should work without them, but I don't want to delete or stash them. Maybe I'll have some usage later.

Is there way to move them with structure to Backup folder out of git?

like image 803
Sergey Senkov Avatar asked Jan 29 '18 08:01

Sergey Senkov


2 Answers

Based on this answer, you can do something like follow assuming your can use bash command (I guess you can do this in git prompt).

cd $Git_Repo
for file in $(git ls-files --others --exclude-standard); do mkdir -p ../backup/$(dirname $file) ; mv $file ../backup/$file ; done

The last command loops on all untracked files, creates structure (dirname) in backup destination folder file. It moves file where it needs.

NOTE: If you have filenames with spaces, you have to specify the delimiter IFS for bash during your command (source). Do not forget to unset this after. The command becomes

cd $Git_Repo
IFS=$'\n'
for file in $(git ls-files --others --exclude-standard); do mkdir -p ../backup/$(dirname $file) ; mv $file ../backup/$file ; done
unset IFS
like image 121
Flows Avatar answered Sep 28 '22 06:09

Flows


rsync -R `git ls-files --others` ../Backup

git ls-files --others lists untracked files including ignored; if you want to exclude files listed in .gitignore add --exclude-standard.

rsync -R copies the listed files into ../Backup directory preserving paths.

After that cleanup worktree with git clean -fd or even git clean -fdx.

like image 28
phd Avatar answered Sep 28 '22 07:09

phd