Chapter 2
初始化
已经存在的project
git init
对文件纳入版本控制
git add filenames
这些文件就会被tracked
克隆已存在的仓库
git clone
记录仓库的变化
each file in your working directory can be in one of two states: tracked (unmodified, modified, or staged)or untracked
刚clone的仓库里的文件都是tracked并且unmodified。在递交后的改动将被视为modified
git add filename
将track被add的文件并且文件是staged
add后的修改会使文件变为unstaged,下次递交的时候不会递交这些修改。可以再次add或commit 时加-a来stage
.gitignore
# a comment - this is ignored
*.a # no .a files
!lib.a # but do track lib.a, even though you're ignoring .a files above
/TODO # only ignore the root TODO file, not subdir/TODO
build/ # ignore all files in the build/ directory
doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
To see what you’ve changed but not yet staged, type git diff
with no other arguments
If you want to see what you’ve staged that will go into your next commit, you can use git diff --cached
. (In Git versions 1.6.1 and later, you can also use git diff --staged
, which may be easier to remember.)
递交
git commit files -m "msg"
git commit
调用$EDITOR编辑,也可以通过
git config --global core.editor
设置
删除
git rm #文件将从磁盘上删除并unstaged
If you modified the file and added it to the index already, you must force the removal with the -f
option.
修改过的被tracked的文件需要-f选项来删除
git rm --cached filename #仅仅不跟踪而不从磁盘上删除
文件名的file-glob patterns:
git rm \*~
可以不加\ ?
移动
git mv name_old name_new
等价于
mv name_old name_new
git rm name_old
git add name_new
历史
git log
git log
lists the commits made in that repository in reverse chronological order最近的记录显示最前
git log -p #显示diff
-N 最近的N条log
--stat显示简略的修改信息,包括多少文件有变更,这些文件里增删的行数
--pretty=online,short,full,fuller, format:"xxxx "
OptionDescription 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 e-mail
%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
--since=, --until= 限制时间段
OptionDescription
-(n) Show only the last n commits
--since, --after 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.
撤销
git commit --amend #修改 commit-message
Unstaging a Staged File
git reset HEAD <file>... #针对Changes to be committed
取消修改
git checkout -- <file>... #针对chenged but not updated,没有add的文件
Remote
http://progit.org/book/ch2-5.html
git remote -v
Tagging
git tag #列出可用的tag
git tag -l 'v1.*' #列出匹配的tag
git tag -a v1.0.0 -m 'my tag v1.0.0'
git tag -a tagname checksum(or part of it) #对某个历史commit 进行标签
git show v1.0.0
By default, the git push
command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches — you can run git push origin [tagname]
.
push默认不把tag传至remote server
git push origin --tags #传送所有tags
TIPS
自动补全
linux, mac: ontrib/completion/g
it-completion.bash 加入至~/.bashrc或/etc/bash_completion.d/
常用的alias
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
$ git config --global alias.last 'log -1 HEAD'
调用外部命令,需要加!
git config --global alias.visual "!gitk"