git-文件操作
git-文件操作
軟件版本:
操作系統:ubuntu10.04
??? 內核版本:Linux version 2.6.32-36-generic
??? git 版本:git version 1.7.0.4
目錄:
1. 文件狀態
2. 跟蹤新文件
3. 移除文件
4. 文件移動
5. 忽略文件
6. 文件取消操作
6.1 取消已暫存文件
6.2 取消對文件的修改
7. 參考資料
1. 文件狀態
??? 查看文件當前處于什么狀態的命令為:git status 。一般倉庫中的文件可能存在于這三種狀態:
??? 1)Untracked files → 文件未被跟蹤;
??? 2)Changes to be committed → 文件已暫存,這是下次提交的內容;
??? 3) Changes bu not updated → 文件被修改,但并沒有添加到暫存區。如果 commit 時沒有帶 -a 選項,這個狀態下的文件不會被提交。
??? 值得注意的是,同一個文件有可能同時出現在第二和第三種狀態中。例如:
$git add NewFile$vim NewFile # 編輯該文件
$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: NewFile
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: NewFile
#
這時只需要將 NewFile 再添加一次就可以了。
$git add NewFile$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: NewFile
#
2. 跟蹤新文件
??? 要跟蹤一個新文件,使用命令 git add 。例如要跟蹤文件 FileName 。
??? 1) 添加跟蹤之前的狀態:
$ git status# On branch master
# Changed but not updated:
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# FileName
no changes added to commit (use "git add" and/or "git commit -a")
??? 2) 跟蹤新文件:
$ git add FileName??? 3)對文件跟蹤后的狀態:
$ git status# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: FileName
#
??? 新添加的文件進入了暫存狀態(Changes to be committed)。
3. 移除文件
??? 將文件從 git 倉庫中移除的最根本目的就是使得 git 不再跟蹤目標文件。這又產生了兩種情況:
??? 1) 將文件從 git 倉庫中移除,但仍然保留在當前目錄中。
rm 'FileName'
$ls # 文件仍然保留在當前目錄下
FileName
$git status # 查看 git 的狀態
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: FileName
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# FileName
??? 2) 將文件從倉庫中移除,并且從當前目錄中刪除。
$git rm FileNamerm 'FileName'
$ls # FileName 已經被刪除
$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: FileName
#
4. 文件移動
??? git 不會自動檢索文件移動,所以如果你在倉庫中對文件重命名則有可能導致錯誤,因為重命名后的文件沒有被跟蹤。
$mv FileName NewFileName$git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: FileName
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# NewFileName
no changes added to commit (use "git add" and/or "git commit -a")
??? 正確的操作方法應該是:
$git mv FileName NewFileName$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: FileName -> NewFileName
#
5. 忽略文件
請參考這里。
6. 文件取消操作
如果我們不小心將某些文件暫存了,或者取消對某個文件的修改,都可以通過 git status 的提示恢復過來。
6.1 取消已暫存文件
假如我們不小心把某個文件 git add 到暫存區里面,需要取消這個暫存文件,按照 git status 的提示去做吧!
$git add .$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: FileName
# modified: AddFile
#
根據提示,我們可以使用 git reset HEAD <file>...命令來取消已暫存的文件 AddFile 。
$ git reset HEAD AddFileAddFile: locally modified
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: FileName
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: AddFile
#
6.2 取消對文件的修改
我們也可以將某個文件修改過但未被提交的文件恢復到上一次提交時的狀態。
$ git status# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: FileName
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: AddFile
#
按照提示,使用 git checkout -- <file>...命令將指定文件恢復。
$ git checkout -- AddFile$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: FileName
#
7. 參考資料:
[1] 《pro git》
轉載于:https://www.cnblogs.com/eddy-he/archive/2012/03/08/git_file_operation.html
總結
- 上一篇: [轉]JavaScript获取HTML
- 下一篇: Vim as Python IDE on