git学习笔记
简介
Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆,在本地机器上拷贝一个完整的Git仓库。
Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
-
Git的功能特性:
从一般开发者的角度来看,git有以下功能:
1、从服务器上克隆完整的Git仓库(包括代码和版本信息)到单机上。
2、在自己的机器上根据不同的开发目的,创建分支,修改代码。
3、在单机上自己创建的分支上提交代码。
4、把服务器上最新版的代码fetch下来,然后跟自己的主分支合并。
5、生成补丁(patch),把补丁发送给主开发者。
6、一般开发者之间解决冲突的方法,开发者之间可以使用pull 命令解决冲突,解决完冲突之后再向主开发者提交补丁。 -
Git 与Github区别
git 是一个软件,可以在 .git 文件夹里面维护你的历史代码。指定了 remote 链接和用户信息之后,git 可以帮你将提交过的代码 push 到远程的仓库或者将远程仓库的代码 fetch 到本地。
github是一个基于git的项目托管平台,它提供了web界面,你可以在上面创建资源仓库来存放你的项目。在本地或服务器创建一个资源仓库通过shell命令或图形用户界面可以和远端的github进行项目同步更新,实现对项目的管理。
初步使用
- 安装
$ git
xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.
$ 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:
在mac上输入git查看是否安装过,没有安装则会自动提示安装,安装后再次输入git就可以看到用法提示。
- 配置
安装后需要进行配置:
$ git config --global user.name pingsheng
$ git config --global user.email pingsheng_w@163.com
- 创建版本库
$ pwd
/Users/twcn/pingsheng
$ git init
Initialized empty Git repository in /Users/twcn/pingsheng/.git/
$ ls
test1.txt test2.txt
$ git status
On branch master
Initial commit
Untracked files: (use "git add <file>..." to include in what will be committed)
test1
test2
nothing added to commit but untracked files present (use "git add" to track)
$ git commit -m "first test"
[master (root-commit) 8451070] first test
2 files changed, 3 insertions(+)
create mode 100644 test1.txt
create mode 100644 test2.txt
$ git status
On branch masternothing to commit, working directory clean
- 创建与删除分支
$ git branch branch1
$ git checkout branch1
Switched to branch 'branch1'
$ git checkout -b branch2
Switched to a new branch 'branch2'
$ git branch -d branch1
Deleted branch branch1 (was 8451070).
- 远程库操作
首先是添加远程库,在GitHub上创建一个与本地git仓库同名的git仓库:
然后关联远程库:
但是在进行操作时候报错了:
$ git remote add origin git@github.com:ApplePP/pingsheng.git
$ git push -u origin master
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
查了资料后才知道是需要在github中添加ssh key。方法如下:
- 查看是否已经有了ssh密钥:cd ~/.ssh如果没有密钥则不会有此文件夹,有则备份删除,生成密钥:
wangpingsheng% ssh-keygen -t rsa -C pingsheng_w@163.com
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/pswang/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/pswang/.ssh/id_rsa.
Your public key has been saved in /Users/pswang/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:07WN4yPhm4oKMIxwlwtfw8zUakEO0dey+c8q0180WN4 pingsheng_w@163.com
The key's randomart image is:
+---[RSA 2048]----+
| o+o. . |
| Oo + . |
|. o o B+ + . . |
|+. + oo.o. . B . |
|+. o. S.o = = E|
| o o.o o . |
| . .ooo . |
| . .o .+oo |
| ... .++o. |
+----[SHA256]-----+
- 添加密钥到ssh:
wangpingsheng% ssh-add
Identity added: /Users/pswang/.ssh/id_rsa (/Users/pswang/.ssh/id_rsa)
cat ~/.ssh/id_rsa.pub
并将ssh key添加到github中
- 下面进行测试:
wangpingsheng% ssh git@github.com
The authenticity of host 'github.com (192.30.253.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi ApplePP! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
- 然后再执行git push
wangpingsheng% git push -u origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 258 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@github.com:ApplePP/pingsheng.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
就将代码上传至Github中:
小结
以前就知道git用来储存代码,并且理解到git的分支可以从开发主线上分离开来,然后在不影响主线的同时继续工作。今天的学习已经初步掌握了git的基本使用方法,但是在关联远程库时遇到了错误,师兄指导后又查了资料最后解决了问题,以后多加应用,希望能够逐渐熟练使用git。