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

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

wptr33 2025-01-08 17:48 20 浏览

  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'



相关推荐

Python自动化脚本应用与示例(python办公自动化脚本)

Python是编写自动化脚本的绝佳选择,因其语法简洁、库丰富且跨平台兼容性强。以下是Python自动化脚本的常见应用场景及示例,帮助你快速上手:一、常见自动化场景文件与目录操作...

Python文件操作常用库高级应用教程

本文是在前面《Python文件操作常用库使用教程》的基础上,进一步学习Python文件操作库的高级应用。一、高级文件系统监控1.1watchdog库-实时文件系统监控安装与基本使用:...

Python办公自动化系列篇之六:文件系统与操作系统任务

作为高效办公自动化领域的主流编程语言,Python凭借其优雅的语法结构、完善的技术生态及成熟的第三方工具库集合,已成为企业数字化转型过程中提升运营效率的理想选择。该语言在结构化数据处理、自动化文档生成...

14《Python 办公自动化教程》os 模块操作文件与文件夹

在日常工作中,我们经常会和文件、文件夹打交道,比如将服务器上指定目录下文件进行归档,或将爬虫爬取的数据根据时间创建对应的文件夹/文件,如果这些还依靠手动来进行操作,无疑是费时费力的,这时候Pyt...

python中os模块详解(python os.path模块)

os模块是Python标准库中的一个模块,它提供了与操作系统交互的方法。使用os模块可以方便地执行许多常见的系统任务,如文件和目录操作、进程管理、环境变量管理等。下面是os模块中一些常用的函数和方法:...

21-Python-文件操作(python文件的操作步骤)

在Python中,文件操作是非常重要的一部分,它允许我们读取、写入和修改文件。下面将详细讲解Python文件操作的各个方面,并给出相应的示例。1-打开文件...

轻松玩转Python文件操作:移动、删除

哈喽,大家好,我是木头左!Python文件操作基础在处理计算机文件时,经常需要执行如移动和删除等基本操作。Python提供了一些内置的库来帮助完成这些任务,其中最常用的就是os模块和shutil模块。...

Python 初学者练习:删除文件和文件夹

在本教程中,你将学习如何在Python中删除文件和文件夹。使用os.remove()函数删除文件...

引人遐想,用 Python 获取你想要的“某个人”摄像头照片

仅用来学习,希望给你们有提供到学习上的作用。1.安装库需要安装python3.5以上版本,在官网下载即可。然后安装库opencv-python,安装方式为打开终端输入命令行。...

Python如何使用临时文件和目录(python目录下文件)

在某些项目中,有时候会有大量的临时数据,比如各种日志,这时候我们要做数据分析,并把最后的结果储存起来,这些大量的临时数据如果常驻内存,将消耗大量内存资源,我们可以使用临时文件,存储这些临时数据。使用标...

Linux 下海量文件删除方法效率对比,最慢的竟然是 rm

Linux下海量文件删除方法效率对比,本次参赛选手一共6位,分别是:rm、find、findwithdelete、rsync、Python、Perl.首先建立50万个文件$testfor...

Python 开发工程师必会的 5 个系统命令操作库

当我们需要编写自动化脚本、部署工具、监控程序时,熟练操作系统命令几乎是必备技能。今天就来聊聊我在实际项目中高频使用的5个系统命令操作库,这些可都是能让你效率翻倍的"瑞士军刀"。一...

Python常用文件操作库使用详解(python文件操作选项)

Python生态系统提供了丰富的文件操作库,可以处理各种复杂的文件操作需求。本教程将介绍Python中最常用的文件操作库及其实际应用。一、标准库核心模块1.1os模块-操作系统接口主要功能...

11. 文件与IO操作(文件io和网络io)

本章深入探讨Go语言文件处理与IO操作的核心技术,结合高性能实践与安全规范,提供企业级解决方案。11.1文件读写11.1.1基础操作...

Python os模块的20个应用实例(python中 import os模块用法)

在Python中,...