git commit 加上 -a 选项,Git 就会自动把所有【已经跟踪过】的文件暂存起来一并提交,从而跳过 git add 步骤,即:
1
git commit -am 'commit xxx'
注意,未被追踪过的数据不会被git commit -am提交
删除文件
方法1 先删除,然后使用git rm记录
如果只是简单地从工作目录中手工删除文件,运行 git status 时就会在 “Changes not staged for commit” 部分(也就是未暂存清单)看到,然后再运行 git rm 记录此次移除文件的操作,此时文件的状态才会变为Changes to be committed(即:删除操作被暂存)
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Changes not staged for commit: # 提示删除操作尚未添加到暂存区 (use "git add/rm <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) deleted: download.c
no changes added to commit (use "git add" and/or "git commit -a")
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Changes to be committed: # 已经将删除文件的操作添加到暂存区 (use "git restore --staged <file>..." to unstage) deleted: download.c
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Changes to be committed: # 此时删除操作直接被记录到暂存区 (use "git restore --staged <file>..." to unstage) deleted: download.c
注意:如果使用git rm删除尚未追踪的文件,会报错fatal: pathspec 'xxx' did not match any files,比如:
1 2 3 4 5 6 7 8 9 10 11 12
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) README.md # 尚未被追踪的文件(即:仅存在与工作区)
no changes added to commit (use "git add" and/or "git commit -a")
86182@yawen MINGW64 /d/coding/git-playground (master) $ git rm README.md # 尝试通过git rm删除 fatal: pathspec 'README.md' did not match any files
方法3 使用git rm -f(暂存区中有改动)
如果删除之前修改过并且已经放到暂存区的话,则必须要用强制删除选项 -f,否则会提示如下错误:
1
error: the following file has changes staged in the index
86182@yawen MINGW64 /d/coding/git-playground (master) $ vim fetch.js # 修改文件
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: fetch.js
no changes added to commit (use "git add" and/or "git commit -a")
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Changes to be committed: # 改动已经保存到暂存区 (use "git restore --staged <file>..." to unstage) modified: fetch.js
86182@yawen MINGW64 /d/coding/git-playground (master) $ git rm fetch.js # 尝试不添加-f参数直接删除文件 error: the following file has changes staged in the index: # 报错 fetch.js (use --cached to keep the file, or -f to force removal)
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: fetch.js
86182@yawen MINGW64 /d/coding/git-playground (master) $ git status On branch master Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) deleted: README
Untracked files: (use "git add <file>..." to include in what will be committed) README.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git log --pretty=format:"%h - %an, %ar : %s" 32b941f - slimterry, 29 minutes ago : 重命名Readme.md 8baa801 - slimterry, 2 hours ago : 删除fetch.js 9990b44 - slimterry, 2 hours ago : 删除robot.txt de8fb67 - slimterry, 2 hours ago : 添加robot.txt 2ae831b - slimterry, 2 hours ago : 直接使用-am参数提交 6b40dfc - slimterry, 2 hours ago : 提交CONTRIBUTING.md 15ba6d0 - slimterry, 2 hours ago : 修改md 2e623a6 - slimterry, 19 hours ago : 提交.gitignore f496a0f - slimterry, 19 hours ago : 新增文件 3b4a7a8 - slimterry, 19 hours ago : 新增 new file: CONTRIBUTING.m 351876e - slimterry, 19 hours ago : initial project version
git log --pretty=format的常用选项如下:
选项
说明
%H
提交对象(commit)的完整哈希字串
%h
提交对象的简短哈希字串
%T
树对象(tree)的完整哈希字串
%t
树对象的简短哈希字串
%P
父对象(parent)的完整哈希字串
%p
父对象的简短哈希字串
%an
作者(author)的名字
%ae
作者的电子邮件地址
%ad
作者修订日期(可以用 --date= 选项定制格式)
%ar
作者修订日期,按多久以前的方式显示
%cn
提交者(committer)的名字
%ce
提交者的电子邮件地址
%cd
提交日期
%cr
提交日期,按多久以前的方式显示
%s
提交说明
图像化输出提交记录(–graph)
当 oneline 或 format 与另一个 log 选项 --graph 结合使用时尤其有用。 这个选项添加了一些ASCII字符串来形象地展示你的分支、合并历史,比如:
1 2 3 4 5 6 7 8 9 10 11
$ git log --pretty=format:"%h %s" --graph * 2d3acf9 ignore errors from SIGCHLD on trap * 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit |\ | * 420eac9 Added a method for getting the current branch. * | 30e367c timeout code and tests * | 5a09431 add timeout protection to grit * | e1193f8 support for heads with slashes in them |/ * d6016bc require time for xmlschema * 11d191e Merge branch 'defunkt' into local
$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: A.java modified: B.java
no changes added to commit (use "git add" and/or "git commit -a"
$ git add * # 意外直接提交了所有改动 git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: A.java modified: B.java
如何只取消暂存两个中的一个呢? git status 命令提示了你use "git restore --staged <file>..." to unstage:
1
$ git restore --staged B.java # 将B.java从暂存中移除
此时再次查看仓库的状态:
1 2 3 4 5 6 7 8 9 10
$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: A.java
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: B.java # B.java已经变为未暂存的状态
git status的结果中给出了方法use "git restore <file>..." to discard changes in working directory
1 2 3 4 5 6 7 8 9 10 11 12
$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: A.java
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: B.java $ git restore B.java # 撤消对文件的修改
List tags. With optional <pattern>..., e.g. git tag --list 'v-*', list only the tags that match the pattern(s).
Running “git tag” without arguments also lists all tags. The pattern is a shell wildcard (i.e., matched using fnmatch(3)). Multiple patterns may be given; if any of them matches, the tag is shown.
还有一个标签信息;并且可以使用 GNU Privacy Guard (GPG)签名与验证。 通常建议创建附注标签,这样你可以拥有以上所有信息;但是如果你只是想用一个临时的标签,或者因为某些原因不想要保存那些信息,轻量标签也是可用的。
附注标签
在 Git 中创建一个附注标签是很简单的。 最简单的方式是当你在运行 tag 命令时指定 -a 选项:
-a or --annotate : Make an unsigned, annotated tag object
-m <msg> or --message=<msg>:Use the given tag message (instead of prompting). If multiple -m options are given, their values are concatenated as separate paragraphs. Implies -a if none of -a, -s, or -u <key-id> is given.