Setup and Config
Getting and Creating Projects
Basic Snapshotting
Branching and Merging
Sharing and Updating Projects
Inspection and Comparison
Patching
Debugging
External Systems
Server Admin
Guides
- gitattributes
- Command-line interface conventions
- Everyday Git
- Frequently Asked Questions (FAQ)
- Glossary
- Hooks
- gitignore
- gitmodules
- Revisions
- Submodules
- Tutorial
- Workflows
- All guides...
Administration
Plumbing Commands
- 2.52.0 2025年11月17日
- 2.48.1 → 2.51.2 no changes
- 2.48.0 2025年01月10日
- 2.47.1 → 2.47.3 no changes
- 2.47.0 2024年10月06日
- 2.45.1 → 2.46.4 no changes
- 2.45.0 2024年04月29日
- 2.44.1 → 2.44.4 no changes
- 2.44.0 2024年02月23日
- 2.43.1 → 2.43.7 no changes
- 2.43.0 2023年11月20日
- 2.42.1 → 2.42.4 no changes
- 2.42.0 2023年08月21日
- 2.38.1 → 2.41.3 no changes
- 2.38.0 2022年10月02日
- 2.35.1 → 2.37.7 no changes
- 2.35.0 2022年01月24日
- 2.30.1 → 2.34.8 no changes
- 2.30.0 2020年12月27日
- 2.29.1 → 2.29.3 no changes
- 2.29.0 2020年10月19日
- 2.28.1 no changes
- 2.28.0 2020年07月27日
- 2.27.1 no changes
- 2.27.0 2020年06月01日
- 2.21.1 → 2.26.3 no changes
- 2.21.0 2019年02月24日
- 2.13.7 → 2.20.5 no changes
- 2.12.5 2017年09月22日
- 2.10.5 → 2.11.4 no changes
- 2.9.5 2017年07月30日
- 2.5.6 → 2.8.6 no changes
- 2.4.12 2017年05月05日
- 2.3.10 no changes
- 2.2.3 2015年09月04日
- 2.1.4 no changes
- 2.0.5 2014年12月17日
名稱
git-init -創建一個空的Git倉儲或是重新初始化已存在的倉儲
格式
git init [-q | --quiet] [--bare] [--template=<模板目錄>] [--separate-git-dir <git 目錄>] [--object-format=<格式>] [-b <分支名稱> | --initial-branch=<分支名稱>] [--shared[=<權限>]] [目錄]
說明
這行指令會創建一個空的Git倉儲,基本上是一個名為`.git`的目錄,含有`objects`,refs/heads,`refs/tags`這幾個子目錄,其他模板檔案,跟一個指向`master`分支Head的初始`HEAD`檔案。
如果有設定環境變數`$GIT_DIR`,則會使用被指定的路徑,而不是`./.git`來建立倉儲的基礎檔案。
如果object儲存路徑有經由環境變數`$GIT_OBJECT_DIRECTORY`所指定,則SHA-1的目錄將會被設置在其底下,否則會使用預設的`$GIT_DIR/objects`目錄。
在現有的倉儲執行’git init’是很安全的。它並不會覆蓋掉已存在的檔案。重新執行’git init’最主要的原因,是去取得新增的模板檔案(或是加上 --separate-git-dir 將倉儲移到另一個位置)。
選項
- -q
- --quiet
-
只會印出錯誤和警告訊息;其他輸出訊息皆會被遮蔽。
- --bare
-
創建一個裸倉儲。如果環境變數`GIT_DIR`未被設定 ,則會設置在當前作業目錄下。
- --object-format=<格式>
-
指定倉庫的物件格式(雜湊演算法)。可以指定為’sha1’或是’sha256'(有啟用的話)。預設值為’sha1'。
Missing zh_HANT/object-format-disclaimer.txt
See original version for this content.
- --template=<template_directory>
-
指定用於取得模板檔案的目錄(請參考下面的"模板檔案目錄"章節。)
- --separate-git-dir=<git dir>
-
不使用`$GIT_DIR`或是`./.git/`作為初始化倉儲的路徑,而是創建一個儲存實際倉儲路徑的檔案。這個檔案會作為資料系統獨立的Git捷徑,連結到倉儲。
如果重新初始化,倉儲則會被移到指定的路徑。
- -b <branch-name>
- --initial-branch=<分支名稱>
-
為新創的倉儲指定初始分支的名稱。若沒有指定,就會退而使用預設名稱’master'。
-
指定的Git倉儲可在多個使用者之間共享,這會允許屬於相同群組的使用者對這個倉儲推送。指定時,組態參數"core.sharedRepository"將會被設定,讓在`$GIT_DIR`底下的檔案和目錄使用指定的權限來創建。當沒有指定時,Git會使用umask(2)所回報的權限。
這個選項可以加上下列數值,若不給值,則預設會使用’group':
- umask (或是 false)
-
使用umask(2)回報的權限。也是當沒有指定`--shared`時的預設值。
- group (或是 true)
-
讓倉儲變成群組可寫入(還有g+sx,因為git群組可能並非所有使用者的主群組)。這用於放寬原本較為安全的umask(2)權限。請注意,umask仍然會適用於其他權限(舉例來說, 當umask是'0022’時,使用’group’並不會移除其他使用者(non-group)的讀取權限。請參閱'0xxx’來知道如何精確指定倉庫的權限。
- all (或 world 或是 everybody)
-
跟’group’大致相同,但會使倉庫可被所有使用者讀取。
- 0xxx
-
0xxx 是個八進位數字,而各個檔案都會擁有權限'0xxx'。'0xxx’會覆蓋掉使用者的umask(2)值(而且並不像group跟all一樣會放寬權限)。'0640’會創建群組可讀,但群組不可寫,其他人也不可存取的倉儲。'0660’會創建當前使用者與群組可讀可寫,但是其他人無法存取的倉儲。
在預設中,共享倉庫的設定旗標 receive.denyNonFastForwards是被啟用的,所以你無法強制使用非快進進行推送。
如果你在指令中提供了’目錄',而此目錄不存在的話,它則會被創建。
模版檔案目錄
並非以點作為名稱開頭的檔案和目錄將會在`$GIT_DIR`被創建後,被複製到其中。
模板目錄將為下列其一(依照順序):
-
在選項`--template`中被給予的引數;
-
環境變數`$GIT_TEMPLATE_DIR`中的內容;
-
設定變數`init.templateDir`; 或是
-
預設模板目錄:
/usr/share/git-core/templates。
The default template directory includes some directory structure, suggested "exclude patterns" (see gitignore[5]), and sample hook files.
The sample hooks are all disabled by default. To enable one of the sample hooks rename it by removing its .sample suffix.
See githooks[5] for more general info on hook execution.
範例
- Start a new Git repository for an existing code base
-
$ cd /path/to/my/codebase $ git init (1) $ git add . (2) $ git commit (3)
-
Create a /path/to/my/codebase/.git directory.
-
Add all existing files to the index.
-
Record the pristine state as the first commit in the history.
-
GIT
Part of the git[1] suite