git-文件操作
(2012-02-02 15:07:41)
git-文件操作
软件版本:
操作系统:ubuntu10.04
内核版本:Linux
version 2.6.32-36-generic
git 版本:git
version 1.7.0.4
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
2)对文件跟踪后的状态:
$ 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
仓库中移除,但仍然保留在当前目录中。
$git rm
--cached
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
FileName
rm '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. 忽略文件
请参考这里。
喜欢
0
赠金笔
加载中,请稍候......