git 学习

2016/11/13 git

git 学习

参考 : 廖雪峰的git教程

安装git

首先需要确认自己的电脑是否已经安装了git. 在terminal下使用命令: git, 来确认是否已经安装了git.

qiuyu :~/note$ git
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Forward-port local commits to the updated upstream head
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
qiuyu :~/note$ 

想这样, 代表我已经安装了git. 如果没有安装, 可以使用brew安装, 执行命令:

qiuyu :~/note$ brew install git

这样就会自动安装git.

===

初始化git

现在必须确保已将安装好了git. 那么, 在使用git之前, 还需要做一些初始化的工作.

  • 需要一个工作目录. 这个工作目录是存放你要使用的git空间. 以后所有要使用git的数据, 都需要放在这个目录下.
qiuyu :~$ cd 
qiuyu :~$ mkdir note
  • 初始化你的git目录. 执行完毕以后, 可以看到, 目录里多了一个隐藏文件.git, 这个是存放一些git的版本控制信息. 不要随便的修改里面的文件.
qiuyu :~$ cd note
qiuyu :~/note$ git init
Initialized empty Git repository in /Users/qiuyu/note/.git/
qiuyu :~/note$ ll
total 0
drwxr-xr-x   3 qiuyu  staff   102B 11 13 09:52 ./
drwxr-xr-x+ 41 qiuyu  staff   1.4K 11 13 09:51 ../
drwxr-xr-x  10 qiuyu  staff   340B 11 13 09:52 .git/
  • 设置邮箱和密码. 每个git用户都需要设置不同的邮箱和用户名. 以方便协同工作.
qiuyu :~/note$ git config --global user.email "[email protected]"
qiuyu :~/note$ git config --global user.name "qiuyu"

===

添加一个文件

  • 确保是在git的工作目录内.
qiuyu :~/note$ pwd
/Users/qiuyu/note
  • 先查看一下目前git的状态.
qiuyu :~/note$ git status
On branch master
nothing to commit, working directory clean
  • 添加一个文件.
qiuyu :~/note$ ll
total 0
drwxr-xr-x   3 qiuyu  staff   102B 11 13 10:21 ./
drwxr-xr-x+ 42 qiuyu  staff   1.4K 11 13 09:55 ../
drwxr-xr-x  13 qiuyu  staff   442B 11 13 10:22 .git/
qiuyu :~/note$ echo This is a demo which first use git. > demo
qiuyu :~/note$ ll
total 8
drwxr-xr-x   4 qiuyu  staff   136B 11 13 10:24 ./
drwxr-xr-x+ 42 qiuyu  staff   1.4K 11 13 09:55 ../
drwxr-xr-x  13 qiuyu  staff   442B 11 13 10:22 .git/
-rw-r--r--   1 qiuyu  staff    36B 11 13 10:24 demo
qiuyu :~/note$ 
  • 使用命令 git add 'filename' , 执行完毕后, 没有任何提示, unix的宗旨是, 没有消息, 就是好消息.
qiuyu :~/note$ git add demo
qiuyu :~/note$ 
  • 再次查看git的状态.
qiuyu :~/note$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   demo

qiuyu :~/note$ 
  • 执行 git commit -m '备注信息'
qiuyu :~/note$ git commit -m 'add demo'
[master 8ca7c18] add demo
 1 file changed, 1 insertion(+)
 create mode 100644 demo
qiuyu :~/note$ 
  • 再次查看git状态, 看一下有什么变化.
qiuyu :~/note$ git status
On branch master
nothing to commit, working directory clean
qiuyu :~/note$ 
  • 到这里, 文件就已经添加到git的版本控制里了.

===

git中add和commit的作用

  • 工作区 / 工作目录(Working Directory)

工作区就是创建的目录, 我这里就是note目录.

  • 版本库(Repository)

首先看一下这张图, 就能明白大致的意思了.

stage: 暂存区, add添加的文件, 都是放入到这里. master: 主分支, 如果没有创建别的分支, 那么就只有这一个分支, git init的时候, 自动创建了这个主分支. 主分支只能有1个.

当我们在工作区中新写入了一个文件, 而我们还没有执行git add命令的时候, git版本库会监测到我们新增了一个文件, 此时, 文件只是放在了我们的工作区内, 并没有放入版本库中.

当我们执行了git add filename后, 文件会在stage中存放我们的文件, 我们也可以多次git add filename1 filename2, 这样, 多个或者一个文件就加入到了stage中.

现在的情况是, 文件已经加入到了stage中, 我们还需要一步操作, 才能让让这个文件加入版本控制系统中. git commit -m '备注信息', 这条命令的意思是: 在stage中, 把变化的信息都提交到版本控制系统中. 如果我们前面执行的add命令, 是多个文件的话, 就是把这些文件一起加入到版本控制系统中.

注意: git status, 这条命令, 都是针对stage来说的, 是查看stage中的变化信息.

===

文件比较

首先查看一下stage的状态.

qiuyu :~/note$ git status
On branch master
nothing to commit, working directory clean
qiuyu :~/note$ 

现在我们在原来的demo文件中, 新加入一行信息. 原来的信息是:

This is a demo which first use git.

加入一行新的内容后, 文本是:

This is a demo which first use git.
insert a new line.

在执行git add demo之前, 查看一下stage的状态.

qiuyu :~/note$ 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:   demo

no changes added to commit (use "git add" and/or "git commit -a")
qiuyu :~/note$ 

意思是: stage中没有改变, 但是检测到工作区中demo文件有修改. 想要查看修改了那些内容, 我们可以执行git diff命令.

qiuyu :~/note$ git diff
diff --git a/demo b/demo
index 08ec907..02a8eff 100644
--- a/demo
+++ b/demo
@@ -1 +1,2 @@
 This is a demo which first use git.
+insert a new line.
qiuyu :~/note$ 

可以看到, 新加入的一行前面, 有1个加号, 说明这1行是新加入的.

我们在修改一下demo文件. 修改一个单词, 把第一行的first修改为second. 修改后的内容:

This is a demo which second use git.
insert a new line.

现在查看变化信息. 执行git diff

qiuyu :~/note$ git diff
diff --git a/demo b/demo
index 08ec907..bb42854 100644
--- a/demo
+++ b/demo
@@ -1 +1,2 @@
-This is a demo which first use git.
+This is a demo which second use git.
+insert a new line.
qiuyu :~/note$ 

可以看到, 改变的信息可以很明显的对比出来.

执行git add *命令.

qiuyu :~/note$ git add * 
qiuyu :~/note$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   demo

qiuyu :~/note$ 

再执行git commit -m 'mod demo'

qiuyu :~/note$ git commit -m 'mod demo'
[master ad5b046] mod demo
 1 file changed, 2 insertions(+), 1 deletion(-)
qiuyu :~/note$ git status
On branch master
nothing to commit, working directory clean
qiuyu :~/note$ 

这样, 修改的文件就又提交到版本控制系统中了.

===

返回到以前的版本

先说1条命令, git log, 用来查看每次commit的记录. 执行git log

qiuyu :~/note$ git log
commit ad5b0468f3397417a0c5ff03b90cce4ec4fc5ce4
Author: qiuyu <[email protected]>
Date:   Sun Nov 13 14:20:46 2016 +0800

    mod demo

commit 8ca7c18fc1464a9a8c7d2fc86c3aca806a8d4961
Author: qiuyu <[email protected]>
Date:   Sun Nov 13 10:52:23 2016 +0800

    add demo

再说明一下. 在git中, 用HEAD表示当前版本. 上一个版本, 就是HEAD^ . 上上个版本就是HEAD^^ . 上上上个版本, 就是HEAD^^^ . 上99个版本, 怎么写, 有简单的方法, 就是HEAD~99.

到目前为止, 我们做了2次的commit, 第一次是添加了一个demo文件, 第二次是修改了demo文件(新增了1行数据, 且把原来第一行中的first改为了second).

那么, 如果我第二次的修改, 是错误的操作, 我想返回到原来的demo文件. (把mod demo版本反悔到add demo版本)

可以的. 执行git reset --hard HEAD^

qiuyu :~/note$ git reset --hard HEAD^
HEAD is now at 8ca7c18 add demo
qiuyu :~/note$ 

提示信息写的很明白了, 我们已经回到了add demo版本.

此时再查看一下记录信息 , 执行git log

qiuyu :~/note$ git log
commit 8ca7c18fc1464a9a8c7d2fc86c3aca806a8d4961
Author: qiuyu <[email protected]>
Date:   Sun Nov 13 10:52:23 2016 +0800

    add demo

commit 2a723137a9986ef51c9fb6963b8977819f2338ee

可以看到, 我们修改demo的记录已经不在了. 现在我们已经达到了我们想要的目的, 返回到了第一个版本, 那么, 如果现在又想回到修改后的版本, 怎么办呢, 现在log已经看不到记录了, 也就不能使用HEAD^来返回了.

是有办法的. 命令 git reflog来帮助你找到每次操作的版本号.

qiuyu :~/note$ git reflog
8ca7c18 HEAD@{0}: reset: moving to HEAD^
ad5b046 HEAD@{1}: commit: mod demo
8ca7c18 HEAD@{2}: commit: add demo

可以看到, mod demo的版本号是ad5b046, 我们就可以根据这个版本号, 来返回到这个版本了.

同样的命令, git reset, 只是后面我们不用 HEAD^表示版本信息, 而是用版本号表示哪个版本. 如:

qiuyu :~/note$ git reset --hard ad5b046
HEAD is now at ad5b046 mod demo
qiuyu :~/note$ 

现在版本有回到了mod demo这个版本.

===

连接github

首先确认用户的根目录下是否有.ssh目录

qiuyu :~$ cd
qiuyu :~$ ll
total 768
drwxr-xr-x+  43 qiuyu  staff   1.4K 11 14 14:40 ./
drwxr-xr-x    6 root   admin   204B 10 27 20:24 ../
-r--------    1 qiuyu  staff     9B 10 27 20:23 .CFUserTextEncoding
-rw-r--r--@   1 qiuyu  staff    18K 11 14 14:25 .DS_Store
drwx------   36 qiuyu  staff   1.2K 11 14 14:45 .Trash/
drwxr-xr-x   16 qiuyu  staff   544B 11  6 22:52 .atom/
-rw-r--r--    1 qiuyu  staff   5.6K 11 14 14:40 .bash_history
drwxr-xr-x  107 qiuyu  staff   3.6K 11 14 15:26 .bash_sessions/
-rw-r--r--    1 qiuyu  staff    18B 10 29 17:07 .bashrc
-rw-r--r--    1 qiuyu  staff    47B 11 13 09:55 .gitconfig
-rw-------    1 qiuyu  staff    35B 11  5 23:54 .lesshst
-rw-------    1 qiuyu  staff    56B 10 29 16:50 .mysql_history
drwxr-xr-x    3 qiuyu  staff   102B 10 29 11:58 .oracle_jre_usage/
drwxr-xr-x   40 qiuyu  staff   1.3K 11 14 15:15 .sogouinput/
drwx------    3 qiuyu  staff   102B 11 13 15:12 .ssh/
drwxr-xr-x    6 qiuyu  staff   204B 10 29 14:28 .subversion/
drwxr-xr-x    3 qiuyu  staff   102B 11  5 09:42 .vim/
-rw-------    1 qiuyu  staff   6.4K 11 14 14:40 .viminfo
drwx------    6 qiuyu  staff   204B 11 14 14:37 Applications/
drwxr-xr-x@   5 qiuyu  staff   170B 11 12 23:12 Applications (Parallels)/
drwx------+   5 qiuyu  staff   170B 11 13 22:02 Desktop/
drwx------+   9 qiuyu  staff   306B 11 12 22:12 Documents/
drwx------+ 102 qiuyu  staff   3.4K 11 14 15:02 Downloads/
drwx------@  60 qiuyu  staff   2.0K 11 12 13:53 Library/
drwx------+   3 qiuyu  staff   102B 10 27 20:23 Movies/
drwx------+   5 qiuyu  staff   170B 11  6 22:52 Music/
drwx------+  17 qiuyu  staff   578B 11 14 15:24 Pictures/
drwxr-xr-x+   6 qiuyu  staff   204B 11  6 22:52 Public/
drwxr-xr-x    3 qiuyu  staff   102B 11 14 14:17 WORK/
drwxr-xr-x   27 qiuyu  staff   918B 11 12 17:05 book/
drwxr-xr-x    4 qiuyu  staff   136B 11  9 14:36 c_test/
-rw-r--r--@   1 qiuyu  staff   315K 11  1 20:33 computer_mac_332026_11.jpg
drwxr-xr-x    3 qiuyu  staff   102B 11 10 10:21 debug/
drwxr-xr-x    3 qiuyu  staff   102B 11  4 19:05 essaty/
drwxrwxrwx   10 qiuyu  staff   340B 10 29 17:46 linux源码包/
drwxrwxrwx   21 qiuyu  staff   714B 10 29 17:46 lnmp-shell-install/
drwxr-xr-x    5 qiuyu  staff   170B 11 14 15:20 mweb/
-rw-r--r--    1 qiuyu  staff    12B 10 29 14:18 mysql初始密码
drwxr-xr-x    4 qiuyu  staff   136B 11 13 14:43 note/
-rwxrwxrwx    1 qiuyu  staff   366B 10 29 14:04 rm_mysql.sh*
-rw-r--r--    1 qiuyu  staff   220B 10 29 12:02 smartsvn.license
drwxr-xr-x    4 qiuyu  staff   136B 11  6 20:11 test/
drwxrwxrwx   10 qiuyu  staff   340B 11 14 15:06 www/
qiuyu :~$ cd .ssh
qiuyu :~/.ssh$ ll
total 8
drwx------   3 qiuyu  staff   102B 11 13 15:12 ./
drwxr-xr-x+ 43 qiuyu  staff   1.4K 11 14 14:40 ../
-rw-r--r--   1 qiuyu  staff   395B 11 13 15:12 known_hosts

可以看到, 我这有.ssh目录, 进入目录后, 没有id_rsaid_rsa.pub两个文件, 如果没有, 就需要生成一下. 在家目录下, 创建SSH Key:

ssh-keygen -t rsa -C "[email protected]"

都是简单的英语, 目录位置, 密码, 我使用的都是空密码和默认位置.

qiuyu :~$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/qiuyu/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/qiuyu/.ssh/id_rsa.
Your public key has been saved in /Users/qiuyu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:tDQZE5cklAajMd7YkH4RR9r3yQrLzaV8JhPtoyMPJVs [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|    +.+=O+o.     |
|   ..O.=o*.      |
|   .+ +o* .      |
|    . .o + + .   |
|     .  S E *    |
|       . @ *     |
|        = O =    |
|        ...* .   |
|         oo.     |
+----[SHA256]-----+
qiuyu :~$ 

创建完ssh_key之后, 进入.ssh目录, 就会看到生成了2个文件了

qiuyu :~$ cd .ssh
qiuyu :~/.ssh$ ll
total 24
drwx------   5 qiuyu  staff   170B 11 14 15:27 ./
drwxr-xr-x+ 43 qiuyu  staff   1.4K 11 14 14:40 ../
-rw-------   1 qiuyu  staff   1.6K 11 14 15:27 id_rsa
-rw-r--r--   1 qiuyu  staff   398B 11 14 15:27 id_rsa.pub
-rw-r--r--   1 qiuyu  staff   395B 11 13 15:12 known_hosts
qiuyu :~/.ssh$ 

这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

Search

    Post Directory