簡體   English   中英

如何不將本地刪除的文件提交到git

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

我們遇到的情況是,使用git來存儲大量掃描(圖像),並且不希望在設置好之后將其保存在本地計算機上; 但是,git將每個本地刪除(遵循此過程)視為要提交給存儲庫的內容,我們希望這種情況不會發生。 關於如何僅提交其他文件而不刪除任何內容的任何想法?

使用跳過工作樹位

git-update-index(1)命令提供了一些解決方法。 基於您要在將斑點從工作樹中刪除時將其保留在歷史記錄中的假設,可以使用以下命令:

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

請注意,最后一行不是Git命令,因為將文件存儲在索引中后,您將從外殼中的工作樹刪除文件。 不要錯誤地使用git rm ,因為它在索引上運行。

列出您的特殊位和僅索引文件

要查看已用skip-worktree位標記的文件:

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

您還可以使用git-ls-files(1)在索引中查找已從工作樹中刪除的文件。

對於每個要刪除的文件,執行以下操作:

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

這應該使Git不再要求您提交刪除操作。

話雖這么說,也許更好的解決方案是使用SFTP,並將其權限設置為禁止上傳后刪除或修改文件。

(編輯:將有效載荷打包為獨立腳本)

您正在使用一個幾乎僅狀態的本地存儲庫,該存儲庫跟蹤您曾經創建的所有內容的存在,但僅保留最新內容的內容(將所有內容推送到上游之后)。 假設您永遠不會重復使用路徑名,那么請注意以下習慣:

一次性設置:

創建並推送一個everything分支,該分支跟蹤您到目前為止獲得的每個圖像。 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

工作(幾乎)就像您不想做任何特殊的事情一樣:

需要在上游保留的everythingeverything分支上,並且您已檢出工作worktree 從工作樹中刪除完成的圖像,在其中制作需要推送的新圖像,然后提交新的工作樹狀態:

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

要執行所需的工作,請運行

merge-push-and-cleanup   # the script below

之后,所有內容都存儲在上游,本地沒有多余的資源,您可以准備進行更多工作。


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

只需將圖像添加到gitignore。 您不需要將它們存儲在倉庫中,對嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM