簡體   English   中英

GIT:簽出到特定文件夾

[英]GIT: Checkout to a specific folder

我想使用類似於:

git checkout -- <path>/<file>

但我想將文件檢出到我選擇的某個文件夾,而不是覆蓋本地<path>/<file>

任何的想法?

另一個更簡潔的解決方案 - 只需指定一個不同的工作樹。

檢出從 HEAD(不是索引)到特定輸出目錄的所有內容:

git --work-tree=/path/to/outputdir checkout HEAD -- .

要將子目錄或文件從 HEAD 簽出到特定目錄:

git --work-tree=/path/to/outputdir checkout HEAD -- subdirname

根據做一個“git export”(比如“svn export”)?

你可以使用git checkout-index ,這是一個低級命令,如果你想導出所有內容,你可以使用-a

git checkout-index -a -f --prefix=/destination/path/

引用手冊頁:

最后的“/”[在前綴上] 很重要。 導出的名稱實際上只是以指定的字符串為前綴。

如果要導出某個目錄,則涉及一些技巧。 該命令只需要文件,而不是目錄。 要將其應用於目錄,請使用“find”命令並將輸出通過管道傳輸到 git。

find dirname -print0 | git checkout-index --prefix=/path-to/dest/ -f -z --stdin

同樣來自手冊頁:

直覺不是這里的目標。 重復性是。

如果您正在使用您的功能並且不想結帳回到主人,您可以運行:

cd ./myrepo

git worktree add ../myrepo_master master

git worktree remove ../myrepo_master

它將創建../myrepo_master目錄和master分支提交,您可以在其中繼續工作

對於單個文件:

git show HEAD:abspath/to/file > file.copy

上述解決方案對我不起作用,因為我需要檢查樹的特定標記版本。 順便說一下,這就是cvs export用途。 git checkout-index不接受 tag 參數,因為它從 index.js 中檢出文件。 git checkout <tag>無論工作樹如何都會更改索引,因此我需要重置原始樹。 對我有用的解決方案是克隆存儲庫。 共享克隆速度非常快,並且不會占用太多額外空間。 如果需要,可以刪除.git目錄。

git clone --shared --no-checkout <repository> <destination>
cd <destination>
git checkout <tag>
rm -rf .git

較新版本的 git 應該支持git clone --branch <tag>來自動檢出指定的標簽:

git clone --shared --branch <tag> <repository> <destination>
rm -rf <destination>/.git

Adrian 的回答是“致命的:此操作必須在工作樹中運行”。 以下是對我們有用的內容。

git worktree add <new-dir> --no-checkout --detach
cd <new-dir>
git checkout <some-ref> -- <existing-dir>

注意事項:

  • --no-checkout不要將任何東西簽出到新的工作樹中。
  • --detach不要為新工作樹創建新分支。
  • <some-ref>適用於任何 ref,例如,它適用於HEAD~1
  • 使用git worktree prune

除了@hasen 的回答 您可能想使用git ls-files而不是find來列出要結帳的文件,例如:

git ls-files -z *.txt | git checkout-index --prefix=/path-to/dest/ -f -z --stdin

git ls-files忽略未提交的文件。

我正在使用這個別名來檢出臨時目錄中的一個分支:

[alias]
    cot = "!TEMP=$(mktemp -d); f() { git worktree prune && git worktree add $TEMP $1 && zsh -c \"cd $TEMP; zsh\";}; f" # checkout branch in temporary directory

用法:

git cot mybranch

然后,您將被放置在臨時目錄中的一個新 shell 中,您可以在該目錄中處理分支。 您甚至可以在此目錄中使用 git 命令。

完成后,刪除目錄並運行:

git worktree prune

在添加新工作樹之前,這也在別名中自動完成。

使用git archive branch-index | tar -x -C your-folder-on-PC git archive branch-index | tar -x -C your-folder-on-PC將分支克隆到另一個文件夾。 我想,然后你可以復制你需要的任何文件

我定義了一個 git 別名來實現這一點(在我發現這個問題之前)。

這是一個簡短的 bash 函數,它保存當前路徑、切換到 git 存儲庫、進行結帳並返回它開始的位置。

git checkto 開發 ~/my_project_git

例如,這會將開發分支檢出到“~/my_project_git”目錄中。

這是~/.gitconfig的別名代碼:

[alias]
    checkTo = "!f(){ [ -z \"$1\" ] && echo \"Need to specify branch.\" && \
               exit 1; [ -z \"$2\" ] && echo \"Need to specify target\
               dir\" && exit 2; cDir=\"$(pwd)\"; cd \"$2\"; \
               git checkout \"$1\"; cd \"$cDir\"; };f"

如果你在mydir工作,為什么不簡單地cp -r mydir mydir2然后cd mydir2; git checkout commit# cd mydir2; git checkout commit#

暫無
暫無

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

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