首页 > 其他分享 >Git 基础使用

Git 基础使用

时间:2023-03-09 12:24:06浏览次数:67  
标签:git CGO0UV3J 基础 github Git master 使用 liuzonglin

参考文档:

(22条消息) cat .git/config查看远端服务器信息(git的配置信息:远端服务器连接信息)_longshenlmj的博客-CSDN博客****

(22条消息) Git知识总结_lili要努力的博客-CSDN博客

git命令总结_奔跑在路上的技术博客_51CTO博客

Git_03分支 (copyfuture.com)

(22条消息) 用git进行本地代码版本管理“流程图”_Solomon-Lang的博客-CSDN博客_代码版本控制图

(22条消息) Git的下载安装和初始设置_Lum0s!的博客-CSDN博客_git默认安装地址


image

Git 官网:Git (git-scm.com)

Windows 安装 Git:Git 详细安装教程(详解 Git 安装过程的每一个步骤)

简易的命令行入门教程:

Git 全局设置:

git config --global user.name "liuzonglin" # 设置用户签名
git config --global user.email "[email protected]" # 设置用户邮箱

创建 git 仓库:

mkdir <仓库名>
cd <仓库名>
git init # 初始化本地库
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin <远程地址>
git push -u origin "master"

已有仓库?

cd existing_git_repo
git remote add origin <远程地址>
git push -u origin "master"

Git 常用指令

image

设置用户签名
$ git config --global user.name "liuzonglin"
$ git config --global user.email "[email protected]"

说明:

签名的作用是区分不同操作者身份。

用户的签名信息在每一个版本的提交信息中能够看 到,以此确认本次提交是谁做的。

Git 首次安装必须设置一下用户签名,否则无法提交代码。

注意:

这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任 何关系。

$ cat ~/.gitconfig
[user]
        name = liuzonglin
        email = [email protected]
[credential "https://gitee.com"]
        provider = generic
初始化本地库
$ git init
Initialized empty Git repository in D:/.github/.dome/df/.git/

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master) # 全局范围的签名设置
$ ls -al
total 4
drwxr-xr-x 1 liuzonglin 197121 0 Jan  6 13:35 ./
drwxr-xr-x 1 liuzonglin 197121 0 Jan  6 13:35 ../
drwxr-xr-x 1 liuzonglin 197121 0 Jan  6 13:35 .git/ # 生成了 `.git`
$ cat .git/config # 每个git库都会有一个配置信息文件
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = https://gitee.com/liuzonglin1/df.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
查看本地库状态
$ > liuzonglin.txt

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed) # 检测到未追踪的文件 liuzonglin.txt
        liuzonglin.txt

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

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master)
添加到暂存区
$ git add <文件名>

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage) # 检测到暂存区有新文件
        new file:   liuzonglin.txt

提交到本地库
git commit -m "日志信息" <文件名> # 将暂存区的文件提交到本地库
查看历史记录
$ git reflog
5e476cb (HEAD -> master) HEAD@{0}: commit: 2
c674444 (origin/master) HEAD@{1}: reset: moving to c674444
c674444 (origin/master) HEAD@{2}: commit: 修改README.md
0999e1f HEAD@{3}: commit (initial): 第一次提交
版本穿梭
git reset --hard <版本号>

Git 分支操作

image

创建分支
$ git branch <分支名>

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master)
$ git branch -v
  lzl    5e476cb 2 # 刚创建的新的分支,并将主分支 master 的内容复制了一份
* master 5e476cb [ahead 1] 2

查看分支
$ git branch -v
* master 5e476cb [ahead 1] 2 # * 代表当前所在的分区

切换分支
$ git checkout <分支名> 
Switched to branch '<分支名>'

把指定的分支合并到当前分支上
git merge <分支名> 

$ git merge lzl
Auto-merging liuzonglin.txt
CONFLICT (content): Merge conflict in liuzonglin.txt
Automatic merge failed; fix conflicts and then commit the result.

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master|MERGING) # 冲突产生的表现:后面状态为 MERGING
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits. # 查看状态(检测到有文件有两处修改)
  (use "git push" to publish your local commits)

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:   liuzonglin.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ cat liuzonglin.txt # 查看冲突文件
<<<<<<< HEAD # 特殊符号
111111111111111111111111111111111111111111111111111111111111111111111111
======= # 当前分支的代码
123456
>>>>>>> lzl # 合并过来的代码
$ vim liuzonglin.txt # 编辑有冲突的文件,删除特殊符号,决定要使用的内容

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master|MERGING)
$ cat liuzonglin.txt
111111111111111111111111111111111111111111111111111111111111111111111111
123456

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master|MERGING)
$ git add liuzonglin.txt # 添加到暂存区

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master|MERGING)
$ git commit -m "解决冲突" # 执行提交(注意:此时使用 git commit 命令时不能带文件名)
[master 3eaeba5] 解决冲突

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master) # MERGING 消失,变为正常

冲突产生的原因:

合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git 无法替 我们决定使用哪一个。必须人为决定新代码内容。

Git 远程仓库操作

image

查看当前所有远程地址别名
$ git remote -v
origin  https://gitee.com/liuzonglin1/df.git (fetch)
origin  https://gitee.com/liuzonglin1/df.git (push)

起别名
$ git remote add <别名> <远程地址>

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.dome/df (master)
$ git remote -v
lzl     https://gitee.com/liuzonglin1/df.git (fetch)
lzl     https://gitee.com/liuzonglin1/df.git (push)

推送本地分支上的内容到远程仓库
$ git push <别名> <分支>
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 12 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 546 bytes | 546.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'lzl' on Gitee by visiting:
remote:     https://gitee.com/liuzonglin1/df/pull/new/liuzonglin1:lzl...liuzonglin1:master
To https://gitee.com/liuzonglin1/df.git
 * [new branch]      lzl -> lzl



# 执行案例
$ git status
On branch master
Your branch is up to date with 'origin/master'.

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:   Git/Git.md

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

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc (master)
$ git add Git/Git.md

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc (master)
$ git remote -v
origin  https://gitee.com/liuzonglin1/file.git (fetch)
origin  https://gitee.com/liuzonglin1/file.git (push)

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc (master)
$ git commit -m "修改参考文档位置"
[master 1c8ca01] 修改参考文档位置
 1 file changed, 30 insertions(+), 25 deletions(-)

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc (master)
$ git push origin master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 12 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 441 bytes | 441.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/liuzonglin1/file.git
   b53eea4..1c8ca01  master -> master

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc (master)

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

小结:clone 会做如下操作。1、拉取代码。2、初始化本地仓库。3、创建别名

将远程仓库对于分支最新内容拉下来后与 当前本地分支直接合并
$ git pull <远程库地址别名> <远程分支名> 
From https://gitee.com/liuzonglin1/df
 * branch            master     -> FETCH_HEAD
Already up to date.

修改文件名案例

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    Windows Git 乱码.md

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

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

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$ git add Git\ 乱码.md

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Git 乱码.md

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    Windows Git 乱码.md

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$ git rm Windows\ Git\ 乱码.md
rm 'Git/Windows Git 乱码.md'

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Git 乱码.md
        deleted:    Windows Git 乱码.md


liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$ git commit -m "修改文件名"
[master 310e588] 修改文件名
 2 files changed, 62 insertions(+), 9 deletions(-)
 create mode 100644 Git/Git 乱码.md
 delete mode 100644 Git/Windows Git 乱码.md

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$ git push origin master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 12 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.07 KiB | 1.07 MiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/liuzonglin1/file.git
   1619138..310e588  master -> master

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

liuzonglin@LAPTOP-CGO0UV3J MINGW64 /d/.github/.doc/Git (master)
$

标签:git,CGO0UV3J,基础,github,Git,master,使用,liuzonglin
From: https://www.cnblogs.com/liuzonglin/p/17197915.html

相关文章

  • npm 基础使用配置淘宝镜像
    查看npm配置文件npmconfiglist配置npm使用淘宝镜像npmconfigsetregistryhttp://registry.npm.taobao.org/安装依赖npminstall注意大家如果np......
  • gitlab 配置 ssh keys
    gitlab 配置sshkeys1、生成id_rsa.pubssh-keygen-trsa-C‘email’ 2、.ssh文件夹目录在c:\users\用户名\下,找到id_rsa.pub文件并复制里面的内容  3......
  • 富文本编译器-配合gin使用
    一。引入样式<!--IncludeEditorstyle.--><linkhref="https://cdn.jsdelivr.net/npm/froala-editor@latest/css/froala_editor.pkgd.min.css"rel="stylesheet"type......
  • IM通讯协议专题学习(六):手把手教你如何在Android上从零使用Protobuf
    1、前言最近我负责的LiveChat客服聊天系统到了自研阶段,任务类似于做一个腾讯云IM这样的通信层SDK。在和后台进行技术选型讨论后,确定了数据传输层协议格式使用Protobuf......
  • Jenkins 使用Send files or execute commands over SSH 插件时,报ERROR: Exception whe
    1.检查Jenkins系统配置下的PublishoverSSH是否连接正确2.配置构建任务,构建步骤中,设置开启Verboseoutputinconsole(控制台中详细输出)这样方便查看具体错误信息......
  • MegEngine 使用小技巧:借助 DataLoader 获取分批数据
    在使用MegEngine进行模型训练时,首先要进行的是数据加载和预处理。在此过程中,MegEngine中的megengine.data模块,提供了数据分批功能,其内部实现流程如下图:通过使用Datal......
  • 游标的使用和创建
    1.游标参考(https://www.cnblogs.com/HDK2016/p/9306377.html)2.优点1)允许程序对由查询语句select返回的行集合中的每一行执行相同或不同的操作,而不是对整个行集合执行同......
  • 扫漏工具nessus的安装使用
    1.下载地址http://www.downza.cn/soft/290013.html2.下载后选择对应电脑系统位数进行安装,我是64位3.安装客户端的安装就是正常安装,没啥可将的4.注册①安装后会自......
  • Java应用【XVII】在Java中使用WebSocket
    如果您觉得本博客的内容对您有所帮助或启发,请关注我的博客,以便第一时间获取最新技术文章和教程。同时,也欢迎您在评论区留言,分享想法和建议。谢谢支持!一、 简介1.1什么是W......
  • 手把手教你使用LabVIEW OpenCV DNN实现手写数字识别(含源码)
    (文章目录)前言今天和大家一起来看一下在LabVIEW中如何使用OpenCVDNN模块实现手写数字识别一、OpenCVDNN模块1.OpenCVDNN简介OpenCV中的DNN(DeepNeuralNetworkmod......