- Git的工作原理
然后是Git的工作方式:
也就是说,每次提交版本变动的时候,git会保存一个快照(snapshot)。如果文件没有被更改,git也不会再次保存,而是提供一个到原来文件的链接。这样一来,git更像是一个小型的文件系统。
此外,git的所有操作都可以是本地的,仅仅在将新版本的内容上传到服务器上时才需要连接网络。
git里面使用了SHA-1检验,因此,你几乎不可能瞒过git对项目代码进行任何修改。
git只会添加数据,而不会删除数据。因此不要担心我们做一些测试时会破坏原有的数据。
git的三种状态:已提交,已更改,和已标记。
已提交表示数据已经安全存储在本地数据库,已修改表示已经修改了文件,但还没有提交到数据库,已标记表示已经在当前版本标记了一个更改的文件,以便进入下一次提交的快照。
参看下图:
Git目录是Git保存元数据和对象数据库的地方。这也是Git最重要的部分。
working directory是项目某个版本的内容。
staging area是一个简单的文件,通常包含在Git目录中。其中存储了将要进入下一次提交的信息。
Git的基本工作流程如下:
1.在working directory中修改文件。
2.标识(stage)文件,并将文件快照添加到staging area。
3.执行commit,将获取staging area中的文件,并将快照永久保存到Git目录中
2. 安装git程序
3\设置SSH
1)检查是否已有SSH key
输入以下命令:
$ cd ~/.ssh
如果提示“No such file or directory“,则跳转到第3步,如果不是则继续第2步。
(2)备份并删除已有的SSH key
$ ls
Lists all the subdirectories in the current directory
config id_rsa id_rsa.pub known_hosts
$ mkdir key_backup
makes a subdirectory called "key_backup" in the current directory
$ cp id_rsa* key_backup
Copies the id_rsa and id_rsa.pub files into key_backup
$ rm id_rsa*
注意,以上只有美元符号后面才是你要在命令行中输入的命令,其它是对该命令的解释说明,或者输入命令后返回的结果。
(3).创建一个新的SSH key
$ ssh-keygen -t rsa -C "your_email@youremail.com"
Creates a new ssh key using the provided email
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):<press enter>
首先注意,your_email@youremail.com这里要用你自己的邮箱地址
然后会提示输入passphrase(密码):
Enter passphrase(empty for no passphrase):<输入你的密码>
Enter same passphrase again:<再次输入你的密码>
打开本地..\.ssh\id_rsa.pub文件。此文件里面内容为刚才生成人密钥。
4)登陆github系统。点击右上角的 Account Settings--->SSH Public keys ---> add another public keys
把你本地生成的密钥复制到里面(key文本框中), 点击 add key 就ok了
5)接着打开git ,测试连接是否成功
$ ssh -T git@github.com
如果提示:Hi defnngj You've successfully authenticated, but GitHub does not provide shell access. 说明你连接成功了
3 设置用户信息:
现在Git已经设置完毕,也输入了SSH Key,就可以配置个人信息了
(1)设置用户名和email
$ git config --global user.name "Firstname Lastname"
Sets the name of the user for all git instances on the system
$ git config --global user.email your_email@youremail.com
需要注意的是,这里的name是你的真名,不是Github的注册账号。
(2)设置GitHub token(根据情况选择)
有些工具可以无需使用SSH连接到GitHub,这样就必须设置API Token。
在网页上点击Account Setting,
然后在命令行中输入下面的命令:
$ git config --global github.user username
Sets the GitHub username for all git instances on the system
$ git config --global github.token 0123456789yourf012345678
此外,每次修改了GitHub密码后,都会创建新的API Token。
到这里,Git的设置工作就圆满了。
创建一个新的repo
在网页中点击New Repository
填写下面的信息,然后点击”Create Repository”
2.创建一个README
一般情况下都建议创建一个README
在Terminal中输入美元符号后面的命令
$ mkdir ~/Hello-World
作用:Creates a directory for your project called "Hello-World" in your user directory
$ cd ~/Hello-World
作用:Changes the current working directory to your newly created directory
$ git init
作用:Sets up the necessary Git files
Initialized empty Git repository in /Users/your_user_directory/Hello-World/.git/
$ touch README
然后使用文本编辑器打开repo里面的Hello-World文件夹的README文件。然后随便输入几句话,编辑完成后保存并关闭。
3.commit这个README
正如第1部分的工作原理所讲,commit其实是创建某个特定时点的项目内所有文件的快照。
使用下面的命令:
$ git add README
作用:Stages your README file, adding it to the list of files to be committed
$ git commit -m 'first commit'
一直到这一步,我们都还是在本地操作。
4.远程创建一个repo(在GitHub服务器中),并push刚才的commit
$ git remote add origin git@github.com:username/Hello-World.git
作用:Sets the origin for the Hello-World repo
$ git push -u origin master
现在你的repository已经在GitHub中创建成功了。
git的常见命令:
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
git merge develop------合并分支(将develop