git客户端配置

基于window上

  • 下载

官网

我们下载 64-bit for windows setUp,下面的 带 Portable的是便携版的,解压就可用

安装

便携版的直接解压到目录下就行,安装版的按照步骤默认即可

略过

配置

  • 配置全局用户名和邮箱
# 配置用户名
# git config --global user.name [你的用户名 ]
git config --global user.name zhangsan

# 配置邮箱
# git config --global user.email [你的邮箱 ] 
git config --global user.email [email protected]

此时查看用户名和邮箱

$ git config --global user.name
zhangsan

$ git config --global user.email
[email protected]
  • 配置公钥和私钥

输入命令: `ssh-keygen -t rsa -C [你的邮箱地址]

默认即可

$ ssh-keygen -t rsa -C [email protected]
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/zhangsan/.ssh/id_rsa): y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in y.
Your public key has been saved in y.pub.
The key fingerprint is:
SHA256:s+GIv7AWuvx7adfdfGqJ2QMDojCc27yVWEgLkp7UUaKU [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|*.oo             |
|+=...            |
|=E.+.            |
|o=B...           |
|.=o.o   S        |
|  .+.o o.+.      |
|  oo*.o.o+ .     |
| .ooo=..o o      |
| .+=+o+o .       |
+----[SHA256]-----+


跳转到用户目录下

cd ~/.ssh

然后查看ssh的公钥

$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfOWcg81+D5w0/j3aN70CTguUI9+KI3EO1MCgu3Zeju8cHMncC3yYirLA/TlB9ozGJTdhCLBGiIqRrEuBROOyZsMuEE2bxUDVvJM5guUlezlqdU5PWwTLycIFA2hNxER5iKirILDS0snrwbjgvvM1Vl9P0EHfALxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxkPjZu405RnfvFzjL3ksn1CpyA4h8Nh4jxTrij48Y4vlDJYJDjxQ5/3BG8fMGlMknV+nHNnLoF5topSBOukQIR [email protected]

  • 将公钥添加到远端服务器

我这里要上传GitHub,所以我去GitHub的设置页面添加一个公钥

登录GitHub,然后转到setting界面的 SSH and GPG keys

选择添加一个sshkey

把刚刚cat id_rsa.pub看到的复制到key中(Title可以不填)

点击 Add SSH key 保存成功,然后就可以给这个账户下的项目提交了

使用

新建目录 test

打开git bash

cd xxx/test

  • 初始化一个项目

执行命令:git init

$ git init
Initialized empty Git repository in C:/Users/zhangsan/Desktop/test/.git/

可以看到在test下创建了一个 .git 的目录(如果看不到,请在系统中设置显示隐藏的文件)

  • 新建README.txt

新建一个README描述文件

  • 查看状态

运行命令 : git status

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

        README.txt

nothing added to commit but untracked files present (use "git add" to track)

可以看到,我们刚刚添加的README。

有如下提示:

(use “git add …” to include in what will be committed)

使用 git add ...命令 来包含将被提交的地方(即:提交到暂存区)

nothing added to commit but untracked files present (use “git add” to track)

没有添加任何内容提交但存在未跟踪的文件(使用“ git add”进行跟踪)

我们接着做

  • 提交文件到暂存区

运行命令 : git add README.txt

$ git add README.txt

# 此时我们就将 README.txt 提交到了暂存区

我们再次查看状态

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

        new file:   README.txt

可以看到 暂存区里有文件了,是 README.txt

并且提示我们:(use “git rm --cached …” to unstage)

使用git rm --cached ...来取消

我们不需要取消,直接去提交到本地仓库

  • 提交代码

运行命令:git commit ...

$ git commit README.txt -m "添加README"
[master (root-commit) 9cad3c3] 添加README
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.txt

# -m 代表提交描述 后面紧跟着描述

master 当前分支 (主分支)

root-commit代表首次提交的意思,之后的提交就不会有了

9cad3c3 本次提交的唯一标志的前7位(git用SHA-1散列,由40个16进制组成的字符串,是基于文件内容生成的,索引文件是靠这个哦)

添加README 就是我们刚刚提交的描述

提交到远程分支

目标是提交到GitHub上的仓库中

  • 首先我们要在GitHub上创建项目

new项目

填写信息

仓库名字叫做 test,描述是test,公共仓库

下面的 Initialize this repository with a README ,初始化一个READM文件

我们有新建README,所以不需要勾选

点击 Create repository 创建项目

我们采用 SSH的方式提交代码,复制链接

回到git bash上

  • 连接远程仓库

运行命令:git remote add <本地分支名> <远程主机>

$ git remote add origin [email protected]:zhangsan/test.git

使用命令:git remote查看

$ git remote
origin
# 上面就是刚刚添加的
  • 推送到远程仓库

运行命令:git push

$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 448 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To [email protected]:zhangsan/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

git push的一般形式为 git push <远程主机名> <本地分支名> <远程分支名>

参考这里

  • 打开仓库地址,就可以看到我们刚刚提交的内容了

这里采用的是SSH方式,HTTPS也是类似的

当我们在GitHub上创建项目的时候,GitHub会给我们很详细的提示,要仔细看

实用操作

  • 查看提交历史

运行命令:git log

$ git log
commit 9cad3c3c4ab804e7dd6e72b4b0029dda8d885de7
Author: zhangsan <[email protected]>
Date:   Wed Nov 13 23:49:43 2019 +0800

    添加README

看到我们刚刚提交的内容,包括根据文件内容生成的SHA-1散列,提交者用户名,邮箱,日期,提交描述

  • 查看远程连接

运行命令:git remote -v

$ git remote -v
origin  https://github.com/zhangsan/test.git (fetch)
origin  https://github.com/zhangsan/test.git (push)

我这里有设置远程仓库

指定选项 -v,会显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL

参考

git remote 
origin
origin
  • 测试sshkey连接

运行命令:sh -vT [email protected]

sh -vT [email protected]
OpenSSH_7.1p2, OpenSSL 1.0.2d 9 Jul 2015
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [52.74.223.119] port 22.
debug1: Connection established.
debug1: identity file /c/Users/zhangsan/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/zhangsan/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/zhangsan/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/zhangsan/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/zhangsan/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/zhangsan/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/zhangsan/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/zhangsan/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.1
debug1: Remote protocol version 2.0, remote software version babeld-2e9d163d
debug1: no match: babeld-2e9d163d
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client [email protected] <implicit> none
debug1: kex: client->server [email protected] <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5ji8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /c/Users/zhangsan/.ssh/known_hosts:1
Warning: Permanently added the RSA host key for IP address '56.74.123.119' to the list of known hosts.
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/zhangsan/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([52.74.223.119]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
Hi renshen052! You've successfully authenticated, but GitHub does not provide shell access.
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3080, received 2072 bytes, in 6.8 seconds
Bytes per second: sent 453.9, received 305.4
debug1: Exit status 1

  • 采用HTTPS方式连接
git remote add origin https://github.com/zhangsan/test.git

# 推送origin 到 master远程分支
git push -u origin master

# 接着输入用户名和密码即可

参考

https://www.cnblogs.com/sea520/p/10071859.html

你可能感兴趣的:(杂项,git,配置,代码提交)