Git源码管理

参考视频:16-git的日志以及版本管理_哔哩哔哩_bilibili

参考博客:Git && Docker 学习笔记-CSDN博客

目录

简介

个人操作初始化

初始化git目录

查看生成的git目录文件

配置git工作目录的用户信息

查看工作区的状态,生成文件的状态

添加文件到暂存区、仓库区

仓库区的版本回退和恢复

暂存区回退到工作区

工作区及暂存区修改的撤销

远程仓库克隆到本地初始化

远程仓库的文件创建与上传及信息查询

Git 多人操作

多人协助冲突的发生及解决

标签

分支操作


简介

当前最先进的分布式版本控制系统,作用于源代码管理,方便多人协同开发,便于版本控制

个人操作初始化
初始化git目录

git init

root@pass:/home/pass/Desktop/mytest# git init          初始化目录生成 .git目录
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/pass/Desktop/mytest/.git/

查看生成的git目录文件

ll         查看隐藏目录命令

root@pass:/home/pass/Desktop/mytest# ll         查看隐藏目录命令ll
total 12
drwxr-xr-x 3 root root 4096  3月  1 20:00 ./
drwxr-xr-x 4 pass pass 4096  3月  1 20:00 ../
drwxr-xr-x 7 root root 4096  3月  1 20:00 .git/      以点开头的目录系统不可见
root@pass:/home/pass/Desktop/mytest# cd .git/
root@pass:/home/pass/Desktop/mytest/.git# ls
branches  config  description  HEAD  hooks  info  objects  refs

配置git工作目录的用户信息

git config user.name pass    用户姓名

git config user.email pass@qq.com  用户邮件

root@pass:/home/pass/Desktop/mytest/.git# git config user.name pass    用户姓名
root@pass:/home/pass/Desktop/mytest/.git# git config user.email pass@qq.com  用户邮件
root@pass:/home/pass/Desktop/mytest/.git# cat config     查看用户信息
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[user]
        name = pass
        email = pass@qq.com

查看工作区的状态,生成文件的状态

git status      查看当前状态

root@pass:/home/pass/Desktop/mytest# git status      查看当前状态
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
root@pass:/home/pass/Desktop/mytest# touch file      工作区创建文件
root@pass:/home/pass/Desktop/mytest# git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file

nothing added to commit but untracked files present (use "git add" to track)

添加文件到暂存区、仓库区

git add file                             添加文件file到暂存区 "."当前全部文件

git commit -m 'first commit'   提交到仓库[参数-m 备注信息]

git log                                    查看提交日志

root@pass:/home/pass/Desktop/mytest# git add file   添加文件file 到暂存区"."当前全部文件
root@pass:/home/pass/Desktop/mytest# git status     查看当前状态
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   file

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        me

root@pass:/home/pass/Desktop/mytest# git commit -m 'first commit'   提交到仓库[-m 备注]
[master (root-commit) 474ca08] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file
root@pass:/home/pass/Desktop/mytest# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        me

nothing added to commit but untracked files present (use "git add" to track)
root@pass:/home/pass/Desktop/mytest# git log                            查看日志
commit 474ca0871818584972749d6b84d2d814825d8b2f (HEAD -> master)
Author: pass <pass@qq.com>
Date:   Fri Mar 1 20:08:06 2024 +0800

    first commit

仓库区的版本回退和恢复

回退版本

  • HEAD   当前最新版本
  • HEAD^  当前最新版本的前一个版本
  • HEAD^^ 当前最新版本的前两个版本,依次类推
  • HEAD~1 当前最新版本的前一个版本
  • HEAD~10 当前版本的前10个版本,依次类推

eg: 

git reset  --hard  HEAD~    回退到上一个版本

git reset  --hard  版本号     回退到指定的版本

git reflog                             查看所有的版本记录

root@pass:/home/pass/Desktop/mytest# git reflog   查看所有的版本记录
c9097f1 (HEAD -> master) HEAD@{0}: commit: second commit
474ca08 HEAD@{1}: commit (initial): first commit
root@pass:/home/pass/Desktop/mytest# git reset --hard HEAD^   回退到上一版本
HEAD is now at 474ca08 first commit
root@pass:/home/pass/Desktop/mytest# git log                             查看提交的日志
commit 474ca0871818584972749d6b84d2d814825d8b2f (HEAD -> master)
Author: pass <pass@qq.com>
Date:   Fri Mar 1 20:08:06 2024 +0800

    first commit
root@pass:/home/pass/Desktop/mytest# git reflog
474ca08 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
c9097f1 HEAD@{1}: commit: second commit
474ca08 (HEAD -> master) HEAD@{2}: commit (initial): first commit
root@pass:/home/pass/Desktop/mytest# git reset --hard c9097f1   回退到第二个版本
HEAD is now at c9097f1 second commit

暂存区回退到工作区

git reset HEAD [file]

root@pass:/home/pass/Desktop/mytest# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   learn.txt
        new file:   me

root@pass:/home/pass/Desktop/mytest# git reset HEAD learn.txt
root@pass:/home/pass/Desktop/mytest# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   me

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        learn.txt

工作区及暂存区修改的撤销

git checkout learn.txt   撤销工作区及暂存区的修改

root@pass:/home/pass/Desktop/mytest# git status   暂存区的文件learn.txt
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   learn.txt
        new file:   me

root@pass:/home/pass/Desktop/mytest# echo 888 >> learn.txt  修改工作区的文件
root@pass:/home/pass/Desktop/mytest# cat learn.txt
666
999
888
root@pass:/home/pass/Desktop/mytest# git checkout learn.txt   撤销工作区及暂存区的修改
Updated 1 path from the index
root@pass:/home/pass/Desktop/mytest# cat learn.txt
666
999

远程仓库克隆到本地初始化

git clone git@github.com:past-plus/learn_test.git

root@pass:/home/pass/remote_learn# git clone git@github.com:past-plus/learn_test.git
Cloning into 'learn_test'...   克隆远程仓库到本地
warning: You appear to have cloned an empty repository.
root@pass:/home/pass/remote_learn# cd learn_test/
root@pass:/home/pass/remote_learn/learn_test# ll
total 12
drwxr-xr-x 3 root root 4096  3月  1 21:24 ./
drwxr-xr-x 3 root root 4096  3月  1 21:24 ../
drwxr-xr-x 7 root root 4096  3月  1 21:24 .git/
root@pass:/home/pass/remote_learn/learn_test# cd .git/
root@pass:/home/pass/remote_learn/learn_test/.git# ls    远程仓库的git配置
branches  config  description  HEAD  hooks  info  objects  refs
root@pass:/home/pass/remote_learn/learn_test/.git# cat config   远程仓库的配置信息
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github.com:past-plus/learn_test.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main
root@pass:/home/pass/remote_learn/learn_test# git config user.name pass   配置用户信息
root@pass:/home/pass/remote_learn/learn_test# git config user.eamil pass@qq.com

远程仓库的文件创建与上传及信息查询

git add .   提交到暂存区

git status  查看当前状态

git commit -m 'test and learn'  提交到仓库

git push    上传到远程仓库

root@pass:/home/pass/remote_learn/learn_test# touch test.py learn.py  创建文件
root@pass:/home/pass/remote_learn/learn_test# git add .   提交到暂存区
root@pass:/home/pass/remote_learn/learn_test# git status  查看当前状态
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   learn.py
        new file:   test.py

root@pass:/home/pass/remote_learn/learn_test# git commit -m 'test and learn'  提交到仓库
[main (root-commit) b7588d1] test and learn
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 learn.py
 create mode 100644 test.py
root@pass:/home/pass/remote_learn/learn_test# git push    上传到远程仓库
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 232 bytes | 232.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:past-plus/learn_test.git
 * [new branch]      main -> main
root@pass:/home/pass/remote_learn/learn_test# vim test.py  更新文件信息,需要再次上传
root@pass:/home/pass/remote_learn/learn_test# git commit -m 'test modified'
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.py

no changes added to commit (use "git add" and/or "git commit -a")
root@pass:/home/pass/remote_learn/learn_test# git add .
root@pass:/home/pass/remote_learn/learn_test# git commit -m 'test modified'
[main 9d0f8f9] test modified
 1 file changed, 2 insertions(+)
root@pass:/home/pass/remote_learn/learn_test# git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
root@pass:/home/pass/remote_learn/learn_test# git log  查看提交日志
commit 9d0f8f9b493856ec917edd67807ad0676e12da9a (HEAD -> main)
Author: pass <10752095+past_pass@user.noreply.gitee.com>
Date:   Fri Mar 1 21:35:20 2024 +0800

    test modified

commit b7588d1d98bf2a1287904610bff4d92397393440 (origin/main)
Author: pass <10752095+past_pass@user.noreply.gitee.com>
Date:   Fri Mar 1 21:30:52 2024 +0800

    test and learn
root@pass:/home/pass/remote_learn/learn_test# git reflog  查看历史版本
9d0f8f9 (HEAD -> main) HEAD@{0}: commit: test modified
b7588d1 (origin/main) HEAD@{1}: commit (initial): test and learn
root@pass:/home/pass/remote_learn/learn_test# git push   提交到远程仓库
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:past-plus/learn_test.git
   b7588d1..9d0f8f9  main -> main

 运行结果:

Git 多人操作

git commit -am 'xxxx'    参数a 表示add添加到暂存区

git pull                           拉取远程仓库到工作区

员工a的操作

root@pass:/home/pass/gitee_test/test_a# git config user.name 'a'  配置用户信息
root@pass:/home/pass/gitee_test/test_a# git config user.name 'a@qq.com'
root@pass:/home/pass/gitee_test/test_a# touch test.py
root@pass:/home/pass/gitee_test/test_a# git add .
root@pass:/home/pass/gitee_test/test_a# git status

root@pass:/home/pass/gitee_test/test_a# git commit -m 'test commit first'  提交到仓库
[master (root-commit) 7bc421c] test commit first
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.py
root@pass:/home/pass/gitee_test/test_a# git push    上传到远程仓库
Username for 'https://gitee.com': past_pass
Password for 'https://past_pass@gitee.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 226 bytes | 226.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/past_pass/test.git
 * [new branch]      master -> master

员工b

root@pass:/home/pass/gitee_test/test_b# git config user.name 'b'
root@pass:/home/pass/gitee_test/test_b# git config user.email 'b@qq.com'
root@pass:/home/pass/gitee_test/test_b# git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 206 bytes | 206.00 KiB/s, done.
From https://gitee.com/past_pass/test
 * [new branch]      master     -> origin/master
root@pass:/home/pass/gitee_test/test_b# ls
test.py

多人协助冲突的发生及解决

造成的原因: 多人修改同一份代码,需要先拉取最新的代码修改,否则会造成冲突

员工a

root@pass:/home/pass/gitee_test/test_a# echo 'echo "git learn"'>>learn.py
root@pass:/home/pass/gitee_test/test_a# cat learn.py
echo "learn"
echo "git learn"
root@pass:/home/pass/gitee_test/test_a# git commit -am 'update learn'
[master cda86ba] update learn
 1 file changed, 1 insertion(+)
root@pass:/home/pass/gitee_test/test_a# git push

员工b[没有pull拉取远程仓库的代码而是直接在本地进行修改上传造成冲突]

root@pass:/home/pass/gitee_test/test_b# cat learn.py
echo "learn"
echo "xxx"
root@pass:/home/pass/gitee_test/test_b# git commit -am 'update file'

root@pass:/home/pass/gitee_test/test_b# git push

➜  git:(test) git pull origin test
 * branch              test       -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

标签
  • 当一个大版本完成之后,需要打一个标签,记录大版本备份代码

git tag -a v1.0(标签名) -m 'version1.0'(描述) 本地打标签

git push origin v1.0(标签名)                                推送标签到远程仓库

root@pass:/home/pass/gitee_test/test# git commit -am 'commit learn file'
[master 62eda34] commit learn file
 1 file changed, 1 insertion(+), 1 deletion(-)
root@pass:/home/pass/gitee_test/test# git tag -a v1.0 -m 'version1.0'
root@pass:/home/pass/gitee_test/test# git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 278 bytes | 139.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/past_pass/test.git
   e1c4aef..62eda34  master -> master
root@pass:/home/pass/gitee_test/test# git push origin v1.0

 运行结果:

分支操作

git branch                         查看当前分支

git checkout -b test          创建并切换分支test

git checkout master         切换主分支master

git merge test                   融合分支test到主支master

root@pass:/home/pass/gitee_test/test# git branch
* master
root@pass:/home/pass/gitee_test/test# git checkout -b test
Switched to a new branch 'test'
root@pass:/home/pass/gitee_test/test# git branch
  master
* test

root@pass:/home/pass/gitee_test/test# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
root@pass:/home/pass/gitee_test/test# git branch
* master
  test

root@pass:/home/pass/gitee_test/test# git merge test
Already up to date.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/503660.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue2:路由history模式的项目部署后页面刷新404问题处理

一、问题描述 我们把Vue项目的路由模式&#xff0c;设置成history 然后&#xff0c;build 并把dist中的代码部署到nodeexpress服务中 访问页面后&#xff0c;刷新页面报404问题 二、原因分析 server.js文件 会发现&#xff0c;文件中配置的路径没有Vue项目中对应的路径 所以…

5GC SBA架构

协议标准&#xff1a;Directory Listing /ftp/Specs/archive/23_series/23.501/ (3gpp.org) NF描述说明NSSFNetwork Slice Selection Function网络切片选择&#xff0c;根据UE的切片选择辅助信息、签约信息等确定UE允许接入的网络切片实例。NEF Network Exposure Function网络开…

鸿蒙学习day1基础语法 基础变量类型

在这里插入图片描述 什么是变量&#xff1a;变量就是一些数据 如125&#xff0c;‘字符串数据’ 通过一个符号来表示 变量的定义 方法 let 变量名&#xff1a;变量类型 ’ 各种数据’ ,let是关键字&#xff0c;系统给的用来定义变量的 let name: string 张亚洲; let age: …

Xcode15与苹果ios17适配以及遇到的问题

大家好&#xff0c;我是你们的好朋友咕噜铁蛋&#xff01;最近&#xff0c;苹果发布了全新的iOS17系统&#xff0c;而作为开发者&#xff0c;我们需要确保我们的应用程序能够与这个新系统完美适配。因此&#xff0c;今天我将和大家分享一些关于Xcode15与苹果17系统适配的经验&a…

Android修行手册-Chaquopy中opencv、numpy的初步应用

Unity3D特效百例案例项目实战源码Android-Unity实战问题汇总游戏脚本-辅助自动化Android控件全解手册再战Android系列Scratch编程案例软考全系列Unity3D学习专栏蓝桥系列ChatGPT和AIGC &#x1f449;关于作者 专注于Android/Unity和各种游戏开发技巧&#xff0c;以及各种资源分…

leetcode刷题(javaScript)——链表相关场景题总结

链表中的元素在内存中不是顺序存储的&#xff0c;而是通过next指针联系在一起的。常见的链表有单向链表、双向链表、环形链表等 在 JavaScript 刷题中涉及链表的算法有很多&#xff0c;常见的包括&#xff1a; 1. 遍历链表&#xff1a;从头到尾遍历链表&#xff0c;处理每个节点…

Sora引发安全新挑战

文章目录 前言一、如何看待Sora二、Sora加剧“深度伪造”忧虑三、Sora无法区分对错四、滥用导致的安全危机五、Sora面临的安全挑战总结前言 今年2月,美国人工智能巨头企业OpenAI再推行业爆款Sora,将之前ChatGPT以图文为主的生成式内容全面扩大到视频领域,引发了全球热议,这…

k8s单机部署zookeeper

&#xff08;作者&#xff1a;陈玓玏&#xff09; 拉取镜像&#xff1a;docker pull zookeeper&#xff1b;编辑yaml&#xff1a; apiVersion: v1 kind: Service metadata:name: zookeeperlabels:app: zookeeper spec:ports:- name: clientport: 2181protocol: TCPtargetP…

C++的引用

目录 引用 常引用 指针与引用的关系 小拓展 引用的价值 做形参 传值、传引用的效率比较 做返回值 函数传值返回 函数传引用返回&#xff08;错误示范&#xff09; 野引用&#xff08;错误示范&#xff09; 引用的正常应用 值和引用作为返回值类型的性能比较 引用和…

智慧回收与售后汽车平台架构设计与实现:打造可持续出行生态

随着汽车保有量的增加和环保意识的提升&#xff0c;汽车回收和售后服务成为了整个汽车产业链中不可或缺的一环。如何设计和实现一个智慧化的回收与售后汽车平台架构&#xff0c;成为了当前汽车行业关注的热点话题。本文将从需求分析、技术架构、数据安全等方面&#xff0c;探讨…

【Pytorch 第四讲】图像分类的Tricks

1. 标签平滑 在分类问题中&#xff0c;最后一层一般是全连接层&#xff0c;然后对应标签的one-hot编码&#xff0c;即把对应类别的值编码为1&#xff0c;其他为0。这种编码方式和通过降低交叉熵损失来调整参数的方式结合起来&#xff0c;会有一些问题。这种方式会鼓励模型对不同…

巧用眼精星票证识别系统将车辆合格证快速转为结构化excel数据,简单方便

眼精星票证识别系统是一款高效且精准的OCR软件&#xff0c;它的魔力在于能将纸质文档迅速转化为电子文档&#xff0c;并实现自动化的数据结构化处理。它拥有一双"火眼金睛"&#xff0c;无论是各类发票、护照&#xff0c;还是车辆合格证等&#xff0c;都能一一识别。而…