Git工具使用 github远程仓库

Git工具使用 github远程仓库

1. 下载安装Git

Git下载地址

今天是20210506,最新版本如下
在这里插入图片描述

安装过程中可以选择指定的文件夹,其它的默认选择就行。

2. 使用Git建立本地仓库

新建一个文件夹,因为后面要建立github远程仓库,所以命名为link_github_test
在这里插入图片描述

2.1 将该文件夹变成一个本地仓库

在新建的空白文件夹下右键选择Git Bash Here
在这里插入图片描述

Git bush中输入如下命令

# Git需要知道是谁创建了仓库
git config --global user.name "cwg"
git config --global user.email "[email protected]"
# --global参数表示你这台电脑所有的Git仓库都会使用这个配置
# username填写你的用户名,我填写的是本地管理员名
# useremail填写你的邮箱

# 初始化版本库
git init

文件夹会出现一个新的名为.git的文件夹,表明初始化成功
在这里插入图片描述

3. 使用Git管理本地仓库

在文件中建立一个名为Hello_world.py的文件

print("Hello Git World !")

在这里插入图片描述

# 在Git bush中输入git status检查仓库的状态
git status
cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Hello_world.py

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

3.1 添加并提交文件到仓库中

现在git 知道了有一个新的文件,但是这个文件并不受 git 的管理,怎么办呢

# 在Git bush中输入git add .将所有新的文件添加到git管理的仓库中
# 它不提交文件,只是让git开始关注它们
git add .

# 提交文件 git commit -m "message" -m让message记录到项目的历史记录中
git commit -m "提交第一个文件"

# 提交之后在检查状态
git status
cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git add .

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git commit -m "提交第一个文件"
[master (root-commit) 0020fbc] 提交第一个文件
 1 file changed, 1 insertion(+)
 create mode 100644 Hello_world.py

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

好了,现在Hello_world.py 就在 git的管辖范围内了,现在来看一下提交的历史记录

扫描二维码关注公众号,回复: 16885293 查看本文章
# 获取详细的历史记录
git log
# 获取更简单版本的历史记录
git log --pretty=oneline
# --pretty=oneline指定显示两项最重要的信息:提交的引用ID和提交记录的消息
cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git log
commit 0020fbc6318fae277b4b0c4d864ac6115f5d6449 (HEAD -> master)
Author: cwg <1084467225@qq.com>
Date:   Thu May 6 15:13:33 2021 +0800

    提交第一个文件

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git log --pretty=oneline
0020fbc6318fae277b4b0c4d864ac6115f5d6449 (HEAD -> master) 提交第一个文件

3.2 修改项目文件并进行二次提交

为了展现版本控制的威力,我们对 Hello_world.py 进行修改

print("Hello Git World !")
print("Hello My HN ,you are wonderful tonight !")

然后查看项目现在的状态

git status
cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   Hello_world.py

no changes added to commit (use "git add" and/or "git commit -a")

Changes not staged for commit表示有修改还未提交,那我们来提交一下

# 第二次提交
git commit -am "第二次提交"
# 提交之后查看状态
git status
cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git commit -am "第二次提交"
[master c177ff4] 第二次提交
 1 file changed, 1 insertion(+)

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

3.3 版本控制

提交之后发现代码写错了怎么办,可以回到上一次提交的代码

# 确保自己处于分支master上面
git status
# 查看提交的历史记录
git log --pretty=oneline
# 根据提交历史记录的ID重回到某一次提交的版本
git reset --hard ID前6
cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git log --pretty=oneline
c177ff427f692040f55cea648bf51a7e6876f597 (HEAD -> master) 第二次提交
0020fbc6318fae277b4b0c4d864ac6115f5d6449 提交第一个文件

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git reset --hard 0020fb
HEAD is now at 0020fbc 提交第一个文件

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git status
On branch master
nothing to commit, working tree clean

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git log --pretty=oneline
0020fbc6318fae277b4b0c4d864ac6115f5d6449 (HEAD -> master) 提交第一个文件

在这里插入图片描述

# 检出某一个版本
git checkout ID前6# 然后回到主分支
git checkout master
cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)
$ git checkout 0020fb
Note: switching to '0020fb'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 0020fbc 提交第一个文件

cwg@cwgworkPC MINGW64 /e/Git/link_github_test ((0020fbc...))
$ git checkout master
Switched to branch 'master'

cwg@cwgworkPC MINGW64 /e/Git/link_github_test (master)

3.4 删除仓库

rm -rf .git

4. 建立远程仓库

4.1 本地创建SSH Key

# 创建SSH key 
# github与本地仓库的传输是通过SSH加密的
ssh-keygen -t rsa -C "[email protected]"
# 双引号中输入你的邮箱
# 密钥会保存在主用户目录下的.ssh文件夹中 c/Users/8888/.ssh
10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (master)
$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/10844/.ssh/id_rsa):
Created directory '/c/Users/8888/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/10844/.ssh/id_rsa.
Your public key has been saved in /c/Users/10844/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:W/a1FCOeGxj2UiP32g/sN+v/ZiJOjktxxR3HBPzg+Ts 1084467225@qq.com
The key's randomart image is:
+---[RSA 3072]----+
|             ..=o|
|             .o.+|
|          + =.=+.|
|         . O *oo.|
|        S * * +. |
|         + = O ..|
|        . . = = .|
|         . +...E+|
|          ooo.oBX|
+----[SHA256]-----+

创建的文件如图所示,第一个为私钥,第二个为公钥

在这里插入图片描述

用记事本打开公钥id_rsa.pub,并复制其中内容

在这里插入图片描述

4.2 在github中添加SSH Key

打开你的github,在setrings中找到SSH Key,点击New key,将上文复制的内容粘贴在其中

在这里插入图片描述

# 验证否成功建立连接
ssh -T git@github.com
10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (master)
$ ssh -T git@github.com
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi cwgHN! You've successfully authenticated, but GitHub does not provide shell access.

如果出现You've successfully authenticated, but GitHub does not provide shell access.表示·连接成功

4.3 在github里面建立仓库

在这里插入图片描述
将本地仓库push到新建的github仓库

# push an existing repository from the command line

git remote add origin https://github.com/cwgHN/link_github_tset.git
git branch -M main
git push -u origin main
# 注意不是master 而是main
10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (master)
$ git remote add origin https://github.com/cwgHN/link_github_tset.git

10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (master)
$ git branch -M main

10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (main)
$ git push -u origin main
Logon failed, use ctrl+c to cancel basic credential prompt.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 238 bytes | 119.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/cwgHN/link_github_tset.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

# 中途可能会要求登录github账号

可以看到远程仓库里面已经包含本地仓库的文件了

在这里插入图片描述

4.4 新建的文件如何推送到远程仓库

首先新建一个second_file.py

在这里插入图片描述

# 将文件添加到本地仓库
git add .
# 提交文件
git commit -m "Second"
# 查看状态
git status
10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (main)
$ git add .

10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (main)
$ git commit -m "Second"
[main f3e424c] Second
 1 file changed, 1 insertion(+)
 create mode 100644 second_file.py

10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (main)
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

将文件推送到远程仓库

git push git@github.com:cwgHN/link_github_tset.git  或者  git push -u origin main
10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (main)
$ git push git@github.com:cwgHN/link_github_tset.git
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 246 bytes | 123.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:cwgHN/link_github_tset.git
   d7dc14a..f3e424c  main -> main
10844@CwgMachine MINGW64 /d/Adata/GIt/link_github_test (main)
$ git push -u origin main
Logon failed, use ctrl+c to cancel basic credential prompt.
Everything up-to-date
Branch 'main' set up to track remote branch 'main' from 'origin'.
# 需要登录github账号

在这里插入图片描述

可以看到远程仓库里面有文件了

猜你喜欢

转载自blog.csdn.net/cwg213_code/article/details/116456132