獲取 git 倉庫通常有兩種方式
將尚未進行版本控制的本地目錄轉換為 git 倉庫;
從其它伺服器 轉殖 乙個已存在的 git 倉庫。
在已存在目錄中初始化倉庫
首先進入該專案目錄,之後執行
$ git init
該命令將建立乙個名為 .git 的子目錄,這個子目錄含有你初始化的 git 倉庫中所有的必須檔案,這些檔案是 git 倉庫的骨幹。
如果此專案中已有檔案,應該開始追蹤這些檔案並進行初始提交。通過 git add 命令來指定所需的檔案來進行追蹤,然後執行 git commit :
轉殖現有的倉庫$ git add *.c
$ git add license
$ git commit -m 'initial project version'
想獲得乙份已經存在了的 git 倉庫的拷貝就要用到轉殖倉庫的命令是 git clone 命令。當執行 git clone 命令的時候,預設配置下遠端 git 倉庫中的每乙個檔案的每乙個版本都將被拉取下來。
$ git clone
這會在當前目錄下建立乙個名為 「reading-notes」 的目錄,並在這個目錄下初始化乙個 .git 資料夾, 從遠端倉庫拉取下所有資料放入 .git 資料夾,然後從中讀取最新版本的檔案的拷貝。
如果想要自定義本地倉庫名字,使用額外引數指定目錄名
$ git clone qwer
目標目錄名被指定為 qwer
檔案狀態變化週期如圖
sequencediagram
participant ut as untracked
participant um as unmodified
participant mo as modified
participant st as staged
ut ->> st:add the file
um ->> mo:edit the file
mo ->> st:stage the file
um ->> ut:remove the file
st ->> um:commit
檢查檔案狀態
git status 檢查專案裡,檔案的狀態,當提交後且沒有新修改的時候如下
建立了乙個 1.txt 後,git 告訴你有乙個未跟蹤的檔案,但是不會自動跟蹤它$ git status
on branch master
nothing to commit, working tree clean
跟蹤新檔案$ git status
on branch master
untracked files:
(use "git add ..." to include in what will be committed)
1.txt
nothing added to commit but untracked files present (use "git add" to track)
git add 跟蹤新檔案或資料夾
$ git add 1.txt
再檢視檔案狀態,看到 1.txt 已被跟蹤,並處於暫存狀態
changes to be committed 這行下面的檔案都是處於 staged 狀態下$ git status
on branch master
changes to be committed:
(use "git restore --staged ..." to unstage)
new file: 1.txt
暫存已修改的檔案
修改了本檔案後,再執行 git status
說明追蹤檔案內容已經變化,但還沒有 staged ,執行 git add ,並檢視檔案狀態$ git status
on branch master
changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: 2 git 基礎.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git add 2\ git\ 基礎.md
但是如果這個時候又修改了檔案內容$ git status
on branch master
changes to be committed:
(use "git restore --staged ..." to unstage)
modified: 2 git 基礎.md
檔案同時出現在暫存區和非暫存區。 這怎麼可能呢? 好吧,實際上 git 只不過暫存了你執行 git add 命令時的版本。 如果你現在提交,檔案的版本是你最後一次執行 git add 命令時的那個版本,而不是你執行 git commit 時,在工作目錄中的當前版本。 所以,執行了 git add 之後又作了修訂的檔案,需要重新執行 git add 把最新版本重新暫存起來$ git status
on branch master
changes to be committed:
(use "git restore --staged ..." to unstage)
modified: 2 git 基礎.md
changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: 2 git 基礎.md
忽略檔案
有些檔案無需納入 git 管理,也不希望它們總出現在未跟蹤列表。可以建立乙個 .gitignore 檔案列出需要忽略的檔案
.gitignore 的格式規範如下:
檢視已暫存和未暫存的修改
想要知道具體修改了什麼地方,可以用 git diff 命令,此命令比較的是工作目錄中當前檔案和暫存區域快照之間的差異。 也就是修改之後還沒有暫存起來的變化內容。若要檢視已暫存的將要新增到下次提交裡的內容,可以用 git diff --staged ( --staged 和 --cached 同義) 命令。 這條命令將比對已暫存檔案與最後一次提交的檔案差異
提交更新
git commit 會啟動選擇的文字編輯器來輸入提交說明 ( 一般是 vim 或 emacs )
也可以在 commit 命令後新增 -m 選項,將提交資訊與命令放在同一行
跳過使用暫存區域
只要在提交的時候,給 git commit 加上 -a 選項,git 就會自動把所有已經跟蹤過的檔案暫存起來一併提交
$ git commit -a -m 'message'
移除檔案
要從 git 中移除某個檔案,就必須要從已跟蹤檔案清單中移除(確切地說,是從暫存區域移除),然後提交。 可以用 git rm 命令完成此項工作,並連帶從工作目錄中刪除指定的檔案,這樣以後就不會出現在未跟蹤檔案清單中了。
如果只是簡單地從工作目錄中手工刪除檔案,執行 git status 時就會在 「changes not staged for commit」 部分看到:$ git rm 1.txt
rm 'pro git/2 git 基礎/1.txt'
$ git status
on branch master
changes to be committed:
(use "git restore --staged ..." to unstage)
deleted: 1.txt
修改進入 staged 後,下一次提交時,該檔案就不再納入版本管理了。 如果要刪除之前修改過或已經放到暫存區的檔案,則必須使用強制刪除選項 -fchanges not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
deleted: 1.txt
想把檔案從暫存區和倉庫中刪除,而不影響工作目錄應該使用
git rm --cached
GIT學習筆記 命令基礎 記錄每次更新到倉庫
獲取git倉庫 在檔案目錄下開啟命令列,輸入 git init git add 新增當前檔案下的所有檔案到git暫存區 git add c 新增當前檔案下字尾為 c 的檔案到git暫存區 git commit m 注釋 將暫存區中的檔案提交到本地倉庫 git commit a m 注釋 跳過暫存操作...
Git 基礎 獲取 Git 倉庫
說明出處 基礎 獲取 git 倉庫 假如你只能閱讀一章來學習 git,本章就是你的不二選擇。本章內容涵蓋你在使用 git 完成各種工作中將要使用的各種基本命令。在學習完本章之後,你應該能夠配置並初始化乙個倉庫 repository 開始或停止跟蹤 track 檔案 暫存 stage 或提交 comm...
3 獲取git倉庫
有兩種取得 git 專案倉庫的方法。第一種是在現有專案或目錄下匯入所有檔案到 git 中 第二種是從乙個伺服器轉殖乙個現有的 git 倉庫。如果你打算使用 git 來對現有的專案進行管理,你只需要進入該專案目錄並輸入 git init 該命令將建立乙個名為.git的子目錄,這個子目錄含有你初始化的 ...