git commit命令详解

按照git的步骤,要提交一个新的文件,或者一个修改过的文件分3步

第1步:将文件放入版本库的目录(貌似说的是废话)

第2步:用 git add 文件名(最好是全英文,尤其是在windows下)

第2.5步 用git status 命令 查看状态(这一步是良好的习惯,但不是必要,这样在修改、删除或者新建的文件比较多的时候能够避免错误)

这里具体讲解一下,状态主要分为三种状态

1)Untracked files → 文件未被跟踪;  (即没有用add命令的情况)
2)Changes to be committed → 文件已暂存,这是下次提交的内容;(用add命令之后或者文件被修改了再用add命令)
3) Changes bu not updated → 文件被修改,但并没有添加到暂存区。如果 commit 时没有带 -a 选项,这个状态下的文件不会被提交。(文件被修改但没有用再次add命令)

$git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   file2
#
# 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:   file
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    file3

上面代码给出的就是这三种状态

从中我们可以看到,如果只将所有被修改或者已删除的且已经被git管理的文档提交倒仓库中如果只是修改或者删除了已被Git 管理的文档,是没必要使用git add 命令的。


第三步:用git commit 命令提交  这里有篇文章讲解的比较好可以参考http://www.cnblogs.com/eddy-he/archive/2012/03/22/git_commit.html

这里有几个命令需要区分

git commit 与 git commit -a

git commit 提交的是暂存区里面的内容,也就是 Changes to be committed 中的文件。

git commit -a 除了将暂存区里的文件提交外,还提交 Changes bu not updated 中的文件。


如果直接运行 git commit 或者 git commit -a 则会进入编辑器进行描述此次更新的注释

一般来说默认是nano编辑器

修改的话有两种方式

一种用命令行git config --global core.editor vim 则修改成vim编辑器,主要对当前用户有效

还有一种是修改配置文件:打开.git/config文件,在core中添加 editor=vim即可。(.git目录的东西一般来说谨慎修改,负责会让版本库出现错误,尤其是windows下!!!!)


一般来说大家都不想进入编辑器中进行修改

所以常用的命令号为 git commit -m 或者 git commit -a -m

具体的实例为

$git commit -a -m "commit info"
这个代码与gitcommit相比快捷方便,但是就是commit信息格式无法控制。


还有一个十分重要的命令行

git commit --amend 一般的网上说明该命令行主要用于修改最后一次commit的信息。

其实还有一个很重要的作用就是修改或取消上一次的提交内容,用于补充文件具体例子如下


比如我们发现漏了 file3 没有提交,我们可以运行一下操作:

$git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    file3
nothing added to commit but untracked files present (use "git add" to track)

$git add file3
$git commit --amend 
[master 671f5cc] commit --amend, add file3
files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 file2
 create mode 100644 file3

$git status
# On branch master
nothing to commit (working directory clean)

当然如果最后一次commit的信息在想修改之前已经push上去了,那。。。。。。

也不是不能修改……比如这篇文章就讲解了怎么修改http://blog.csdn.net/tangkegagalikaiwu/article/details/8542827

不过最好不要出现这样的情况


最后,感谢

http://blog.csdn.net/hudashi/article/details/7664409

http://www.cnblogs.com/eddy-he/archive/2012/03/22/git_commit.html

文章的作者,部分内容转载自上述两篇

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值