git使用基础

本文详细介绍了Git中文件的各种状态,包括未跟踪(untracked)、已暂存(staged)及已修改但未更新的状态。并提供了如何使用Git命令来管理这些状态的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在加入git系统中的文件,处于以下三种区域的某一中(working directory:包含正在被编辑的文件; staging area: 包含下一提交(commit)时将被提交至仓库的内容):


对于git系统,文件状态有如下几种:


1.在本地对某一个工程进行git管理时,进入该工程目录,并进行git 初始化: git init

此时,工程中的文件均处于untracked状态,即git系统不管理这些文件。

2.通过git status 查看处于untracked状态的文件

$ vim README
$ git status
# On branch master
# Untracked files:
#(use "git add <file>..." to include in what will be committed)
#
#README
从例子中看出,文件README处于untracked状态

3.将希望由git管理的文件加入git系统:git add filename

此时文件处于staged区域,用git status 查看处于staged区域文件

$ git status
# On branch master
# Changes to be committed:
#(use "git reset HEAD <file>..." to unstage)
#
#new file: README
#

4.如果此时修改工作区的一个文件,如benchmarks.rb,用git status,则会提醒如下:

$ git status
# On branch master
# Changes to be committed:
#(use "git reset HEAD <file>..." to unstage)
#
# Changes to be committed:
#(use "git reset HEAD <file>..." to unstage)
#
#new file: README
#
# Changed but not updated:
#(use "git add <file>..." to update what will be committed)
#
#modified: benchmarks.rb
#
5.提交(commit)staged中的文件:git commit -m ‘message for commit'

6.当需要删除文件时,以删除文件grit.gemspec为例:

$ rm grit.gemspec
$ git status
#On branch master
#Changed but not updated:
#(use "git add/rm <file>..." to update what will be committed)
#deleted: grit.gemspec
$ git rm grit.gemspec
$ git status
#On branch master
#Changes to be committed:
#(use "git reset HEAD <file>..." to unstage)
#deleted: grit.gemspec
7.对文件重命名,以文件README.TXT为例:

$ git mv README.txt README
$ git status
#On branch master
#Your branch is ahead of ’origin/master’ by 1 commit.
#Changes to be committed:
#(use "git reset HEAD <file>..." to unstage)
#renamed: README.txt -> README

附:参考《pro git 2009》page 13-23




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值