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

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

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

  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'



相关推荐

Linux文件系统操作常用命令(linux文件内容操作命令)

在Linux系统中,有一些常用的文件系统操作命令,以下是这些命令的介绍和作用:#切换目录,其中./代表当前目录,../代表上一级目录cd#查看当前目录里的文件和文件夹ls#...

别小看tail 命令,它难倒了技术总监

我把自己以往的文章汇总成为了Github,欢迎各位大佬star...

lnav:基于 Linux 的高级控制台日志文件查看器

lnav是一款开源的控制台日志文件查看器,专为Linux和Unix-like系统设计。它通过自动检测日志文件的格式,提取时间戳、日志级别等关键信息,并将多个日志文件的内容按时间顺序合并显示,...

声明式与命令式代码(声明模式和命令模式)

编程范式中的术语和差异信不信由你,你可能已经以开发人员的身份使用了多种编程范例。因为没有什么比用编程理论招待朋友更有趣的了,所以这篇文章可以帮助您认识代码中的流行范例。命令式编程命令式编程是我们从As...

linux中的常用命令(linux常用命令和作用)

linux中的常用命令linux中的命令统称shell命令shell是一个命令行解释器,将用户命令解析为操作系统所能理解的指令,实现用户与操作系统的交互shell终端:我们平时输入命令,执行程序的那个...

提高工作效率的--Linux常用命令,能够决解95%以上的问题

点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf...

如何限制他人操作自己的电脑?(如何控制别人的电脑不让发现)

这段时间,小猪罗志祥正处于风口浪尖,具体是为啥?还不知道的小伙伴赶紧去补一下最近的娱乐圈八卦~简单来说,就是我们的小罗同事,以自己超强的体力,以及超强的时间管理能力,重新定义了「多人运动」的含义,重新...

最通俗易懂的命令模式讲解(命令模式百科)

我们先不讲什么是命令模式,先通过一个场景来引出命令模式,看看命令模式能解决什么样的问题。现在有一个渣男张三,他有还几个女朋友,你现在是不是还是单身狗,你就说你气不气?然后他需要每天分别叫几个女朋友起床...

互联网大厂后端必看!Spring Boot 中Runtime执行与停止命令?

你是否曾在使用SpringBoot开发项目时,遇到需要执行系统命令的场景?比如调用脚本进行文件处理,又或是启动外部程序?很多后端开发人员会使用Processexec=Runtime.get...

Linux 常用命令(linux常用的20个命令面试)

日志排查类操作命令...

Java字节码指令:if_icmpgt(0xA3)(java字节码使用的汇编语言)

if_icmpgt是Java字节码中的一条条件跳转指令,其全称是"IfIntegerCompareGreaterThan"。它用于比较两个整数值的大小。如果栈顶的第一个...

外贸干货|如何增加领英的曝光量和询盘

#跨境电商#...

golang执行linux命令(golang调用shell脚本)

需求需要通过openssl生成rsa秘钥,然后保存该秘钥。代码实例packagemainimport("io/ioutil""bytes"&...

LINUX磁盘挂载(linux磁盘挂载到windows)

1、使用root用户查看磁盘挂载情况:fdisk-l2、使用df查看当前磁盘挂载情况,根据和fdisk-l的结果进行对比,查看还有那些磁盘未使用3、挂载:mount磁盘挂载路径...

Linux命令学习——nl命令(linux ln命令的使用)

nl命令主要功能为每一个文件添加行号,每一个输入的文件添加行号后发送到标准输出。当没有文件或文件为-时,读取标准输入...