创建仓库 采用git init 在目录创建新的仓库
1 2 3 $ mkdir newrepository   $ cd  newrepository $ git init 
或者直接采用 git init 仓库名
1 $ git init newrepository 
软件仓库下载 下载仓库 git clone 拷贝到本地
例如,从我的github上下载代码到本地
1 $ get clone  https://github.com/Hubery-Lee/LeeEmacs-setting 
软件仓库的快照 git的日常使用 git 的日常使用过程
git add 命令可将文件添加到缓存()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 $ touch README $ touch hello.php $ ls README        hello.php $ git status -s ?? README ?? hello.php $ git add README hello.php  $ git status -s A  README A  hello.php $  在 README 添加以下内容: $ echo  "# Runoob Git" >>README  $ git status -s AM README A  hello.php $ git add . $ git status -s A  README A  hello.php 
执行 git diff 来查看执行 git status 的结果的详细信息。
git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个主要的应用场景。
尚未缓存的改动:git diff
查看已缓存的改动: git diff --cached
查看已缓存的与未缓存的所有改动:git diff HEAD
显示摘要而非整个 diff:git diff --statgit status 显示你上次提交更新后的更改或者写入缓存的改动, 而 git diff 一行一行地显示这些改动具体是啥。
接下来我们来查看下 git diff --cached 的执行效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 在 hello.php 文件中输入以下内容: <?php echo  '菜鸟教程:www.runoob.com' ;?> $ git status -s A  README AM hello.php $ git diff diff --git a/hello.php b/hello.php index e69de29..69b5711 100644 --- a/hello.php +++ b/hello.php @@ -0,0 +1,3 @@ +<?php +echo  '菜鸟教程:www.runoob.com' ; +?> $ git add hello.php  $ git status -s A  README A  hello.php $ git diff --cached diff --git a/README b/README new file mode 100644 index 0000000..8f87495 --- /dev/null +++ b/README @@ -0,0 +1 @@ + diff --git a/hello.php b/hello.php new file mode 100644 index 0000000..69b5711 --- /dev/null +++ b/hello.php @@ -0,0 +1,3 @@ +<?php +echo  '菜鸟教程:www.runoob.com' ; +?> 
git commit 将缓存区内容添加到仓库中
1 2 3  配置用户名和邮箱地址 $ git config --global user.name 'runoob'  $ git config --global user.email test @runoob.com 
提交缓存到仓库
1 2 3 4 5 6 7 8 9 $ git commit -m '第一次版本提交'  [master (root-commit) d32cf1f] 第一次版本提交  2 files changed, 4 insertions(+)  create mode 100644 README  create mode 100644 hello.php $ git status nothing to commit (working directory clean) 
git reset HEAD 命令用于取消已缓存的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 $ git status -s  M README  M hello.php $ git add . $ git status -s M  README M  hello.pp $ git reset HEAD hello.php  Unstaged changes after reset: M    hello.php $ git status -s M  README  M hello.php $ git commit -m '修改'  [master f50cfda] 修改  1 file changed, 1 insertion(+) $ git status -s  M hello.php 
仍然可以再次提交hello.php修改的内容
1 2 3 4 5 6 $ git commit -am '修改 hello.php 文件'  [master 760f74d] 修改 hello.php 文件  1 file changed, 1 insertion(+) $ git status On branch master nothing to commit, working directory clean 
简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存。
git rm
1 2 3 4 5 6 $ git rm <file> $ git rm -f  <file> $ git rm -r <file> $ git rm --cached <file> 类似有git mv 移动和重命名 $ git mv README  README.md 
要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交。可以用以下命令完成此项工作
git中tag和branch的使用 创建分支、列出分支、切换分支、删除分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 $ git branch testing  $ git branch    * master   testing $ git checkout testing Switched to branch 'testing'  $ git checkout master Switched to branch 'master'  $ git checkout -b newtest   Switched to a new branch 'newtest'  $ git branch -d testing   Deleted branch testing (was 85fc7e7). $ git branch   * master   newtest $ git merge newtest      Updating 2e082b7..556f0a0 Fast-forward  test2.txt | 1 -  1 file changed, 1 deletion(-)  delete mode 100644 test2.txt 
当同时记录到master的修改和branch分支的修改时,会出现合并分支冲突
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 $ git merge change_site Auto-merging test.txt CONFLICT (content): Merge conflict in  test.txt Automatic merge failed; fix conflicts and then  commit the result. $ cat test.txt  <<<<<<< HEAD  runoob.com 新增加一行 ======= www.runoob.com >>>>>>> change_site $ vim test.txt  $ cat test.txt  www.runoob.com 新增加一行 $ git diff diff --cc test.txt index f84c2a4,bccb7c2..0000000 --- a/test.txt +++ b/test.txt @@@ -1,2 -1,1 +1,2 @@@ - runoob.com + www.runoob.com  +新增加一行 $ git status -s UU test.txt $ git add test.txt  $ git status -s M  test.txt $ git commit [master 88afe0e] Merge branch 'change_site' 
查看提交历史
1 2 3 4 5 6 7 $ git log    $ git log  --oneline   $ git log  --oneline --graph   $ git log  --reverse --oneline  $ git log  --author=Linus --oneline -5  $ git log  --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges 
标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ git tag -a v1.0  $ git log  --oneline --decorate --graph   $ git tag -a v0.9 85fc7e7        $ git log  --oneline --decorate --graph *   88afe0e (HEAD, tag: v1.0, master) Merge branch 'change_site'  |\   | * d7e7346 (change_site) changed the site * | 14b4dca 新增加一行 |/   * 556f0a0 removed test2.txt * 2e082b7 add test2.txt * 048598f add test.txt * 85fc7e7 (tag: v0.9) test  comment from runoob.com $ git tag   v0.9 v1.0 $ git tag -a <tagname> -m "runoob.com标签"    $ git tag -s <tagname> -m "runoob.com标签"    
远程软件仓库的提取 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ git remote    origin $ git remote -v   origin    git@github.com:tianqixin/runoob-git-test.git (fetch) origin    git@github.com:tianqixin/runoob-git-test.git (push) $ git fetch [alias ]  $ git fetch origin   remote: Counting objects: 3, done . remote: Compressing objects: 100% (2/2), done . remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done . From github.com:tianqixin/runoob-git-test    0205aab..febd8ed  master     -> origin/master 
远程软件仓库的上传 1 2 3 4 5 6 7 8 9 10 11 12 13 $ git merge [alias ]/[branch]  $ git merge origin/master   Updating 0205aab..febd8ed Fast-forward  README.md | 1 +  1 file changed, 1 insertion(+) $ git push [alias ] [branch]  $ git push origin master     git remote rm  [alias ] git remote mv [aliasA] [aliasB] 
获取软件仓库更改历史 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ git status $ git status -s $ git diff  $ git diff --cached $ git log    $ git log  --oneline   $ git log  --oneline --graph   $ git log  --reverse --oneline  $ git log  --author=Linus --oneline -5  $ git log  --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges $ git branch $ git tag $ git remote 
回滚到软件仓库的历史版本 1 2 3 4 $ git reset  $ git reset HEAD [file] $ git reset --hard <target_commit_id> $ git reset --soft origin/source  命令(source  分支)