美文网首页工具
Git学习笔记(一)

Git学习笔记(一)

作者: jacob110 | 来源:发表于2015-08-16 18:04 被阅读4312次

先贴一个有趣又好玩Git学习站点 learnGitBranching

前言

Windows 环境下 有有两种Git工具, Git BashGit Shell。这两种主要的区别是,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

或者也可以查看某个环境变量的设定

$ git config user.name
JJ_Jacob

Git 基础

获取项目的Git仓库

获取仓库有两种方法:一种是直接Clone已有的Git仓库,另一种是在现存目录下新建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/

现在是一个空的目录,先随便加点东西,新建个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.

出错了。查看官网 帮助文档 意思是如果用 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.

现在可以了,继续刚才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 文件状态

查看文件状态 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下来的项目,未做任何改动。

现在新建一个文本文件,不提交再看看

$ 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.txtUntracked,提示 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

现在已经添加到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 提示修改在本地已提交,可以推送到远程仓库。

总结,Git 入门,一些初始化配置,本地提交提交修改

$ git config --global []
$ git config --list
$ git init
$ git add
$ git commit
$ git clone [url]

相关文章

网友评论

  • 567ad186d9ca:你好 git报这个错 解决了么 谢谢 小白一个 求解决
    Your branch is up-to-date with 'origin/master'.
    Bestime:我也遇到过,当时就是老老实实
    git add .
    git commit

本文标题:Git学习笔记(一)

本文链接:https://www.haomeiwen.com/subject/vevwqttx.html