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

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

liebian365 2025-01-09 14:12 16 浏览 0 评论

  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'



相关推荐

4万多吨豪华游轮遇险 竟是因为这个原因……

(观察者网讯)4.7万吨豪华游轮搁浅,竟是因为油量太低?据观察者网此前报道,挪威游轮“维京天空”号上周六(23日)在挪威近海发生引擎故障搁浅。船上载有1300多人,其中28人受伤住院。经过数天的调...

“菜鸟黑客”必用兵器之“渗透测试篇二”

"菜鸟黑客"必用兵器之"渗透测试篇二"上篇文章主要针对伙伴们对"渗透测试"应该如何学习?"渗透测试"的基本流程?本篇文章继续上次的分享,接着介绍一下黑客们常用的渗透测试工具有哪些?以及用实验环境让大家...

科幻春晚丨《震动羽翼说“Hello”》两万年星间飞行,探测器对地球的最终告白

作者|藤井太洋译者|祝力新【编者按】2021年科幻春晚的最后一篇小说,来自大家喜爱的日本科幻作家藤井太洋。小说将视角放在一颗太空探测器上,延续了他一贯的浪漫风格。...

麦子陪你做作业(二):KEGG通路数据库的正确打开姿势

作者:麦子KEGG是通路数据库中最庞大的,涵盖基因组网络信息,主要注释基因的功能和调控关系。当我们选到了合适的候选分子,单变量研究也已做完,接着研究机制的时便可使用到它。你需要了解你的分子目前已有哪些...

知存科技王绍迪:突破存储墙瓶颈,详解存算一体架构优势

智东西(公众号:zhidxcom)编辑|韦世玮智东西6月5日消息,近日,在落幕不久的GTIC2021嵌入式AI创新峰会上,知存科技CEO王绍迪博士以《存算一体AI芯片:AIoT设备的算力新选择》...

每日新闻播报(September 14)_每日新闻播报英文

AnOscarstatuestandscoveredwithplasticduringpreparationsleadinguptothe87thAcademyAward...

香港新巴城巴开放实时到站数据 供科技界研发使用

中新网3月22日电据香港《明报》报道,香港特区政府致力推动智慧城市,鼓励公私营机构开放数据,以便科技界研发使用。香港运输署21日与新巴及城巴(两巴)公司签署谅解备忘录,两巴将于2019年第3季度,开...

5款不容错过的APP: Red Bull Alert,Flipagram,WifiMapper

本周有不少非常出色的app推出,鸵鸟电台做了一个小合集。亮相本周榜单的有WifiMapper's安卓版的app,其中包含了RedBull的一款新型闹钟,还有一款可爱的怪物主题益智游戏。一起来看看我...

Qt动画效果展示_qt显示图片

今天在这篇博文中,主要实践Qt动画,做一个实例来讲解Qt动画使用,其界面如下图所示(由于没有录制为gif动画图片,所以请各位下载查看效果):该程序使用应用程序单窗口,主窗口继承于QMainWindow...

如何从0到1设计实现一门自己的脚本语言

作者:dong...

三年级语文上册 仿写句子 需要的直接下载打印吧

描写秋天的好句好段1.秋天来了,山野变成了美丽的图画。苹果露出红红的脸庞,梨树挂起金黄的灯笼,高粱举起了燃烧的火把。大雁在天空一会儿写“人”字,一会儿写“一”字。2.花园里,菊花争奇斗艳,红的似火,粉...

C++|那些一看就很简洁、优雅、经典的小代码段

目录0等概率随机洗牌:1大小写转换2字符串复制...

二年级上册语文必考句子仿写,家长打印,孩子照着练

二年级上册语文必考句子仿写,家长打印,孩子照着练。具体如下:...

一年级语文上 句子专项练习(可打印)

...

亲自上阵!C++ 大佬深度“剧透”:C++26 将如何在代码生成上对抗 Rust?

...

取消回复欢迎 发表评论: