git 简单使用

git 简单使用

  • [git 安装](#git 安装)
  • [git 使用](#git 使用)
    • [1. 从git仓库克隆项目](#1. 从git仓库克隆项目)
    • [2. git checout -b 创建新分支](#2. git checout -b 创建新分支)
      • [(1) git checkout 切换分支](#(1) git checkout 切换分支)
      • [(2) git checkout 切换提交](#(2) git checkout 切换提交)
      • [(3) git checkout 清空当前工作区修改](#(3) git checkout 清空当前工作区修改)
    • [3. git status 查看当前分支的状态](#3. git status 查看当前分支的状态)
    • [4. git diff 查看修改的具体内容](#4. git diff 查看修改的具体内容)
    • [5. git add 添加更改](#5. git add 添加更改)
    • [6. git restore 还原修改内容](#6. git restore 还原修改内容)
    • [7. git commit 提交修改](#7. git commit 提交修改)
    • [7. git config 配置邮箱和名字](#7. git config 配置邮箱和名字)
    • [8. git log 查看提交日志](#8. git log 查看提交日志)
    • [9. git push 上传](#9. git push 上传)
    • [10. git reset 回退此次提交](#10. git reset 回退此次提交)
  • [git 创建仓库](#git 创建仓库)

操作系统:linux(ubuntu,debian,kali)

git 安装

shell 复制代码
sudo apt update
sudo apt install git

git 使用

1. 从git仓库克隆项目

shell 复制代码
git clone https://git.aaronl.cn/Aaron/my-blog.git
shell 复制代码
cd my-blog

2. git checout -b 创建新分支

执行 git checout 前需要确保当前工作区是干净的,即没有需要保存提交的修改

创建 test 新分支后会切换到此分支

shell 复制代码
git checkout -b test

(1) git checkout 切换分支

使用git checkout 分支名可以切换到指定的分支,如:main

shell 复制代码
git checkout main

(2) git checkout 切换提交

使用git checkout 提交ID可以切换到指定的分支,如:b2f37d3e49ba628363381cb82a5aefa73e8701c9

shell 复制代码
git checkout b2f37d3e49ba628363381cb82a5aefa73e8701c9

(3) git checkout 清空当前工作区修改

有时我们因为调试等原因修改了很多文件内容,结束后要删除所有的更改,可以快速实现 :

shell 复制代码
git checkout .

3. git status 查看当前分支的状态

shell 复制代码
vi README.md # 随意修改一些内容
shell 复制代码
git status
shell 复制代码
On branch test
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:   README.md

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

4. git diff 查看修改的具体内容

若不加具体文件,将展示所有修改的内容

shell 复制代码
git diff README.md
patch 复制代码
diff --git a/README.md b/README.md
index f9f29cb..cbcdf1b 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@
 # myBlog

+my name is Aaron
+
 我的博客

5. git add 添加更改

重新修改README.md,并将修改git add保存
git add . 可以添加将当前目录及子目录 所有修改都保存,但是不建议

一般是使用git status查看修改,根据需要指定根据文件逐个添加 或者指定文件夹(可以将此文件夹的所有修改都添加),除非你每次都了解并清晰自己的每个修改内容

shell 复制代码
git add README.md
git status

6. git restore 还原修改内容

需要指定文件,将修改的内容删除

如果 还没有 git add保存

shell 复制代码
git restore README.md
git status

如果已经 git add保存

shell 复制代码
git restore --stage README.md
git status

7. git commit 提交修改

需要描述 一下本次提交的内容 ,最好简洁明了

直接使用 git commit

shell 复制代码
git commit -m "Motify README.md"

若需要更详细的描述,可以这样:

shell 复制代码
git commit

这将使用nano打开一个文件,写完后,Ctrl + o保存,Ctrl + x 退出

若没有配置提交者的信息 ,将无法提交
看下一点的内容,配置一下

配置好后,就可以提交了

7. git config 配置邮箱和名字

shell 复制代码
git config --global user.email "123456789@qq.com"
git config --global user.name "Aaron"

8. git log 查看提交日志

shell 复制代码
git status

9. git push 上传

使用我的例子是无法上传的,这是我自己的仓库,知道此流程即可

shell 复制代码
# git push
# 因为新创建分支的原因,需要这样上传,上游仓库就会多了test分支了
git push --set-upstream origin test

10. git reset 回退此次提交

如果放弃此次修改

首先git log 查看commit日志

shell 复制代码
git log
shell 复制代码
commit d7991bb05db6ffb9bce7b5124849b9a9442a264b (HEAD -> test)
Author: Aaron <1242129679@qq.com>
Date:   Sun Apr 14 23:03:36 2024 +0800

    Motify README.md

commit 269a380e596af4fad15dff4746345e3961493ff3 (origin/main, origin/HEAD, main)
Author: Aaron <1242129679@qq.com>
Date:   Wed Apr 3 21:26:44 2024 +0800

    blog init
...

然后回退到上一个提交id即可

shell 复制代码
git reset 269a380e596af4fad15dff4746345e3961493ff3 

最后使用git restore README.md就可以了

当然如果修改的内容过多git checkout .就可以了(谨慎 :它会把所有的修改全部删除)

git 创建仓库

目的 :在自己的上游仓库创建一个名为test的仓库
github,gitee,gitlab,gitee

访问不了github 的话,可以用其它的

我后面会写一个部署自己的gitea的文章

不需要在自己的上游创建git仓库:

shell 复制代码
echo "# git repo" >> README.md
git init .
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/Aaron-xx/test.git(换成自己的仓库连接)
git push -u origin main

需要先在自己的上游创建git仓库:

shell 复制代码
git remote add origin https://github.com/Aaron-xx/test.git(换成自己的仓库连接)
git branch -M main
git push -u origin main

至此,git相关的内容就结束了

能力一般,水平有限,希望能帮到您

相关推荐
权咚2 小时前
阿权的开发经验小集
git·ios·xcode
轻松Ai享生活8 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr9 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神5559 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆9 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++10 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
獭.獭.11 小时前
Linux -- 信号【上】
linux·运维·服务器
hashiqimiya12 小时前
centos配置环境变量jdk
linux·运维·centos
hashiqimiya12 小时前
权限更改centos中系统文件无法创建文件夹,使用命令让普通用户具备操作文件夹
linux
逆小舟16 小时前
【Linux】人事档案——用户及组管理
linux·c++