Git的操作

在上一篇中repository已經創建成功

添加文件

在git的bash界面執行vim readme.txt並添加如下內容learning git並保存,通過git status命令查詢文件狀態,這個時候文件狀態是untracked.在git中支持vim語法。後面執行git add readme.txt命令將文件添加到庫中。

vim readme.txt
##添加後並保存
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.txt

nothing added to commit but untracked files present (use "git add" to track)

這個時候再執行git status查看文件狀態

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   readme.txt

這個時候文件是在暫存區中,並沒有存儲到庫中。執行下面命令將文件添加到庫中。

$ git commit -m 'add readme.txt'
[master (root-commit) ecd5ead] add readme.txt
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt

再查看文件狀態:

$ git status
On branch master
nothing to commit, working tree clean

小結

通過vim命令創建文件,當然你也可以拷貝過來。
通過git add 添加文件到暫存區。
通過git commit添加文件到本地庫。

修改文件後提交

在剛纔新加的文件上提交 one day。具體如下,這邊是通過vim編輯器添加的:
這裏寫圖片描述
這個時候這些git status命令:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

通過上面的信息也可以知道,git給我接下來提供了兩條路:
一條是’use “git add …” to update what will be committed’
一條是’use “git checkout – …” to discard changes in working directory’
這個選擇 git add 這條路,因爲我們需要提交到暫存區。而且注意文件名前面有一個’modified’狀態。後面就執行git commit命令提交文件到本地倉庫。

$ git commit readme.txt -m 'append one day to readme.txt'
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory.
[master 6c4d618] append one day to readme.txt
 1 file changed, 1 insertion(+)

刪除文件

使用如下命令刪除文件:

$ git rm readme.txt
rm 'readme.txt'

這個時候查通過 git status 查看工作空間狀態

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    readme.txt

執行如下命令提交刪除的文件到庫中:

$ git commit
[master 4d36493] delete readme.txt
 1 file changed, 2 deletions(-)
 delete mode 100644 readme.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章