那么,下面咱们开始git的hello world之旅吧! html
$ mkdir ~/public_html $ cd ~/public_html $ echo 'My website is alive!' > index.html $ git init Initialized empty Git repository in /Users/longkai/public_html/.git/
执行后生成的隐藏.git/目录就是git维护的你的这个repository的整个版本库 git
你的pulic_html/被称为工做目录 github
使用git add (file|files|dirs)向repository中添加文件 web
一次能够添加一个或者多个文件,也能够添加一个目录 shell
$ git add index.html
命令执行成功后,git知道了index.html将要保留在repository中,可是git只是把index.html暂存了起来,做为在下一次提交以前的一个临时动做。 vim
git将add和commit划分为两个过程是为了保持repository的稳定性,深思熟虑后的提交不至于repository太乱:-) bash
接下来执行git status来查看工做目录和版本库之间的状态 编辑器
$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: index.html #
运行的结果表示新文件index.html文件将在下一次提交后添加到repository中 ui
git repository除了文件内容的变化外,git还会记录每次提交的元数据,包括提交log和做者 google
$ git commit -m "Initial contents of public_html" [master (root-commit) 0d3625d] Initial contents of public_html 1 file changed, 1 insertion(+) create mode 100644 index.html
这里简单的使用了一句话的log,实际中你可使用你本身喜欢的文本编辑器记录更详细的log,好比vim,若是默认不是vim,那么在bash中export GIT_EDITOR=vim
接下来咱们再来看看工做目录和reposiroty之间的状态
$ git status # On branch master nothing to commit, working directory clean
看,clean working directory,这意味着工做目录如今没有修改,添加,删除的文件或内容啦!
刚才的提交我没有指名做者,由于我已经配置了默认的做者:-)
$ git config --global user.name "longkai" $ git config --global user.email "im.longkai@gmail.com"
$ cd ~/public_html $ vim index.html # edit file $ cat index.html <html> <body> welcome to my site! </body> </html>
接下来直接提交
$ git commit index.html # open vim to add log $ git status # On branch master nothing to commit, working directory clean
也许你会以为奇怪,为何不是先add再commit呢?若是熟悉git后发现其实不是这样的,由于这个文件在提交以前已经加入到repository了,git已经认识她啦!
如今,咱们已经有了2个提交
一旦你有了提交后你即可以查阅你的提交
命令git log将整个提交列表倒序打印出来
$ git log commit fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e Author: longkai <im.longkai@gmail.com> Date: Thu Nov 28 23:55:02 2013 +0800 convert to html commit 0d3625d1b212debc3e683928c791cc7cf9e6181d Author: longkai <im.longkai@gmail.com> Date: Thu Nov 28 23:40:06 2013 +0800 Initial contents of public_html
以上只是查看了提交的ID(如fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e),做者,日期和log,想要进一步查看内容的变动,使用git show命令
$ git show fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e commit fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e Author: longkai <im.longkai@gmail.com> Date: Thu Nov 28 23:55:02 2013 +0800 convert to html diff --git a/index.html b/index.html index 34217e9..8a9dac1 100644 --- a/index.html +++ b/index.html @@ -1 +1,5 @@ -My website is alive! +<html> + <body> + welcome to my size! + </body> +</html>
不带commit id则表示最近的一次提交。很酷,是吧!
另外,命令git show-branch则提供了更简洁的展示
$ git show-branch --more=7 [master] convert to html [master^] Initial contents of public_html
因为咱们只有2次提交,因此只显示了2条,不带任何参数则表示最近的一次提交
想要查看任意两个提交之间的差别?用git diff commit_id1 commit_id2
$ git diff fcf1a7 0d3625 diff --git a/index.html b/index.html index 8a9dac1..34217e9 100644 --- a/index.html +++ b/index.html @@ -1,5 +1 @@ -<html> - <body> - welcome to my size! - </body> -</html> +My website is alive!
能够看到,commit_id能够是开头的几个字母的简写~
这个和普通的命很像,只是加上了git命令前缀
假设你的repository中有一个test.txt文件不想要他了
$ git rm test.txt rm 'test.txt' $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: test.txt # $ git commit -m "remove test.txt file" [master daca0eb] remove test.txt file 1 file changed, 1 deletion(-) delete mode 100644 test.txt
能够看到,执行git rm test.txt命令后,test.txt文件被删除到了暂存区,下一次提交就将被删除,这个和add很是像
假设你有一个foo.txt的文件,想要重命名为bar.txt
$ git mv foo.txt bar.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: foo.txt -> bar.txt # $ git commit -m "rename foo.txt to bar.txt" [master 2e1ca23] rename foo.txt to bar.txt 1 file changed, 0 insertions(+), 0
OK,就是这么简单!
使用git clone命令能将一个完整的版本库复制,包裹全部的提交,变动,彻底同样。
若是是从web上clone,那么还会有一些额外的信息复制下来,方便追踪与增长提交等,这里就先不涉及啦。
$ cd ~ $ git clone public_html/ public_html2 Cloning into 'public_html2'... done. Checking connectivity... done
git的配置文件是继承式的,下面分别从优先级高到低说明
.git/config repository相关的配置,可使用--file选项对其进行操做(默认操做也是如此) ~/.gitconfig 用户相关的配置,可使用--global选项对其进行操做 /etc/gitconfig 系统相关的配置,可使用--system选项对其进行配置(若是你有足够的权限),另外,该文件可能根据操做系统的不一样放在不一样的目录
可使用git config -l命令查看当前适用的配置
$ git config -l user.name=longkai user.email=im.longkai@gmail.com color.diff=auto color.ui=true alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit alias.st=status alias.br=branch alias.ci=commit core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.ignorecase=true core.precomposeunicode=false
固然,你也能够直接查看.git/config文件的内容
$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true ignorecase = true precomposeunicode = false
发现怎么没有以前show的那么长呢?其实它是继承了用户配置的,使用git config -l --global查看便可!
使用git config --unset命令删除配置,固然,你也可使用文本编辑器直接编辑配置文件!
$ git config --unset --global user.mail
命令太长?别担忧,正如shell提供的别名同样,你也能够给命令添加别名呢!
$ git status # On branch master nothing to commit, working directory clean $ git config alias.s "status" $ git s # On branch master nothing to commit, working directory clean
能够看到,咱们使用git s完成了git status的命令,另外,咱们使用的是项目相关的配置哦~
本笔记参考自[version control with git 2nd],感谢原做者!
github连接:https://github.com/longkai/longkai/blob/master/notes/git/get-started.md