当前位置: 首页 > news >正文

GIT分布式版本控制系统 | 命令讲解入门

Git概述

Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 也是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件;分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库。

Git常用命令

命令名称作用
git config --global user.name 用户名设置用户签名
git congfig --global user.email 邮箱设置用户邮箱
git init初始化本地库
git status查看本地库状态
git add 文件名添加到缓存区
git commit -m “日志信息” 文件名提交到本地库
git reflog查看历史记录
git reset --hard 版本号版本穿梭

工作区(写代码) == git add ==》 暂存区(临时存储) == git commit == 》 本地库(历史版本)

设置用户签名/邮箱

ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop
$ git config --global user.name weishuo

ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop
$ git config --global user.email weishuo@weishuo.com

初始化本地库——git init

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo
$ git init
Initialized empty Git repository in D:/GIT/Git-Space/git.demo/.git/

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$

查看本地库状态——git status

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master	#当前本地库分支位置

No commits yet		#目前没有文件要提交

nothing to commit (create/copy files and use "git add" to track) 	#未提交,且无文件提交


ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master

No commits yet

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

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

添加暂存区——git add

git add 文件名
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory


ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master

No commits yet

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

文件删除暂存区——rm --cached

rm --cached 文件名
#删除暂存区,仍然存在工作区
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git rm --cached hello.txt
rm 'hello.txt'

提交本地库——git commit -m

将暂存区文件提交到本地库

git commit -m “日志信息” 文件名
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git commit -m "first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master (root-commit) 26da280] first commit
 1 file changed, 7 insertions(+)
 create mode 100644 hello.txt

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master
nothing to commit, working tree clean

查看版本信息——git reflog

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git reflog
26da280 (HEAD -> master) HEAD@{0}: commit (initial): first commit
#版本号													版本

查看版本详细信息——git log

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git log
commit 26da28016adaee50fc76b4be71fbf2b4b3fda929 (HEAD -> master)
Author: weishuo <weishuo@weishuo.com>
Date:   Tue Apr 12 21:53:00 2022 +0800

    first commit

版本穿梭——git reset --hard 版本号

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git reset --hard ffce8cd
HEAD is now at ffce8cd second commit

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git reflog
ffce8cd (HEAD -> master) HEAD@{0}: reset: moving to ffce8cd
534cbd3 HEAD@{1}: commit: third commit
ffce8cd (HEAD -> master) HEAD@{2}: commit: second commit
26da280 HEAD@{3}: commit (initial): first commit

分支的操作

命令名称作用
git branch 分支名创建分支
git branch -v查看当前分支
git checkout 分支名切换分支
git merge 分支名把指定的分支合并到当前分支上

查看当前分支——git branch -v

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git branch -v
* master 534cbd3 third commit

创建分支——git branch 分支名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git branch hot-fix

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git branch -v
  hot-fix 534cbd3 third commit
* master  534cbd3 third commit

切换分支——git checkout 分支名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git branch -v
* hot-fix 534cbd3 third commit
  master  534cbd3 third commit

合并分支——git merge 分支名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git merge hot-fix
Updating 534cbd3..017517e
Fast-forward
 hello.txt | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)


合并分支(冲突合并)

检测有文件有两处修改

显示冲突
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")


冲突合并
# 1.查看冲突代码
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ vim hello.txt
# <<<< HEAD  当前分支代码
# <<<< HEAD 当前分支代码	=======
# ======= 合并分支代码   >>>>>>> hot-fix
#删除特殊符号 以及 重复代码

#2.添加到暂存区
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ git add hello.txt

#3.提交本地库(不能带文件名)
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ git commit -m "marge test"
fatal: cannot do a partial commit during a merge.

#4.切换分支查看
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

#冲突合并,合并当前分支,不合并被合并分支
# head ---> master(分支内容)--->版本号
# HEAD决定当前分支

Git团队协作

在这里插入图片描述

在这里插入图片描述

命令名称作用
git remote -v查看当前所有进程地址别名
git remote add 别名 远程地址起别名
git push 别名 分支推送本地分支上的内容到远程仓库
git push 远程地址 分支推送本地分支上的内容到远程仓库
git clone 远程地址将远程仓库的内容克隆到本地
git pull 远程库地址别名 远程分支名将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并
git push -f 远程库地址别名 远程分支名强制推送

创建远程库别名

git remote -v #查看当前所有进程地址别名
git remote add 别名 远程地址 #起别名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git remote -v

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git remote add git.demo https://github.com/weishuoHH/git-demo.git

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git remote -v
git.demo        https://github.com/weishuoHH/git-demo.git (fetch)
git.demo        https://github.com/weishuoHH/git-demo.git (push)

推送本地分支上的内容到远程仓库

git push 别名 分支 #推送本地分支上的内容到远程仓库

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git push git.demo master
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 16 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (21/21), 1.55 KiB | 792.00 KiB/s, done.
Total 21 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/weishuoHH/git-demo.git
 * [new branch]      master -> master

拉取远程库到本地库

git pull 远程库地址别名 远程分支名

$ git pull git@gitee.com:wei-shuo-red/travel.git master --allow-unrelated-histories
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git pull git.demo master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 649 bytes | 64.00 KiB/s, done.
From https://github.com/weishuoHH/git-demo
 * branch            master     -> FETCH_HEAD
   b129c29..d40c0cd  master     -> git.demo/master
Updating b129c29..d40c0cd
Fast-forward
 hello.txt | 1 +
 1 file changed, 1 insertion(+)

克隆远程库到本地库

git clone 远程地址 将远程仓库的内容克隆到本地

clone操作:
1.拉取代码
2.初始化本地库
3.创建别名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc
$ git clone https://github.com/weishuoHH/git-demo.git
Cloning into 'git-demo'...
remote: Enumerating objects: 27, done.
remote: Counting objects: 100% (27/27), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 27 (delta 6), reused 20 (delta 4), pack-reused 0
Receiving objects: 100% (27/27), done.
Resolving deltas: 100% (6/6), done.

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc
$ ll
total 0
drwxr-xr-x 1 ws199 197609 0 Apr 17 14:27 git-demo/

#创建别名
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc/git-demo (master)
$ git remote  -v
origin  https://github.com/weishuoHH/git-demo.git (fetch)
origin  https://github.com/weishuoHH/git-demo.git (push)

GitHub免密登录

生成ssh免密登录命令 -t "以某种加密算法登录 -C "描述

ws199@DESKTOP-2N1I9JA MINGW64 ~
$ ssh-keygen.exe -t rsa -C weishuoHH@weishuo.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/ws199/.ssh/id_rsa):
Created directory '/c/Users/ws199/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/ws199/.ssh/id_rsa
Your public key has been saved in /c/Users/ws199/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:CVIbj1aIxRUz8t0m7WXpzBqwJ6RzNHze+B5dd5Jyiwk weishuoHH@weishuo.com
The key's randomart image is:
+---[RSA 3072]----+
|     +=o*.       |
|    ..oO = o   . |
|    . = o O = +  |
|     o . = X O . |
|        S E B O +|
|         o + O ++|
|            + + .|
|             . . |
|              .  |
+----[SHA256]-----+

ws199@DESKTOP-2N1I9JA MINGW64 ~
$

#通过ssh拉去远程库代码
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git pull git@github.com:weishuoHH/git-demo.git master
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
From github.com:weishuoHH/git-demo
 * branch            master     -> FETCH_HEAD
Already up to date.

相关文章:

  • iStat Menus for Mac:强大的系统监控工具
  • 02.Vue2.x Vue模版语法
  • 数值分析复习:Richardson外推和Romberg算法
  • (回溯)记忆化搜索和dp
  • Drive Scope for Mac:硬盘健康监测分析工具
  • 十一、Yocto集成tcpdump等网络工具
  • 尚硅谷webpack5笔记2
  • 初识C++
  • [C++]18:set和map的使用
  • RV32/64 特权架构 - 特权模式与指令
  • 2024数字中国创新大赛·数据要素赛道“能源大数据应用赛”正式上线!参赛指南请查收
  • 新版vscode remote ssh不兼容老系统 (waiting for server log)
  • CentOS下将 /home 目录合并到 / 目录
  • 「Redis数据结构」RedisObject
  • 微服务框架 SpringCloud微服务架构 10 使用Docker 10.7 数据卷命令
  • Web中的Bias(更新中)
  • 计算机毕业设计Java的自助旅游导航系统(源码+系统+mysql数据库+lw文档)
  • 【LIN总线测试】——LIN主节点物理层测试
  • 安卓属性动画
  • JS 的 apply 方法
  • 【前沿技术RPA】 一文了解UiPath Orchestrator的触发器和监听器
  • Java基于springboot+vue的游戏物品销售购物商城系统 前后端分离
  • HTML5期末大作业:美妆网页主题网站设计——清新的手工肥皂网站展示(4页)HTML+CSS+JavaScript
  • [附源码]Python计算机毕业设计Django三星小区车辆登记系统
  • 《MySQL实战45讲》学习笔记
  • 【网关路由测试】——网关状态转换测试
  • Mali GPU“补丁缺口”让 Android 用户容易受到攻击
  • (一)整合管理范围管理
  • ElementUI组件-日期时间控件设置禁用日期
  • Yocto创建自己的分区(基于STM32MP1)
  • 2022年物联卡的发展前景如何
  • Springboot龙龙汽车配件网站88000计算机毕业设计-课程设计-期末作业-毕设程序代做