git提交版本-git基礎(七)


之前的文章主要講述了如何追蹤文件,以及文件的狀態、文件所在區域的流轉。現在是最終的對文件進行版本控制了。
之前新建或修改后的文件,我們都add進了暫存區,文件還沒有提交到版本倉庫。現在我們就進行正式的版本控制吧。

一、提交版本

1. 提交版本命令 git commit -m <message>
message是對本次提交的備注信息,是必填項。
在提交之前,必須確認還有什么已修改或新建的文件還沒有 git add 過, 否則提交的時候不會記錄這些尚未暫存的變化。
這些已修改但未暫存的文件保留在本地磁盤。 所以每次准備提交前,先用 git status 命令看下,你所需要的文件是不是都已暫存起來了。
提交版本,是將暫存區的文件,提交進入倉庫。
執行命令提交我們在工作區中的兩個已暫存文件

$ git commit -m 初次提交
[master (root-commit) 337070e] 初次提交
 2 files changed, 1 insertion(+)
 create mode 100644 .gitignore
 create mode 100644 hello.txt


hello.txt的內容為 hello ,.gitignore文件的內容為空。

現在你已經成功的創建了項目的第一個版本。

注意:提交時的文件是放在暫存區域的。 任何還未暫存的文件仍然保持已修改狀態,可以在下次提交時納入版本管理。 每一次運行提交操作,都是對你項目作一次版本,以后可以回到這個版本,或者進行比較。


2. 跳過使用暫存區域
命令 git commit -a -m <message>
給 git commit 加上 -a 選項,Git 就會自動把所有已經跟蹤過的文件暫存起來一並提交,從而跳過 git add 步驟。
注意是針對已追蹤的文件,如果是新建文件,還是執行 add 操作的。
實例:
此時 hello.txt 是已追蹤的,我們修改內容為 hello,並新建一個 world.txt 文件。
執行 git commit -a -m 第二次提交跳過暫存區。

先查看狀態,再提交后查看狀態:

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage) modified: hello.txt Untracked files: (use "git add <file>..." to include in what will be committed) world.txt $ git commit -a -m 第二次提交跳過暫存區 [master 84e27d4] 第二次提交跳過暫存區 1 file changed, 1 insertion(+), 1 deletion(-) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) world.txt nothing added to commit but untracked files present (use "git add" to track) 


新建的 world.txt 未追蹤,在工作區。hello.txt 是已追蹤文件。 執行提交后,只有暫存區的文件進入倉庫了。


3.移除文件(刪除文件)
git rm 命令
所謂的移除文件或刪除文件,是指從暫存區或工作區中刪除。
執行 git rm 命令,會從暫存區,並連帶從工作區中刪除文件,這樣以后就不會出現在未跟蹤文件清單中了。刪除完后需要提交版本。記住所有提交了版本的代碼都是可以恢復的。
這個命令刪除暫存區和工作區中的文件,即已經追蹤的文件。
實踐:
執行前:

git status :

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed) world.txt nothing added to commit but untracked files present (use "git add" to track)


此時我們的 hello.txt 已經追蹤了,執行刪除命令。
執行后:

$ git rm hello.txt
rm 'hello.txt' $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: hello.txt Untracked files: (use "git add <file>..." to include in what will be committed) world.txt 


此時 hello.txt 已經刪除了。工作區中也沒有該文件了。並產生了一個待提交事項(Changes to be committed)。

我們提交刪除,並查看狀態:

$ git commit -m 刪除hello文件
[master 1e3d8be] 刪除hello文件
 1 file changed, 1 deletion(-)
 delete mode 100644 hello.txt


$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed) world.txt nothing added to commit but untracked files present (use "git add" to track) 

現在只剩下我們未追蹤的 world.txt 了,hello.txt已經被刪除了。

3. 暫存區中移除文件
git rm --cached <filename> 命令
另外一種情況是,我們想把文件從暫存區域移除,但仍然希望保留在當前工作目錄中。 
即讓文件保留在工作區,但是並不想讓 Git 繼續跟蹤。 
當你忘記添加 .gitignore 文件,不小心把一個很大的日志文件或一堆 .a 這樣的編譯生成文件添加到暫存區時,這一做法尤其有用。 
git rm --cached <filename> 是 git add <filename> 的反向操作命令。


4. 重命名文件
命令 git mv file_from file_to
該命令將重新命名文件。文件必須是已追蹤的文件。
實踐:
我們追蹤 world.txt 並將其重命名為 world2.txt

$ git add world.txt

$ git mv world.txt world2.txt

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage) new file: world2.txt 



其實,運行 git mv 就相當於運行了下面三條命令:

$ mv README.md README
$ git rm README.md
$ git add README



轉載自:知優碼  https://www.javaidea.cn/topic/1238.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM