Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git_4 仓库初始化/克隆现有仓库(git init/clone、add、commit) #64

Open
Qingquan-Li opened this issue Oct 12, 2017 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Oct 12, 2017

新建一个仓库并初始化

新建作为仓库的文件夹

  1. mkdir 文件夹名 :新建文件夹,示例: mkdir test
  2. touch 文件名 :新建文件,示例: touch test.txt ;或 cat > 文件名 :创建文件并进入编辑模式( Control + D 2次,退出编辑)
  3. cd 文件夹名 :切换到文件夹目录进行其他操作,示例: cd test

注:也可以手动新建文件,不使用命令。

在现有目录中初始化仓库

如果你打算使用 Git 来对现有的项目进行管理,你只需要进入该项目目录( cd 文件夹名 )并输入:

$ git init

该命令将创建一个名为 .git 的子目录,这个子目录含有已经初始化的 Git 仓库中所有的必须文件,这些文件是 Git 仓库的骨干。 但是,在这个时候,我们仅仅是做了一个初始化的操作,项目里的文件还没有被跟踪(使用 git add 命令跟踪文件)。

注意:创建是的 .git 文件为隐藏文件, Command + Shift + . :显示隐藏文件( macOS Sierra )。

附:如果在一个已经存在文件的文件夹(而不是空文件夹)中初始化 Git 仓库来进行版本控制的话,应该开始跟踪这些文件并提交。可通过 git add 命令来实现对指定文件的跟踪,然后执行 git commit 提交:

$ git add \*.c
$ git add LICENSE
$ git commit -m 'initial project version'

使用 git init 后运行 git add <file> 命令,开始跟踪当前目录下的文件。

注: git add 命令使用文件或目录的路径作为参数;如果参数是目录的路径,该命令将递归地跟踪该目录下的所有文件。



克隆现有的仓库

Git 克隆的是该 Git 仓库服务器上的几乎所有数据。 默认配置下远程 Git 仓库中的每一个文件的每一个版本都将被拉取下来。

注:你可以把 git clone 命令理解为高级点的复制,这个时候该项目本身就已经是一个 Git 仓库了,不需要执行 git init 进行初始化,而且甚至都已经关联好了远程仓库。

  • 克隆仓库的命令格式是 git clone [url]
    • 示例: git clone https://github.com/libgit2/libgit2
  • 如果想在克隆远程仓库的时候,自定义本地仓库的名字: git clone [url] 自定义名字
    • 示例: git clone https://github.com/libgit2/libgit2 mylibgit

传输协议

git clone 支持多种协议,除了 HTTP(s) 以外,还支持 SSH 、Git、本地文件协议等,下面是一些例子:

$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git 
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/

SSH协议还有另一种写法:

$ git clone [user@]example.com:path/to/repo.git/

通常来说, Git 协议下载速度最快, SSH 协议用于需要用户认证的场合。



  • Git 可以使用四种主要的协议来传输资料
    本地协议(Local),HTTP 协议,SSH(Secure Shell)协议及 Git 协议。
    其中,后 3 个协议中,SSH 协议同时支持读写操作,HTTP 协议和 Git 协议都是只读的。
    参考: https://git-scm.com/book/zh/v2/服务器上的-Git-协议
  • GitHub 上的项目可通过 HTTP 协议或 SSH 协议(需配置)访问,示例(克隆仓库)
    • HTTP 协议: git clone https://github.com/FatliTalk/blog
    • SSH 协议: git clone ssh://git@github.com/FatliTalk/blog
      或者 git clone git@github.com:FatliTalk/blog
@Qingquan-Li Qingquan-Li changed the title 仓库初始化/克隆现有仓库 Git_4 仓库初始化/克隆现有仓库 Oct 12, 2017
@Qingquan-Li Qingquan-Li changed the title Git_4 仓库初始化/克隆现有仓库 Git_4 仓库初始化/克隆现有仓库(git init/clone、add、commit) Jun 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant