8

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?

9

5 Answers 5

27

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
4
  • Perfect! Only issue is that trick failed on names with space like 20150409_2 Report.txt. It think that there are 2 different files : ` mv: cannot stat '20150409_2': No such file or directory mv: cannot stat 'Report.txt': No such file or directory` Commented Feb 5, 2018 at 10:34
  • Do you have any ideas to upgrade script? Commented Feb 5, 2018 at 10:47
  • I updated my answer. You have to specify the delimiter with IFS bash variable.
    – Flows
    Commented Feb 5, 2018 at 15:33
  • filenames with spaces can also be handled by just quoting the command substitution: "$(git ls-files ...)"
    – scry
    Commented May 31, 2023 at 14:51
9
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.

1
  • 1
    This variant also works, but has same issue. Tried "rsync -R git ls-files --others --exclude-standard ../Backup". Failed on names with space like 20150409_2 Report.txt. It think that there are 2 different files : ` rsync: link_stat "20150409_2" failed: Bad file number (9) rsync: link_stat "Report.txt" failed: Bad file number (9) ` Commented Feb 5, 2018 at 12:16
1

You can keep those changes to another branch as backup branch or you may keep these in another folder switching to backup branch its up to you but I won't recommend making a extra folder for that.

You didn't want to stash them, but to move these changes you need to stash them, these won't be in your current branch if you follow bellow procedure-

In your current branch-

git add . 
git checkout -b backup

By this you have moved with all the stashed changes in backup branch Make a commit here -

git commit -m "backup"

Now you have all the changes in backup branch You can go back your working/master branch without those changes-

git checkout -b master
0

The previous answer could copy things that may already be in .gitignore but let's say you just have some scratch files in your current repo and you just want to save them somewhere like ./private you can do something like this.

mkdir private/scratch_files_issue123/
rsync $(git status -u -s | awk '{print $2}')
git clean -df # to remove the unstanged files. 
0

From Mark Longair's answer to How to make quick backup of untracked files which I want to delete by git clean?:

The following command will create a tar archive in your home directory of all of the untracked (and not ignored) files in your directory:

git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.tar

If you're going to use this technique, check carefully that git ls-files --others --exclude-standard on its own produces the list of files you expect!

A few notes on this solution might be in order:

  • I've used -z to get git ls-files to output the list of files with NUL (a zero byte) as the separator between files, and the -0 parameter to xargs tells it to consider NUL to be the separator between the parameters it reads from standard input. This is a standard trick to deal with the possibility that a filename might contain a newline, since the only two bytes that aren't allowed in filenames on Linux are NUL and /.
  • If you have a huge number of untracked files then xargs will run the tar command more than once, so it's important that I've told tar to append files (r) rather than create a new archive (c), otherwise the later invocations of tar will overwrite the archive created just before.

Not the answer you're looking for? Browse other questions tagged or ask your own question.