簡體   English   中英

如何在git repo的第一次提交中添加文件?

[英]How to add a file to the first commit on a git repo?

我一直在將舊的SVN回購轉換為GIT。 我已經獲得了轉換的分支和標簽。 包括在GIT中制作SVN標簽真實標簽。

但是,我想在新創建的GIT倉庫的第一次提交中添加一個gitignore。 我希望它好像文件一直在那里。 因此,所有后面的提交(在主服務器或分支機構中)現在將具有此文件,因為父樹將返回第一個提交。

似乎我需要某種形式的git rebase或git filter-branch --tree-filter。 我已經嘗試了其中的每一個,但我最終斷開了分支機構。

如果將來有人需要這樣做,這是一個更簡單的解決方案。 我最近不得不修改早期提交,因為提交消息中存在問題拼寫錯誤。 這適用於更改先前提交的任何內容。 在這里修改了Charles Bailey的答案。

# Go back to the commit you want to change (detach HEAD)
git checkout <sha1_for_commit_to_change>

# Make any changes now (add your new file) then add them to the index
git add <new_files>

# amend the current commit
git commit --amend

# temporarily tag this new commit
# (or you could remember the new commit sha1 manually)
git tag tmp

# go back to the original branch (assume master for this example)
git checkout master

# Replay all the commits after the change onto the new initial commit
git rebase --preserve-merges --onto tmp <sha1_for_commit_to_change>

# remove the temporary tag
git tag -d tmp

請注意,這將在修改后的提交后更改所有提交ID,因此不建議在公共存儲庫中使用。 此外,您還必須重新創建所有標記

如果您願意,可以在干凈的工作樹上,在新克隆上執行此操作:

$ git checkout -b withgitignore $firstcommithash
$ git add .gitignore
$ git commit --amend
$ for branch in branch1 branch2 branch3 ... ; do
      git checkout $branch
      git rebase withgitignore
   done

未經測試;)

假設這是一個全新的重建,那么一個技巧是在第一次提交時啟動一個分支並在那里添加gitignore。 您現在可以在該分支的頭部進行“正確”提交。 現在可以使用grafts設施來重新排序感知的提交序列。 普通的git filter-branch將使其永久化,盡管舊的層次結構仍將列在refs \\ info(IIRC)中的originals下。

雖然這將創建包含gitignore文件的早期提交,但所有其他提交仍將沒有gitignore文件。

編輯:---------- git-filter-branch ,舉例:

git filter-branch --tree-filter 'rm filename' HEAD--index-filter variant。

你可以簡單地add .gitignore你的.gitignore而不是刪除,並使用--index-filter這樣你的工作樹仍然可以完整復制。

首先確保你有備份! 報告它是否適合您。

暫無
暫無

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

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