先貼一個有趣又好玩Git學習站點 learnGitBranching
前言
Windows 環(huán)境下 有有兩種Git工具, Git Bash 和Git Shell。這兩種主要的區(qū)別是,Git Bash 是Linux風格的命令行工具,Git Shell 是Windows Power Shell 的風格,可以使用Windows 和Shell的命令來操作。我使用了 Git Bash。
基本配置
配置用戶信息
$ git config --global user.name github username
$ git config --global user.email github email
查看本機 Git 配置信息
$ git config --list
core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
...
user.name=github username
user.email=github email
或者也可以查看某個環(huán)境變量的設(shè)定
$ git config user.name
JJ_Jacob
Git 基礎(chǔ)
獲取項目的Git倉庫
獲取倉庫有兩種方法:一種是直接Clone已有的Git倉庫,另一種是在現(xiàn)存目錄下新建Git倉庫
新建Git倉庫
Administrator@QH-20150523KJHQ /d/java_projects
$ mkdir git-study
Administrator@QH-20150523KJHQ /d/java_projects
$ cd git-study/
Administrator@QH-20150523KJHQ /d/java_projects/git-study
$ git init
Initialized empty Git repository in d:/java_projects/git-study/.git/
現(xiàn)在是一個空的目錄,先隨便加點東西,新建個README文本文件,然后加入版本控制
$ git add README.md
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
Administrator@QH-20150523KJHQ /d/java_projects/git-study (master)
$ git commit -m 'init and add README'
[master (root-commit) aeda323] init and add README
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
1 file changed, 2 insertions(+)
create mode 100644 README.md
克隆已有倉庫
命令格式為 git clone[url], 比如我將Github的博客Clone到本地
$ git clone git@github.com:Jacob110/Jacob110.github.io.git jacobblog
Cloning into 'jacobblog'...
The authenticity of host 'github.com (192.30.252.130)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know
n hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
出錯了。查看官網(wǎng) 幫助文檔 意思是如果用 Git Bash的話需要將SSH key 添加到 ssh-agent 中。
如果沒有SSH Key 需要生成一下 這里 Generating SSH keys 也講的很清楚。
最后測試下
$ ssh -T git@github.com
Hi Jacob110! You've successfully authenticated, but GitHub does not provide shel
l access.
現(xiàn)在可以了,繼續(xù)剛才clone倉庫
$ git clone git@github.com:Jacob110/Jacob110.github.io.git jacobblog
Cloning into 'jacobblog'...
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to t
he list of known hosts.
remote: Counting objects: 287, done.
remote: Compressing objects: 63% (91/144) R
remote: Compressing objects: 100% (144/144), done.
Rremote: Total 287 (delta 21), reused 0 (delta 0), pack-reused 136eceiving objec
Receiving objects: 100% (287/287), 1.66 MiB | 291.00 KiB/s, done.
Resolving deltas: 3% (2/60)
Resolving deltas: 100% (60/60), done.
Checking connectivity... done.
這里clone的時候在本地新建了一個目錄。
Git 文件狀態(tài)
查看文件狀態(tài) git status
Administrator@QH-20150523KJHQ /d/java_projects/jacobblog (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
這是剛才Clone下來的項目,未做任何改動。
現(xiàn)在新建一個文本文件,不提交再看看
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
顯示剛添加的 test.txt 為 Untracked,提示 add,add 之后再看
$ git add test.txt
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test.txt
現(xiàn)在已經(jīng)添加到Git 版本控制,也就是可追蹤 tracked,等待被提交。提交下再看看
$ git commit
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
use "git push" to publish your local commits 提示修改在本地已提交,可以推送到遠程倉庫。
總結(jié),Git 入門,一些初始化配置,本地提交提交修改
$ git config --global []
$ git config --list
$ git init
$ git add
$ git commit
$ git clone [url]