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

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

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

  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'



相关推荐

【推荐】一款开源免费、美观实用的后台管理系统模版

如果您对源码&技术感兴趣,请点赞+收藏+转发+关注,大家的支持是我分享最大的动力!!!项目介绍...

Android架构组件-App架构指南,你还不收藏嘛

本指南适用于那些已经拥有开发Android应用基础知识的开发人员,现在想了解能够开发出更加健壮、优质的应用程序架构。首先需要说明的是:AndroidArchitectureComponents翻...

高德地图经纬度坐标批量拾取(高德地图批量查询经纬度)

使用方法在桌面上新建一个index.txt文件,把下面的代码复制进去保存,再把文件名改成index.html保存,双击运行打开即可...

flutter系列之:UI layout简介(flutter ui设计)

简介对于一个前端框架来说,除了各个组件之外,最重要的就是将这些组件进行连接的布局了。布局的英文名叫做layout,就是用来描述如何将组件进行摆放的一个约束。...

Android开发基础入门(一):UI与基础控件

Android基础入门前言:...

iOS的布局体系-流式布局MyFlowLayout

iOS布局体系的概览在我的CSDN博客中的几篇文章分别介绍MyLayout布局体系中的视图从一个方向依次排列的线性布局(MyLinearLayout)、视图层叠且停靠于父布局视图某个位置的框架布局(M...

TDesign企业级开源设计系统越发成熟稳定,支持 Vue3 / 小程序

TDesing发展越来越好了,出了好几套组件库,很成熟稳定了,新项目完全可以考虑使用。...

WinForm实现窗体自适应缩放(winform窗口缩放)

众所周知,...

winform项目——仿QQ即时通讯程序03:搭建登录界面

上两篇文章已经对CIM仿QQ即时通讯项目进行了需求分析和数据库设计。winform项目——仿QQ即时通讯程序01:原理及项目分析...

App自动化测试|原生app元素定位方法

元素定位方法介绍及应用Appium方法定位原生app元素...

61.C# TableLayoutPanel控件(c# tabcontrol)

摘要TableLayoutPanel在网格中排列内容,提供类似于HTML元素的功能。TableLayoutPanel控件允许你将控件放在网格布局中,而无需精确指定每个控件的位置。其单元格...

想要深入学习Android性能优化?看完这篇直接让你一步到位

...

12个python数据处理常用内置函数(python 的内置函数)

在python数据分析中,经常需要对字符串进行各种处理,例如拼接字符串、检索字符串等。下面我将对python中常用的内置字符串操作函数进行介绍。1.计算字符串的长度-len()函数str1='我爱py...

如何用Python程序将几十个PDF文件合并成一个PDF?其实只要这四步

假定你有一个很无聊的任务,需要将几十个PDF文件合并成一个PDF文件。每一个文件都有一个封面作为第一页,但你不希望合并后的文件中重复出现这些封面。即使有许多免费的程序可以合并PDF,很多也只是简单的将...

Python入门知识点总结,Python三大数据类型、数据结构、控制流

Python基础的重要性不言而喻,是每一个入门Python学习者所必备的知识点,作为Python入门,这部分知识点显得很庞杂,内容分支很多,大部分同学在刚刚学习时一头雾水。...