git使用入门(一)

本文详细介绍了Windows版Git的下载与安装,git的基本状态(已提交、已修改、已暂存),并指导了初始化本地库、文件添加与提交、修改文件处理、git log与版本控制、撤销暂存及远程仓库使用。重点讲解了GitHub克隆与推送,以及解决常见问题的方法。
摘要由CSDN通过智能技术生成

在学习git,所以跟着自己写一下笔记,以及学习中出现的一些问题。

windos版本git下载

直接在git的官网进行下来,安装直接无脑next就可以。
在这里插入图片描述

git的三种状态

1.已提交 2.已修改 3.已暂存
git中有一个暂存区和本地库,当我们在当前的git文件夹中添加了一个文件,需要先将其放入暂存区中,然后再放入本地库。
当文件放入暂存区时就是一个已暂存的状态
放入本地库就是一个已提交的状态
还有就是文件进行了修改后重新提交的状态

基本使用步骤

1.初始化本地库

$ git init

这样在文件夹中会创建一个.git的文件夹,这个就是git的本地库
2.新建一个文件

文件名为test.txt

3.将test.txt文件添加到暂存区

git add test.txt
使用git status查看当前状态
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   test.txt

这样会显示有一个新文件test.txt进入暂存区
4.将暂存区的文件放入本地库中

$ git commit -m 'test'
[master 38cc4a6] test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

这样就表示文件放入本地库成功

修改文件和文件提交

对test.txt文件进行修改并显示

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    git01.txt
        modified:   test.txt

显示修改的test.txt文件
重新将test.txt文件放入暂存区

$ git add test.txt

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.txt

可以发现已经修改的文件放入了暂存区
使用git log查看日志

$ git log
commit 06fcb763a852480b0796061ac84c2347105eb80c (HEAD -> master)
Author: ninjasone <xxxx@xxx.com>
Date:   Mon Aug 23 04:03:46 2021 +0800

    git 时光穿梭机

commit 38cc4a6d679586fe08e85e7c8a1810d42225789d
Author: ninjasone <xxxx@xxx.com>
Date:   Sun Aug 22 23:57:47 2021 +0800

    test

commit 49369a62f0a39ea56a3e0e2d1d4c7481f1ca0f45
Author: ninjasone <xxxx@xxx.com>
Date:   Sun Aug 22 23:37:17 2021 +0800

    git版本库初始化与文件提交操作

有三个版本放入本地库中

注意:修改文件后先将文件存入暂存区中,然后再存入本地库中,否则文件修改无效
查看本地库中文件内的内容

使用git diff HEAD – filename查看该文件的变动信息

$ git diff HEAD -- git01.txt
diff --git a/git01.txt b/git01.txt
deleted file mode 100644
index 0007192..0000000
--- a/git01.txt
+++ /dev/null
@@ -1 +0,0 @@
-git版本库初始化与文件提交操作
\ No newline at end of file

如何撤销已经放入暂存区的文件?

使用git reset HEAD 命令就可撤销

版本退回

当对文件进行多次修改时,会产生多个版本

$ git log
commit be116f6a660bc88c5114b667366554f208e9e3cb (HEAD -> master)
Author: ninjasone <xxxx@xxx.com>
Date:   Mon Aug 23 04:23:10 2021 +0800

    git 第三次修改

commit 06fcb763a852480b0796061ac84c2347105eb80c
Author: ninjasone <xxxx@xxx.com>
Date:   Mon Aug 23 04:03:46 2021 +0800

    git 时光穿梭机

commit 38cc4a6d679586fe08e85e7c8a1810d42225789d
Author: ninjasone <xxxx@xxx.com>
Date:   Sun Aug 22 23:57:47 2021 +0800

    test

commit 49369a62f0a39ea56a3e0e2d1d4c7481f1ca0f45
Author: ninjasone <xxxx@xxx.com>
Date:   Sun Aug 22 23:37:17 2021 +0800

    git版本库初始化与文件提交操作

可以看到上图中有多个版本,但是这样看版本输出较为复杂,所以使用简化版本输出的命令

$ git log -4 --pretty=oneline
be116f6a660bc88c5114b667366554f208e9e3cb (HEAD -> master) git 第三次修改
06fcb763a852480b0796061ac84c2347105eb80c git 时光穿梭机
38cc4a6d679586fe08e85e7c8a1810d42225789d test
49369a62f0a39ea56a3e0e2d1d4c7481f1ca0f45 git版本库初始化与文件提交操作

这样每个版本就很清晰了。
从上图看出当前所在版本为第三次修改,那么如何回退到上一个版本中呢?使用下面命令即可

git reset --hard HEAD^
HEAD is now at 06fcb76 git 时光穿梭机

这样版本就回退到上一个版本了,但是要回退到多个版本之前呢?

git reset --hard HEAD~2
HEAD is now at 49369a6 git版本库初始化与文件提交操作

~号后面的数字就是要回退几个版本,2就是回退两个版本

要是进入前面的新版本怎么办呢?
我们可以看到每个版本前面有一串很长的字码,这个就是版本标识符,当我们向前跳版本时就可以利用该标识符进行跳转。

git reset --hard be116f6

但如果git log中没有想要版本的标识符怎么办?

git reflog
49369a6 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~2
06fcb76 HEAD@{1}: reset: moving to HEAD^
be116f6 HEAD@{2}: commit: git 第三次修改
06fcb76 HEAD@{3}: commit: git 时光穿梭机
38cc4a6 HEAD@{4}: commit: test
49369a6 (HEAD -> master) HEAD@{5}: commit (initial): git版本库初始化与文件提交操作

这个命令就可以显示出以前的记录,还有其标识符。这样就可以进行跳转了。

文件删除

对于git而言,文件只要出现过变动就算是被修改过
新建一个git01.txt文件,按顺序放入暂存区和本地库中

git add git01.txt
git commit -m 'git01'

放入本地库之后将git01.txt文件删除,并查看当前状态

git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    git01.txt

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

会发现有一个文件被删除。
但其实只是工作区的文件被删除了,本地库中的文件还没有删除。所以我们要还原这个文件怎么办?

git checkout -- git01.txt

使用该命令就可以还原工作区被删除,因为本地库中的文件没有被删除才可以这样还原啦!

如何彻底的删除git01.txt文件怎么办?

git rm git01.txt

这样就彻底删除了,本地库中的文件也会被删除掉

首先看本地库中有哪些文件

$ git ls-files
git01.txt
test.txt

然后使用git rm命令删除git01.txt文件

git rm git01.txt

再次查看

git ls-files
test.txt

这样就只有test.txt文件了,git01.txt文件被彻底删除了。

远程仓库

开源的远程仓库主要有两个一个是github,另一个是gitee也就是国内的码云。

将远程仓库克隆到本地

首先区github或者gitee中找到我们想要的项目
在这里插入图片描述

在这里插入图片描述
然后点击code按钮
在这里插入图片描述
将git@github.com:xxxxxxx.git的连接复制下来
进入自己本地的git文件夹中,使用git bash here

git clone git@github.com:xxxxxxx.git

但是我们在使用该命令时会出现Please make sure you have the correct access rights and the repository exists.这个错误,导致无法从git上克隆项目。那怎么办呢?
不用急,下面就是解决办法:
1.在git上创建使用名和用户邮箱

$ git config --global user.name "xxx"
$ git config --global user.email "xxx@xxx.com"

2.去c盘中的用户文件夹里找到.ssh文件夹
在这里插入图片描述
就是这个玩意儿。里面会有一个known_hosts文件,将这个文件删除掉。
3.在git中输入下面的命令

$ git  ssh-keygen -t rsa -C "xxx@xxx.com"  这个邮箱就是第一步中设置的邮箱

然后出现下面的提示

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):

直接回车就行。
4.完成以上步骤后重新查看.ssh文件夹,会出现两个玩意
在这里插入图片描述
用记事本打开id_rsa.pub文件,将里面的内容全部复制。
5.打开github,点开下图的Settings
在这里插入图片描述
在account settings里面找到下面这一栏
在这里插入图片描述
点进去,并点开下图中的New SSH key
在这里插入图片描述
将前面从id_rsa.pub文件中复制的东西,全部粘贴到下图的大框框中
在这里插入图片描述
点一下Add SSH key按钮。
6.在git中输入下面命令

$ ssh -T git@github.com

会产生提示,输入yes,回车。

这样就可以了。
重新在git文件夹中使用git

git clone git@github.com:xxxxxxx.git

这样就可以将项目克隆到本地文件夹中了。

对了,由于github不是国内网站,许多小伙伴进不去或者很慢。我的话是使用谷歌浏览器然后加上下面这款插件,就可以很轻松的上github了。
在这里插入图片描述
而且这款插件注册很简单。

将文件推送到远程仓库

1.在自己的github账号这里点击下图选项
在这里插入图片描述
然后点击NEW按钮
在这里插入图片描述
然后进入下面的页面
在这里插入图片描述
Repository name那为项目名称,Description可以写对该项目的描述,可以写也可以不写。然后选择将该项目公开或者私密。最后点击Create repository创建项目。
注:创建的时候可能会出现网页断开的情况,重新连接即可。
下图就是创建好的项目界面
在这里插入图片描述
create a new repository on the command line下面那些命令就是将你的本地文件推送到该远程仓库的步骤。

git init  初始一个新的本地库

在这里插入图片描述
新建一个readme.md文件

$ git status
On branch master

No commits yet

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

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

查看一下,会发现一个未追踪的文件。

$ git add readme.md

$ git commit -m 'readma'
[master (root-commit) 5954bbf] readma
 1 file changed, 1 insertion(+)
 create mode 100644 readme.md

通过命令将其放入本地库

git branch -M main 将master分支绑定main
$ git remote add origin https://github.com/ninjasone/git_test.git

将远程仓库地址与本地库连接并起别名为origin
接下来进行推送

git push -u origin main

但是在推送时会出现fatal: unable to access ‘https://github.com/ninjasone/git_test.git/’: OpenSSL SSL_read: Connection was reset, errno 10054的情况,输入下面的命令

//取消http代理
git config --global --unset http.proxy
//取消https代理 
git config --global --unset https.proxy

然后继续推送,接着可能又出现下面的问题

warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
fatal: 响应状态代码不指示成功: 401 (Unauthorized)。
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://github.com/ninjasone/git_test.git/'

这是因为8月13号开始git不支持使用账号密码登录推送了。所以要用一下的解决办法。
在github上找到seetings->Developer settings->Personal access tokens->Generate new token
如果是第一次的话就点那个Personal access tokens里面可以点击的那串generate什么的。

Note:名字可以自己定义
Expiration:过期时间,可以自己设置
Select scopes:个人使用的话可以全选

然后会生成一串码,复制一下。
在git上重新推一下

git push -u origin main

然后会弹出一个框框,将刚才复制的码粘贴进去,然后sign in就可以推送到远程仓库了。
在这里插入图片描述
github上就有刚刚推上去的东西了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值