Git git commit -a 命令困惑

zlx_star · 2012年06月28日 · 最后由 jinjidexiaogaigai 回复于 2020年08月17日 · 82474 次阅读

各位,我最近刚接触 git,对其内部原理不甚了解,想问一下 git commit -a 代表什么意思?

参考 图解 git

git commit -a 相当于运行 git add 把所有当前目录下的文件加入暂存区域再运行。git commit.

而 git 的帮助文档上又说

commit 
-a  Tell the command to automatically stage files that have been modified and deleted, but new files you have not told git about are not affected.

两个地方不一致啊。

另外,请问各位,如果想系统地学习 git 命令,除了 git 的帮助文档外有什么参考资料吗?

感谢各位,但是我这里环境比较恶劣,除了 http://book.douban.com/subject/6526452/ 这个链接 OK 外,其它的都无法访问。 @shaorui23 这个链接就是我的问题所在,它和 git 的帮助文档不一致。 @primemars 这个游戏刚刚玩过,做的很好,让我对 git 更了解了。:-) @huacnlee -A ?

在我的版本中,没有 -A 这个参数 详见

$ git --version
git version 1.7.1

$ git commit -A
error: unknown switch `A'
usage: git commit [options] [--] <filepattern>...

    -q, --quiet           be quiet
    -v, --verbose         be verbose

Commit message options
    -F, --file <FILE>     read log from file
    --author <AUTHOR>     override author for commit
    --date <DATE>         override date for commit
    -m, --message <MESSAGE>
                          specify commit message
    -c, --reedit-message <COMMIT>
                          reuse and edit message from specified commit
    -C, --reuse-message <COMMIT>
                          reuse message from specified commit
    --reset-author        the commit is authored by me now (used with -C-c/--amend)
    -s, --signoff         add Signed-off-by:
    -t, --template <FILE>
                          use specified template file
    -e, --edit            force edit of commit
    --cleanup <default>   how to strip spaces and #comments from message
    --status              include status in commit message template

Commit contents options
    -a, --all             commit all changed files
    -i, --include         add specified files to index for commit
    --interactive         interactively add files
    -o, --only            commit only specified files
    -n, --no-verify       bypass pre-commit hook
    --dry-run             show what would be committed
    --short               show status concisely
    --porcelain           show porcelain output format
    -z, --null            terminate entries with NUL
    --amend               amend previous commit
    --no-post-rewrite     bypass post-rewrite hook
    -u, --untracked-files[=<mode>]
                          show untracked files, optional modes: all, normal, no. (Default: all)
    --allow-empty         ok to record an empty change

所以是不是 http://marklodato.github.com/visual-git-guide/index-zh-cn.html 这里面写错了?

Pro Git 这本书中解释的是: 在修改文件后,需要使用git add把文件加入暂存区,这样git commit时才能把已经修改的信息加入版本库,而使用git commit -a可以不用再git add

英文解释是完全正确的,图解 git 的中文解释只是助于理解的,忽略了细节,完全没有提及新文件是不会被加入到暂存区,正因为这个原因,一般都不推荐使用git commit -a,还是推荐先git addgit commit@huacnlee 所提 -A 应该是指git add -A,跟楼主问题无关。 资料我也推荐 Pro Git,可去新浪爱问搜搜看。

我在实际中偶尔会用 git commit -am“some sthing” 这个命令会让当前在暂存区里面的文件修改保存并且 commit,但是 untracted files 不会被加入到仓库区。 你可以试一下。

@AshZ 哦,不好意思,我看成 add 了

git commit -a 是把 unstaged 的文件变成 staged(这里不包括新建 (untracked) 的文件),然后 commit

#8 楼 @AshZ 我是来顶这个回复的,耐心、细致、全面

如果修改的文件需要全部提交,我一般都是 git commit -am "commit messages" 如果修改的文件只提交一部分,用 git add -p git commit -m "commit messages"

@camel 这个说的最清楚 , 其实上面的命令帮助里说的很清楚了

谢谢 @camel 谢谢大家,我现在理解啦。😊

AshZ 回复

太感谢前辈了,你这 2012 年的回复,依然帮助到了我,我是学习前端的小白阶段,接触到了 git,一直在想 git commit 时候,加不加 -a,有什么区别,有时候 加不加好像都没区别,但是我 git commit --help 调出帮助文档的时候,也看到了和题主 一样的英文描述,我一直理解的是,“加了 -a,后,会自动把 modified 和 deleted add 到 stage 里,但是新增文件不会受影响。 ”对这后半句可是彻底理解反了,我以为不会受影响,是指我不需要操心新文件,,,,难怪我一直觉得哪里不对劲,今天花点时间,再加点实践,总算是搞明白了。。。。。。加了-a,在 commit 的时候,能帮你省一步 git add,但也只是对修改和删除文件有效,新文件还是要 git add,不然就是 UNtracked!

AshZ 回复

特地注册了账号来回复,同时感谢楼主开的贴的,提的问题,以至于 5 年后的今天,我还是能在 google 里方便的找到了答案

居然这么早的问题都被翻出来。

特意注册个账号来点赞 时隔这么多年的帖子 还是解决了我的困惑~

需要 登录 后方可回复, 如果你还没有账号请 注册新账号