Giter Site home page Giter Site logo

gittest's Introduction

GitTest - A test project for project management via Github

I. 简介

Git并不像SVN那样有个中心服务器。目前我们使用到的Git命令都是在本地执行,比如你想通过Git分享你的代码或者与其他开发人员合作进行项目开发,又比如你有多台PC或者多个工作地点,想实现项目版本更新同步,你就需要将数据放到一台其他开发人员或其他PC能够连接的服务器上。为了方便项目开发及代码管理,在Github上建立远程仓库是十分明智的选择。

II. 工作原理

III. 具体操作

A. 关联个人电脑(PC)与Github账户

为了方便利用Github进行项目管理,我们可以先建立PC与对应Github账户之间的连接。由于本地Git仓库和GitHub远程仓库之间的传输是通过SSH加密的,所以我们需要配置验证信息。首先,我们可以使用以下命令在本地生成SSH Key:

$ ssh-keygen -t rsa -C "[email protected]"

有时我们的本地机器可能会用到多个Git账号对于不同的项目或者是远程服务器,此时我们就需要不同的SSH Key来对应不同Git账号。但是如果我们已经有生成好的SSH Key文件又直接通过上述命令行来生成新Key,新的密钥就会把老的密钥覆盖掉。此时,我们就可以通过给密钥文件定义新的文件名加以区分:

$ ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_github

标签-f指定密钥文件存储文件名,利用-f标签,我们可以给密钥文件赋予新的文件名。关于一台本地终端如何配置多个SSH Key,更多细节可以参考这篇博文:Mac上配置SSH - 多个SSH。标签-t指定密钥类型,默认是 rsa ,可以省略。标签-C后面的内容为注释,为了方便区分,可以把[email protected]改为个人的Github账号(当然,不管也是没问题的)。之后会要求确认路径和输入密码,在此只需一直回车选择默认选项就行,就像这样:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/liusongwei/.ssh/id_rsa):  # 首次生成SSH Key可选择密钥存放地址,对于健忘人士,建议直接回车
Enter passphrase (empty for no passphrase):                           # 登陆密码,对于健忘人士,建议直接回车不要密码
Enter same passphrase again:                                          # 如未设置密码,直接回车就好
Your identification has been saved in /Users/liusongwei/.ssh/id_rsa
Your public key has been saved in /Users/liusongwei/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:GTFbdl7uJAnG4tmCmH/frOIbM1JCAO1g5YuowSmXXg8 [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|           . o   |
|   . .    . . +  |
|  . o . .  ... . |
|   o o o o o.+.  |
|  o . . E *.o.o. |
|   o . . +.B+o. .|
|    . . o.=+=o.o |
|       ..o ===o  |
|      ..  o+Bo   |
+----[SHA256]-----+

成功的话会在~/下生成.ssh文件夹,里面存放着私钥id_rsa和公钥id_rsa.pub。 利用记事本打开id_rsa.pub,复制里面的公钥并上传到Github即可。详细操作可见这篇博文:如何使用SSH连接到Github。接下来,我们还需激活与Github的连接,要完成这一步,只需在本地终端执行以下命令即可:

$ ssh -T [email protected]
Hi MajestyV! You've successfully authenticated, but GitHub does not provide shell access.

有时新的客户端初次连接Github时会显示这种警报:

$ ssh -T [email protected]
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:XXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

这种情况下其实没有任何问题,就是应当注意不要直接回车(按enter),要先输入yesenter

The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:XXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Hi MajestyV! You've successfully authenticated, but GitHub does not provide shell access.

详情可以参考:git使用--解决The authenticity of host ‘github.com (20.205.243.166)‘ can‘t be established。这类警报的原因是当前客户端为没有host关于Github网站的内容,所以出现问题。

B. Managing project? (not yield decided)

X. 上传代码到Github远程仓库

首先,git add要上传的文件或者目录到暂存区:

$ git add [file1] [file2] ...  # 添加一个或多个文件到暂存区
$ git add [dir]                # 添加指定目录到暂存区,包括其子目录
$ git add .                    # 添加当前目录下的所有文件到暂存区

然后,利用git commit命令将暂存区内容添加到本地仓库中:

$ git commit -m [message]      # 标签-m后面可以写上本次提交的备注信息

最后,上传代码到远程仓库并合并:

$ git push

有时,本地仓库和远程仓库会存在版本冲突问题,例如:

To github.com:MajestyV/GitTest.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:MajestyV/GitTest.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

可以先git pull拉取远程仓库的最新版本融合到本地,再重新上传一次代码(即重复上述操作)。更多问题,可以参考这篇博文:Git push 时如何避免出现 "Merge branch 'master' of ...".

IV. 常用命令行详解

(X) git pull - 下载远程代码并合并

git pull命令用于从远程获取代码并合并本地的版本。git pull其实就是git fetchgit merge FETCH_HEAD的简写。

$ git pull <远程主机名> <远程分支名>:<本地分支名>

(X) git push - 上传远程代码并合并

git push命令用于从将本地的分支版本上传到远程并合并。

$ git push <远程主机名> <本地分支名>:<远程分支名>

如果本地分支名与远程分支名相同,则可以省略冒号:

$ git push <远程主机名> <本地分支名>

(X) git

外部链接

A. 关于SSH

[X] 安全外壳协议(Secure Shell Protocol,简称SSH)- 维基百科

[X] SSH 免密登录是怎么玩儿的?

B. 关于Git

[1] Git官网

[X] Git教程 - 菜鸟教程

C. 关于Github

[1] Github官网

[2] Github中文操作手册(可谓包罗万有)

[X] Github简明教程 - 菜鸟教程

D. 利用Github进行项目代码管理

[1] 关于Github存储库

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.