关注廖雪峰博客http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

下载github的桌面客户端,会有一个git shell程序,点击打开进入命令行模式~~

所有git的命令都git 开头

常用命令:

1.创建版本库respository  

$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit

首先创建即将存放代码的文件夹,进入该文件夹,pwd命令可查看当前所在的文件夹位置。

$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/

通过 git init命令创建版本库respository。当前目录下会多出.git的隐藏文件夹

 

2.添加到版本库 git add 和 git commit

添加文件到Git仓库,分两步:

  • 第一步,使用命令git add <file>,将文件的修改添加到stage(.git文件夹里面的暂存区)注意,可反复多次使用,添加多个文件;

  • 第二步,使用命令git commit,完成。


$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "add 3 files."
$ git commit -m "wrote a readme file"

 3.查看工作区状态和文件修改内容

  • 要随时掌握工作区的状态,使用git status命令。

  • 如果git status告诉你有文件被修改过,用git diff可以查看修改内容。

    $ 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:   readme.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    $ git diff readme.txt 
    diff --git a/readme.txt b/readme.txt
    index 46d49bf..9247db6 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,2 +1,2 @@
    -Git is a version control system.
    +Git is a distributed version control system.
     Git is free software.

    4.版本回退和前进返回

    • HEAD指向的版本就是当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id
      --hard : 重设暂存区(index/stage)和工作区(working directory),所有的改变都被重置,并且HEAD指针指向commit_id
      --soft:暂存区和工作区都不做任何改变,仅仅把HEAD指针指向commit_id
      --mixid:默认选项,仅重置暂存区(index/stage),暂存区与commit_id同步,但保留工作区的修改,HEAD指向commit_id

    • 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本。

    • 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。

      $ git reset --hard HEAD^
      $ git log
      commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
      Author: Michael Liao <askxuefeng@gmail.com>
      Date:   Tue Aug 20 14:53:12 2013 +0800
      
          add distributed
      
      commit cb926e7ea50ad11b8f9e909c05226233bf755030
      Author: Michael Liao <askxuefeng@gmail.com>
      Date:   Mon Aug 19 17:51:55 2013 +0800
      
          wrote a readme file
      $ git reset --hard 3628164
      $ git reflog
      ea34578 HEAD@{0}: reset: moving to HEAD^
      3628164 HEAD@{1}: commit: append GPL
      ea34578 HEAD@{2}: commit: add distributed
      cb926e7 HEAD@{3}: commit (initial): wrote a readme file

posted on 2016-04-05 10:31  heyucool  阅读(190)  评论(0编辑  收藏  举报