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》

转载于:https://www.cnblogs.com/eddy-he/archive/2012/03/08/git_file_operation.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐