git-文件操作
git-文件操作
軟件版本:
操作系統(tǒng):ubuntu10.04
??? 內(nèi)核版本:Linux version 2.6.32-36-generic
??? git 版本:git version 1.7.0.4
目錄:
1. 文件狀態(tài)
2. 跟蹤新文件
3. 移除文件
4. 文件移動(dòng)
5. 忽略文件
6. 文件取消操作
6.1 取消已暫存文件
6.2 取消對(duì)文件的修改
7. 參考資料
1. 文件狀態(tài)
??? 查看文件當(dāng)前處于什么狀態(tài)的命令為:git status 。一般倉庫中的文件可能存在于這三種狀態(tài):
??? 1)Untracked files → 文件未被跟蹤;
??? 2)Changes to be committed → 文件已暫存,這是下次提交的內(nèi)容;
??? 3) Changes bu not updated → 文件被修改,但并沒有添加到暫存區(qū)。如果 commit 時(shí)沒有帶 -a 選項(xiàng),這個(gè)狀態(tài)下的文件不會(huì)被提交。
??? 值得注意的是,同一個(gè)文件有可能同時(shí)出現(xiàn)在第二和第三種狀態(tài)中。例如:
$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
#
這時(shí)只需要將 NewFile 再添加一次就可以了。
$git add NewFile$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: NewFile
#
2. 跟蹤新文件
??? 要跟蹤一個(gè)新文件,使用命令 git add 。例如要跟蹤文件 FileName 。
??? 1) 添加跟蹤之前的狀態(tài):
$ 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)對(duì)文件跟蹤后的狀態(tài):
$ git status# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: FileName
#
??? 新添加的文件進(jìn)入了暫存狀態(tài)(Changes to be committed)。
3. 移除文件
??? 將文件從 git 倉庫中移除的最根本目的就是使得 git 不再跟蹤目標(biāo)文件。這又產(chǎn)生了兩種情況:
??? 1) 將文件從 git 倉庫中移除,但仍然保留在當(dāng)前目錄中。
rm 'FileName'
$ls # 文件仍然保留在當(dāng)前目錄下
FileName
$git status # 查看 git 的狀態(tài)
# 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) 將文件從倉庫中移除,并且從當(dāng)前目錄中刪除。
$git rm FileNamerm 'FileName'
$ls # FileName 已經(jīng)被刪除
$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: FileName
#
4. 文件移動(dòng)
??? git 不會(huì)自動(dòng)檢索文件移動(dòng),所以如果你在倉庫中對(duì)文件重命名則有可能導(dǎo)致錯(cuò)誤,因?yàn)橹孛蟮奈募]有被跟蹤。
$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")
??? 正確的操作方法應(yīng)該是:
$git mv FileName NewFileName$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: FileName -> NewFileName
#
5. 忽略文件
請(qǐng)參考這里。
6. 文件取消操作
如果我們不小心將某些文件暫存了,或者取消對(duì)某個(gè)文件的修改,都可以通過 git status 的提示恢復(fù)過來。
6.1 取消已暫存文件
假如我們不小心把某個(gè)文件 git add 到暫存區(qū)里面,需要取消這個(gè)暫存文件,按照 git status 的提示去做吧!
$git add .$git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: FileName
# modified: AddFile
#
根據(jù)提示,我們可以使用 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 取消對(duì)文件的修改
我們也可以將某個(gè)文件修改過但未被提交的文件恢復(fù)到上一次提交時(shí)的狀態(tài)。
$ 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>...命令將指定文件恢復(fù)。
$ 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》
轉(zhuǎn)載于:https://www.cnblogs.com/eddy-he/archive/2012/03/08/git_file_operation.html
總結(jié)
- 上一篇: [轉]JavaScript获取HTML
- 下一篇: perl 命令行小记