简体   繁体   English

如何不将本地删除的文件提交到git

[英]How to NOT commit locally deleted files to git

We have a situation where were are using git to stash myriad scans (images) and do not wish to preserve them on the local machine once set on up; 我们遇到的情况是,使用git来存储大量扫描(图像),并且不希望在设置好之后将其保存在本地计算机上; however, git is seeing each local deletion (following this process) as something to be committed up to the repo, and we want that not to happen. 但是,git将每个本地删除(遵循此过程)视为要提交给存储库的内容,我们希望这种情况不会发生。 Any ideas on how to commit only the additional files and never any deletions? 关于如何仅提交其他文件而不删除任何内容的任何想法?

Use the Skip-Worktree Bit 使用跳过工作树位

The git-update-index(1) command provides some ways of dealing with this. git-update-index(1)命令提供了一些解决方法。 Based on the assumption that you want to keep the blobs in your history while deleting them from your working tree, you could use the following: 基于您要在将斑点从工作树中删除时将其保留在历史记录中的假设,可以使用以下命令:

git add --all
git commit --message="Add new assets to repository."
git update-index update-index --skip-worktree <files ...>
rm <files ...>

Note that the last line isn't a Git command because you're removing the files from the working tree in the shell after you've stored them in the index. 请注意,最后一行不是Git命令,因为将文件存储在索引中后,您将从外壳中的工作树删除文件。 Don't use git rm by mistake, because it operates on the index. 不要错误地使用git rm ,因为它在索引上运行。

Listing Your Special Bits and Index-Only Files 列出您的特殊位和仅索引文件

To see the files you've marked with the skip-worktree bit: 要查看已用skip-worktree位标记的文件:

git ls-files -v | awk -v IGNORECASE=1 '$1 ~ /s/ {print}'

You can also use git-ls-files(1) to find files in the index that have been removed from your working tree. 您还可以使用git-ls-files(1)在索引中查找已从工作树中删除的文件。

For each file to be deleted, do this: 对于每个要删除的文件,执行以下操作:

git update-index --assume-unchanged <file>

This should stop Git from asking you to commit the deletion. 这应该使Git不再要求您提交删除操作。

That being said, perhaps a better solution would be to use SFTP with permissions set to disallow deleting or modifying files once uploaded. 话虽这么说,也许更好的解决方案是使用SFTP,并将其权限设置为禁止上传后删除或修改文件。

(edit: package up the payload as a self-contained script) (编辑:将有效载荷打包为独立脚本)

You're after a nearly status-only local repository that tracks the existence of everything you've ever created but keeps the contents of only the most recent stuff (after pushing everything upstream). 您正在使用一个几乎仅状态的本地存储库,该存储库跟踪您曾经创建的所有内容的存在,但仅保留最新内容的内容(将所有内容推送到上游之后)。 Presuming you never reuse a pathname, here's the ritual: 假设您永远不会重复使用路径名,那么请注意以下习惯:

One-time setup: 一次性设置:

Create and push an everything branch that tracks every image you've got so far. 创建并推送一个everything分支,该分支跟踪您到目前为止获得的每个图像。 Base a (local-only) worktree branch off that. worktree基础(仅本地)的工作worktree分支。

git checkout -b everything     # this will be the branch you push
git push origin everything     # .
git checkout -b worktree       # you'll stay on this branch

Work (almost) as if you didn't want to do anything special: 工作(几乎)就像您不想做任何特殊的事情一样:

Everything that needs preserving upstream is on the everything branch, and you've got worktree checked out. 需要在上游保留的everythingeverything分支上,并且您已检出工作worktree Remove images you're done with from the worktree, make new images there that need pushing, and commit the new worktree state: 从工作树中删除完成的图像,在其中制作需要推送的新图像,然后提交新的工作树状态:

# work work:
rm -r anything/ you\'re/ done with
create new images
git add -A .  # good gitignore patterns make life easy
git commit

To do the requested work, run 要执行所需的工作,请运行

merge-push-and-cleanup   # the script below

After which everything's stored upstream, nothing redundant remains locally, and you're ready for more work. 之后,所有内容都存储在上游,本地没有多余的资源,您可以准备进行更多工作。


The merge-push-and-cleanup script: merge-push-and-cleanup脚本:

#!/bin/sh
# Git can do "index-only" merges when the content doesn't need to be examined,
# you can casually create sideband indexes, and `git commit-tree` works on any 
# tree in the repo.

  (
# ^ subshell to keep G_I_F export local
  export GIT_INDEX_FILE=.git/sideband-index

  # make a merged index that includes everything from both branches, by
  # starting from an empty merge base so all files show up as additions.

  git read-tree -im $(git mktree </dev/null) worktree everything

  # commit to the `everything` branch straight from the constructed index,
  # reusing the commit message from `worktree`

  git cat-file -p worktree | sed 1,/^$/d \
  | git commit-tree -p everything -p worktree $(git write-tree --missing-ok) \
  | xargs git update-ref refs/heads/everything
)

git push origin everything

# preserve the branch metadata and, just for safety, your latest images
# by packing it all up:

mkdir -p .git/preserved/pack
rm -f .git/preserved/pack/*
(
  git rev-list worktree everything
  git rev-parse worktree^{tree} everything^{tree}
  git ls-tree -rt worktree | awk '{ print $3 }'
  git ls-tree -rt everything | awk '$2 != "blob" { print $3 }'
) \
| git pack-objects .git/preserved/pack/pack

# now for the fun one:
rm -rf .git/objects/*   # !!!

# put the preserved stuff back:
mv .git/preserved/pack .git/objects

Just add images to gitignore. 只需将图像添加到gitignore。 You do not need to store them in repo, right? 您不需要将它们存储在仓库中,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM