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?

Sergey Senkov
  • 1,490
  • 1
  • 13
  • 23
  • Ctrl+C, Ctrl+V? – CodeCaster Jan 29 '18 at 08:13
  • They are at different levels of project. By hand it should take hours to find them. – Sergey Senkov Jan 29 '18 at 08:14
  • 4
    Why? What exactly is your goal? Do you just want to copy the unversioned files? Can't you copy the entire project directory? – CodeCaster Jan 29 '18 at 08:16
  • 2
    See also [Git command to show which specific files are ignored by .gitignore](https://stackoverflow.com/questions/466764/git-command-to-show-which-specific-files-are-ignored-by-gitignore) and [Git: list only “untracked” files (also, custom commands)](https://stackoverflow.com/questions/3801321/git-list-only-untracked-files-also-custom-commands), you can feed that output to a copy command. – CodeCaster Jan 29 '18 at 08:18
  • It`s possible, but then I need to store second copy of project instead of move 400 untracked files to separate folder. – Sergey Senkov Jan 29 '18 at 08:21
  • So? Disk space is extremely cheap, cheaper than your time. Anyway see previous comment. – CodeCaster Jan 29 '18 at 08:22
  • Create a new branch where you can commit everything you don't need. So it will be backed up and not seen in your master branch(this is a workaround, not the right way to use git) – Saifer Jan 29 '18 at 08:38
  • 1
    Make a copy of your entire folder, in the copy you can remove the .git folder but keep everything else, tracked and untracked alike. You only have to dig in this folder if you actually need some of the files later and then you know which files to dig for. As is said, disk space is cheap so simply zip up the copy and archive it. – Lasse V. Karlsen Jan 29 '18 at 08:48
  • 2
    **See Also** [How to make quick backup of untracked files which I want to delete by git clean?](https://stackoverflow.com/q/5205305/1366033) – KyleMit Aug 05 '20 at 00:33

5 Answers5

26

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
Flows
  • 3,675
  • 3
  • 28
  • 52
  • 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` – Sergey Senkov Feb 05 '18 at 10:34
  • Do you have any ideas to upgrade script? – Sergey Senkov Feb 05 '18 at 10:47
  • I updated my answer. You have to specify the delimiter with IFS bash variable. – Flows Feb 05 '18 at 15:33
  • filenames with spaces can also be handled by just quoting the command substitution: `"$(git ls-files ...)"` – scry May 31 '23 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.

phd
  • 82,685
  • 13
  • 120
  • 165
  • 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) ` – Sergey Senkov Feb 05 '18 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. 
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
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.
KyleMit
  • 30,350
  • 66
  • 462
  • 664