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 仓库中移除,但仍然保留在当前目录中。

复制代码

$git rm --cached FileName

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. 忽略文件

  请参考这里

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 AddFile
AddFile: 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》

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值