百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

Git基本使用,分布式版本控制 git fench

bigegpt 2024-10-27 08:17 7 浏览

  1. Git基本 命令

设置用户名和邮件

 $ git config --global user.name "用户名"
 $ git config --global user.email "邮箱"

git status 命令用于查看在你上次提交之后是否有对文件进行再次修改。git status -s可以获得简短的输出结果A表示成功

 $ git status -s
 A  test.txt

git init 将文件夹初始化为git仓库

 $ git init
 Initialized empty Git repository in C:/Users/LDH/Desktop/wocao/.git/

git add . 将文件添加到暂存区

 $ git add .
 warning: LF will be replaced by CRLF in test.txt.
 The file will have its original line endings in your working directory

git commit -m “描述”将文件添加到版本库-m用来指定提交信息,这样提交只有一行-m "commit title" -m "commit description"

 $ git commit -m "添加了test.txt文件"
 [master (root-commit) 02959e8] 添加了test.txt文件
  1 file changed, 2 insertions(+)
  create mode 100644 test.txt

为什么要分add和commit两部?因为commit一次可以提交很多文件,所以可以多次add不同的文件

git diff test.txt可以查看文件被修改的具体内容

 LDH@DESKTOP-F8BM77J MINGW64 ~/Desktop/wocao (master)
 $ git diff test.txt
 warning: LF will be replaced by CRLF in test.txt.
 The file will have its original line endings in your working directory
 diff --git a/test.txt b/test.txt
 index bb3f1f2..a938bbb 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -1,3 +1,4 @@
  git is a control version
  git is a free software
 -wocao niubi
 +wocao niubi
 +wocoa

git log --oneline --graph --oneline查看历史记录的简介版本,--graph查看分支结构

 $ git log --oneline --graph
 * 028c201 (HEAD -> master) 修改了test.txt文件1
 * cf6ce9f 修改了test.txt文件
 * 02959e8 添加了test.txt文件

git reset --hard id回退到以前的版本有了--hard会直接修改工作区的内容不加--hard只是修改暂存区

 $ git reset --hard cf6ce9f
 HEAD is now at cf6ce9f 修改了test.txt文件

git reflog可以看到已经删除的提交命令

 $ git reflog
 cf6ce9f (HEAD -> master) HEAD@{0}: reset: moving to cf6ce9f
 028c201 HEAD@{1}: commit: 修改了test.txt文件1
 cf6ce9f (HEAD -> master) HEAD@{2}: commit: 修改了test.txt文件
 02959e8 HEAD@{3}: commit (initial): 添加了test.txt文件

git checkout test.txt把test.txt文件在工作区的修改全部撤销回到最近一次git add 或git commit的状态

 $ git checkout test.txt
 Updated 1 path from the index

git reset HEAD test.txt将test.txt从暂存区撤销,放回工作区

 $ git reset HEAD .
 Unstaged changes after reset:
 M       test.txt

git rm 文件名称 git commit 从版本库中删除文件

 $ git rm wocao.txt
 rm 'wocao.txt'
 $ git commit -m "删除wocao"
 [master 36a7ff6] 删除wocao
  1 file changed, 0 insertions(+), 0 deletions(-)
  delete mode 100644 wocao.txt

远程仓库

git remote -v查看是否有远程仓库

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

git remote rm origin1删除名为origin1的远程库

 $ git remote rm origin1
  1. 第一步:创建sshkey id_rsa是私钥不能告诉任何人 id_rsa.pub是公钥可以告诉任何人 $ ssh-keygen -t rsa -C "2815843603@qq.com"
  2. 第二步:登陆github,打开设置,将id_rsa.pub中的内容添加到ssh key中
  3. 关联远程库,origin是远程库的名字,默认 $ git remote add origin https://github.com/ldh55/test.git
    error: remote origin already exists.
  4. 将本地库的内容推送到远程库上 $ git push origin master
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 347 bytes | 347.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    To https://github.com/ldh55/test.git
    101b930..c32db90 master -> master

从远程库克隆

 $ git clone https://gitee.com/dong-hai-luo/niubi.git
 Cloning into 'niubi'...
 info: detecting host provider for 'https://gitee.com/'...
 info: detecting host provider for 'https://gitee.com/'...
 remote: Enumerating objects: 4, done.
 remote: Counting objects: 100% (4/4), done.
 remote: Compressing objects: 100% (4/4), done.
 remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
 Receiving objects: 100% (4/4), done.

git pull origin3 master --allow-unrelated-histories先pull在 push pull = fetch+merge

 $ git pull origin3 master --allow-unrelated-histories
 info: detecting host provider for 'https://gitee.com/'...
 info: detecting host provider for 'https://gitee.com/'...
 From https://gitee.com/dong-hai-luo/life
  * branch            master     -> FETCH_HEAD
 Already up to date.
 Merge made by the 'recursive' strategy.

分支管理

git checkout -b develop创建一个新的分支并切换到新的分支

 $ git checkout -b develop
 Switched to a new branch 'develop'


Git基本 命令

设置用户名和邮件

 $ git config --global user.name "ldh"
 $ git config --global user.email "2815843603@qq.com"

git status 命令用于查看在你上次提交之后是否有对文件进行再次修改。git status -s可以获得简短的输出结果A表示成功

 $ git status -s
 A  test.txt

git init 将文件夹初始化为git仓库

 $ git init
 Initialized empty Git repository in C:/Users/LDH/Desktop/wocao/.git/

git add . 将文件添加到暂存区

 $ git add .
 warning: LF will be replaced by CRLF in test.txt.
 The file will have its original line endings in your working directory

git commit -m “描述”将文件添加到版本库-m用来指定提交信息,这样提交只有一行-m "commit title" -m "commit description"

 $ git commit -m "添加了test.txt文件"
 [master (root-commit) 02959e8] 添加了test.txt文件
  1 file changed, 2 insertions(+)
  create mode 100644 test.txt

为什么要分add和commit两部?因为commit一次可以提交很多文件,所以可以多次add不同的文件

git diff test.txt可以查看文件被修改的具体内容

 LDH@DESKTOP-F8BM77J MINGW64 ~/Desktop/wocao (master)
 $ git diff test.txt
 warning: LF will be replaced by CRLF in test.txt.
 The file will have its original line endings in your working directory
 diff --git a/test.txt b/test.txt
 index bb3f1f2..a938bbb 100644
 --- a/test.txt
 +++ b/test.txt
 @@ -1,3 +1,4 @@
  git is a control version
  git is a free software
 -wocao niubi
 +wocao niubi
 +wocoa

git log --oneline --graph --oneline查看历史记录的简介版本,--graph查看分支结构

 $ git log --oneline --graph
 * 028c201 (HEAD -> master) 修改了test.txt文件1
 * cf6ce9f 修改了test.txt文件
 * 02959e8 添加了test.txt文件

git reset --hard id回退到以前的版本有了--hard会直接修改工作区的内容不加--hard只是修改暂存区

 $ git reset --hard cf6ce9f
 HEAD is now at cf6ce9f 修改了test.txt文件

git reflog可以看到已经删除的提交命令

 $ git reflog
 cf6ce9f (HEAD -> master) HEAD@{0}: reset: moving to cf6ce9f
 028c201 HEAD@{1}: commit: 修改了test.txt文件1
 cf6ce9f (HEAD -> master) HEAD@{2}: commit: 修改了test.txt文件
 02959e8 HEAD@{3}: commit (initial): 添加了test.txt文件

git checkout test.txt把test.txt文件在工作区的修改全部撤销回到最近一次git add 或git commit的状态

 $ git checkout test.txt
 Updated 1 path from the index

git reset HEAD test.txt将test.txt从暂存区撤销,放回工作区

 $ git reset HEAD .
 Unstaged changes after reset:
 M       test.txt

git rm 文件名称 git commit 从版本库中删除文件

 $ git rm wocao.txt
 rm 'wocao.txt'
 $ git commit -m "删除wocao"
 [master 36a7ff6] 删除wocao
  1 file changed, 0 insertions(+), 0 deletions(-)
  delete mode 100644 wocao.txt

远程仓库

git remote -v查看是否有远程仓库

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

git remote rm origin1删除名为origin1的远程库

 $ git remote rm origin1
  1. 第一步:创建sshkey id_rsa是私钥不能告诉任何人 id_rsa.pub是公钥可以告诉任何人 $ ssh-keygen -t rsa -C "2815843603@qq.com"
  2. 第二步:登陆github,打开设置,将id_rsa.pub中的内容添加到ssh key中
  3. 关联远程库,origin是远程库的名字,默认 $ git remote add origin https://github.com/ldh55/test.git
    error: remote origin already exists.
  4. 将本地库的内容推送到远程库上 $ git push origin master
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 347 bytes | 347.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    To https://github.com/ldh55/test.git
    101b930..c32db90 master -> master

从远程库克隆

 $ git clone https://gitee.com/dong-hai-luo/niubi.git
 Cloning into 'niubi'...
 info: detecting host provider for 'https://gitee.com/'...
 info: detecting host provider for 'https://gitee.com/'...
 remote: Enumerating objects: 4, done.
 remote: Counting objects: 100% (4/4), done.
 remote: Compressing objects: 100% (4/4), done.
 remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
 Receiving objects: 100% (4/4), done.

git pull origin3 master --allow-unrelated-histories先pull在 push pull = fetch+merge

 $ git pull origin3 master --allow-unrelated-histories
 info: detecting host provider for 'https://gitee.com/'...
 info: detecting host provider for 'https://gitee.com/'...
 From https://gitee.com/dong-hai-luo/life
  * branch            master     -> FETCH_HEAD
 Already up to date.
 Merge made by the 'recursive' strategy.

分支管理

git checkout -b develop创建一个新的分支并切换到新的分支

 $ git checkout -b develop
 Switched to a new branch 'develop'



相关推荐

AI「自我复制」能力曝光,RepliBench警示:大模型正在学会伪造身份

科幻中AI自我复制失控场景,正成为现实世界严肃的研究课题。英国AISI推出RepliBench基准,分解并评估AI自主复制所需的四大核心能力。测试显示,当前AI尚不具备完全自主复制能力,但在获取资源...

【Python第三方库安装】介绍8种情况,这里最全看这里就够了!

**本图文作品主要解决CMD或pycharm终端下载安装第三方库可能出错的问题**本作品介绍了8种安装方法,这里最全的python第三方库安装教程,简单易上手,满满干货!希望大家能愉快地写代码,而不要...

pyvips,一个神奇的 Python 库!(pythonvip视频)

大家好,今天为大家分享一个神奇的Python库-pyvips。在图像处理领域,高效和快速的图像处理工具对于开发者来说至关重要。pyvips是一个强大的Python库,基于libvips...

mac 安装tesseract、pytesseract以及简单使用

一.tesseract-OCR的介绍1.tesseract-OCR是一个开源的OCR引擎,能识别100多种语言,专门用于对图片文字进行识别,并获取文本。但是它的缺点是对手写的识别能力比较差。2.用te...

实测o3/o4-mini:3分钟解决欧拉问题,OpenAI最强模型名副其实!

号称“OpenAI迄今为止最强模型”,o3/o4-mini真实能力究竟如何?就在发布后的几小时内,网友们的第一波实测已新鲜出炉。最强推理模型o3,即使遇上首位全职提示词工程师RileyGoodsid...

使用Python将图片转换为字符画并保存到文件

字符画(ASCIIArt)是将图片转换为由字符组成的艺术作品。利用Python,我们可以轻松实现图片转字符画的功能。本教程将带你一步步实现这个功能,并详细解释每一步的代码和实现原理。环境准备首先,你...

5分钟-python包管理器pip安装(python pip安装包)

pip是一个现代的,通用、普遍的Python包管理工具。提供了对Python包的查找、下载、安装、卸载的功能,是Python开发的基础。第一步:PC端打开网址:选择gz后缀的文件下载第二步:...

网络问题快速排查,你也能当好自己家的网络攻城狮

前面写了一篇关于网络基础和常见故障排查的,只列举了工具。没具体排查方式。这篇重点把几个常用工具的组合讲解一下。先有请今天的主角:nslookup及dig,traceroute,httping,teln...

终于把TCP/IP 协议讲的明明白白了,再也不怕被问三次握手了

文:涤生_Woo下周就开始和大家成体系的讲hadoop了,里面的每一个模块的技术细节我都会涉及到,希望大家会喜欢。当然了你也可以评论或者留言自己喜欢的技术,还是那句话,希望咱们一起进步。今天周五,讲讲...

记一次工控触摸屏故障的处理(工控触摸屏维修)

先说明一下,虽然我是自动化专业毕业,但已经很多年不从事现场一线的工控工作了。但自己在单位做的工作也牵涉到信息化与自动化的整合,所以平时也略有关注。上一周一个朋友接到一个活,一家光伏企业用于启动机组的触...

19、90秒快速“读懂”路由、交换命令行基础

命令行视图VRP分层的命令结构定义了很多命令行视图,每条命令只能在特定的视图中执行。本例介绍了常见的命令行视图。每个命令都注册在一个或多个命令视图下,用户只有先进入这个命令所在的视图,才能运行相应的命...

摄像头没图像的几个检查方法(摄像头没图像怎么修复)

背景描述:安防监控项目上,用户的摄像头运行了一段时间有部分摄像头不能进行预览,需要针对不能预览的摄像头进行排查,下面列出几个常见的排查方法。问题解决:一般情况为网络、供电、设备配置等情况。一,网络检查...

小谈:必需脂肪酸(必需脂肪酸主要包括)

必需脂肪酸是指机体生命活动必不可少,但机体自身又不能合成,必需由食物供给的多不饱和脂肪酸(PUFA)。必需脂肪酸主要包括两种,一种是ω-3系列的α-亚麻酸(18:3),一种是ω-6系列的亚油酸(18:...

期刊推荐:15本sci四区易发表的机械类期刊

  虽然,Sci四区期刊相比收录在sci一区、二区、三区的期刊来说要求不是那么高,投稿起来也相对容易一些。但,sci四区所收录的期刊中每本期刊的投稿难易程度也是不一样的。为方便大家投稿,本文给大家推荐...

be sick of 用法考察(be in lack of的用法)

besick表示病了,做谓语.本身是形容词,有多种意思.最通常的是:生病,恶心,呕吐,不适,晕,厌烦,无法忍受asickchild生病的孩子Hermother'sverysi...