# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Changes to be committed: # new file: README # modified: CONTRIBUTING.md # ~ ~ ~ ".git/COMMIT_EDITMSG" 9L, 283C
可以在其中编辑本次提交信息,如果不写任何东西的话,Git是不会承认本次commit的。
如果不想打开编辑器,可以直接在commit时加上-m参数,如下
1
$ git commit -m "Story 182: Fix benchmarks for speed"
可以用-a -m或者-am把stage、commit两个操作一起完成
1 2 3 4 5 6 7 8 9 10 11 12
$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
no changes added to commit (use "git add" and/or "git commit -a") $ git commit -a -m 'added new benchmarks' [master 83e38c7] added new benchmarks 1 file changed, 5 insertions(+), 0 deletions(-)
Removing Files
直接rm文件的话,文件是处于unstaged状态的,此时需要先add,再commit
1 2 3 4 5 6 7 8 9 10 11
$ rm PROJECTS.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
deleted: PROJECTS.md
no changes added to commit (use "git add" and/or "git commit -a")
或者直接用git rm,将文件直接置于staged状态
1 2 3 4 5 6 7 8
$ git rm PROJECTS.md rm 'PROJECTS.md' $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
$ git log --pretty=oneline ca82a6dff817ec66f44342007202690a93763949 changed the version number 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
或者自己定制输出模版
1 2 3 4
$ git log --pretty=format:"%h - %an, %ar : %s" ca82a6d - Scott Chacon, 6 years ago : changed the version number 085bb3b - Scott Chacon, 6 years ago : removed unnecessary test a11bef0 - Scott Chacon, 6 years ago : first commit
模版参数定义如下
Option
Description of Output
%H
Commit hash
%h
Abbreviated commit hash
%T
Tree hash
%t
Abbreviated tree hash
%P
Parent hashes
%p
Abbreviated parent hashes
%an
Author name
%ae
Author email
%ad
Author date (format respects the –date=option)
%ar
Author date, relative
%cn
Committer name
%ce
Committer email
%cd
Committer date
%cr
Committer date, relative
%s
Subject
在log中使用--graph来展示ASCII格式的图形化提交历史,建议使用更傻瓜的GUI工具来查看
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
Limit the commits to those made after the specified date.
–until, –before
Limit the commits to those made before the specified date.
–author
Only show commits in which the author entry matches the specified string.
–committer
Only show commits in which the committer entry matches the specified string.
–grep
Only show commits with a commit message containing the string
-S
Only show commits adding or removing code matching the string
举个🌰,直接照搬书中的好了:
For example, if you want to see which commits modifying test files in the Git source code history are merged and were committed by Junio Hamano in the month of October 2008, you can run something like this:
1 2 3 4 5 6 7 8
$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \ --before="2008-11-01" --no-merges -- t/ 5610e3b - Fix testcase failure when extended attributes are in use acd3b9e - Enhance hold_lock_file_for_{update,append}() API f563754 - demonstrate breakage of detached checkout with symbolic link HEAD d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths 51a94af - Fix "checkout --track -b newbranch" on detached HEAD b0ad11e - pull: allow "git pull origin $something:$current_branch" into an unborn branch
$ git add * $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README modified: CONTRIBUTING.md $ git reset HEAD CONTRIBUTING.md Unstaged changes after reset: M CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
$ git remote show origin * remote origin URL: https://github.com/my-org/complex-project Fetch URL: https://github.com/my-org/complex-project Push URL: https://github.com/my-org/complex-project HEAD branch: master Remote branches: master tracked dev-branch tracked markdown-strip tracked issue-43 new (next fetch will store in remotes/origin) issue-45 new (next fetch will store in remotes/origin) refs/remotes/origin/issue-11 stale (use 'git remote prune' to remove) Local branches configured for'git pull': dev-branch merges with remote dev-branch master merges with remote master Local refs configured for'git push': dev-branch pushes to dev-branch (up to date) markdown-strip pushes to markdown-strip (up to date) master pushes to master (up to date)
$ git config --global alias.unstage 'reset HEAD --'
这样,你就可以用
1
$ git unstage fileA
来代替
1
$ git reset HEAD -- fileA
如果你想要运行Git外部命令(rather than a Git subcommand),在命令前加上!(原文给的解释是This is useful if you write your own tools that work with a Git repository.笔者绞尽脑汁也没有想到这个功能用处在哪里…)