簡介
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。