Git简单应用Git

git学习笔记

2016-12-07  本文已影响157人  王平升

简介

Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆,在本地机器上拷贝一个完整的Git仓库。
Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

初步使用

$ 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).

然后关联远程库:

但是在进行操作时候报错了:

$ 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。方法如下:

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]-----+
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.
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。

上一篇 下一篇

猜你喜欢

热点阅读