首页 > 其他分享 >一篇博客带你上手Git

一篇博客带你上手Git

时间:2023-08-07 15:47:06浏览次数:38  
标签:git 一篇 OJQFEH00 LAPTOP 博客 hecheng Git master MINGW64

概述

安装Git

下载官方网站,下载后安装包样式:双击安装,安装成功后右键文件会有如下选项证明安装成功。

基本配置

  1. 设置用户信息,桌面右键,选择Git bash here
hecheng@LAPTOP-OJQFEH00 MINGW64 ~/Desktop
$ git config --global user.name "hecheng"

hecheng@LAPTOP-OJQFEH00 MINGW64 ~/Desktop
$ git config --global user.email "[email protected]"

自己编写自己的用户名和邮箱,之后可以查看,效果如下

基本操作

  1. 创建文件——>提交到缓存区——>提交到仓库
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest
$ git init
Initialized empty Git repository in E:/GitTest/.git/

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ ll
total 8
drwxr-xr-x 1 hecheng 197121 0  7月 17 16:18 ./
drwxr-xr-x 1 hecheng 197121 0  7月 17 16:17 ../
drwxr-xr-x 1 hecheng 197121 0  7月 17 16:18 .git/

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ touch file01.txt

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master

No commits yet

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

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

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master

No commits yet

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


hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "add file01"
[master (root-commit) e205281] add file01
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file01.txt

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git log
commit e2052811960420feb66b8b7bef48980537ea859b (HEAD -> master)
Author: hecheng <[email protected]>
Date:   Mon Jul 17 16:30:35 2023 +0800

    add file01

  1. 修改文件后,文件又进入工作区
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch 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:   file01.txt

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

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file01.txt


hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "update file01"
[master 033fcd2] update file01
 1 file changed, 1 insertion(+)

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git log
commit 033fcd2c7386e611bff68c325ab2c770e075f014 (HEAD -> master)
Author: hecheng <[email protected]>
Date:   Mon Jul 17 16:36:21 2023 +0800

    update file01

commit e2052811960420feb66b8b7bef48980537ea859b
Author: hecheng <[email protected]>
Date:   Mon Jul 17 16:30:35 2023 +0800

    add file01


  1. 版本回退
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 033fcd2 (HEAD -> master) update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git reset --hard e205281
HEAD is now at e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* e205281 (HEAD -> master) add file01

  1. 查看已删除的记录
$ git reflog
033fcd2 (HEAD -> master) HEAD@{0}: reset: moving to 033fcd2
e205281 HEAD@{1}: reset: moving to e205281
033fcd2 (HEAD -> master) HEAD@{2}: commit: update file01
e205281 HEAD@{3}: commit (initial): add file01

Git分支

  1. 查看,创建,切换分支
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch
* master

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch dev01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 033fcd2 (HEAD -> master, dev01) update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ touch .gitignore

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore

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

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   .gitignore


hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "add ignore file"
[master 4008424] add ignore file
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 4008424 (HEAD -> master) add ignore file
* 033fcd2 (dev01) update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout dev01
Switched to branch 'dev01'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git checkout master
Switched to branch 'master'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout -b dev02
Switched to a new branch 'dev02'


  1. 合并、删除分支
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev02)
$ git checkout master
Switched to branch 'master'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 4008424 (HEAD -> master, dev02) add ignore file
* 033fcd2 (dev01) update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout dev01
Switched to branch 'dev01'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ touch file02.txt

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git add .

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git commit -m 'add file02'
[dev01 f2a85f1] add file02
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file02.txt

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git-log
* f2a85f1 (HEAD -> dev01) add file02
| * 4008424 (master, dev02) add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git checkout master
Switched to branch 'master'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git merge dev01
Merge made by the 'ort' strategy.
 file02.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file02.txt

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
*   c3ebaa2 (HEAD -> master) Merge branch 'dev01'
|\
| * f2a85f1 (dev01) add file02
* | 4008424 (dev02) add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -d dev02
Deleted branch dev02 (was 4008424).

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
*   c3ebaa2 (HEAD -> master) Merge branch 'dev01'
|\
| * f2a85f1 (dev01) add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01


强制删除

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 88d63ff (dev02) abcde
*   b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
*   c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -d dev02
error: The branch 'dev02' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev02'.

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -D dev02
Deleted branch dev02 (was 88d63ff).

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
*   b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
*   c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

  1. 解决冲突
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout -b dev
Switched to a new branch 'dev'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git status
On branch dev
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:   file01.txt

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

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git add .

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git commit -m "update file01 count-2"
[dev b6c3192] update file01 count-2
 1 file changed, 1 insertion(+), 1 deletion(-)

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git-log
* b6c3192 (HEAD -> dev) update file01 count-2
*   c3ebaa2 (master) Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git checkout master
Switched to branch 'master'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "update file01 count-3"
[master d9ed7d0] update file01 count-3
 1 file changed, 1 insertion(+), 1 deletion(-)

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
Merge branch 'dev'























[master b357906] Merge branch 'dev'

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
*   b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 (dev) update file01 count-2
* | d9ed7d0 update file01 count-3
|/
*   c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

  1. 分支创建原则
    master,主分支,用于发行的版本,develop,开发的分支,feature(可以删除),功能的分支,hotfix,修复bug的分支,feature合并到develop,develop合并到master,用户反馈到bug,创建hotfix分支,写好修复代码后,分别合并到master分支和develop分支。

四、远程仓库

1.

创建码云账户,创建仓库,获取公匙。
再连接本地仓库与远程仓库(添加远程仓库:git remote add 远端名称 仓库路径),查看远程仓库,将代码推送到远程仓库(git push 远端名称 本地分支名:远端分支名),查看关联关系:git branch -vv

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git remote add origin [email protected]:hecheng11223/git_-test.git

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git remote
origin

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push origin master
Enumerating objects: 22, done.
Counting objects: 100% (22/22), done.
Delta compression using up to 16 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (22/22), 1.82 KiB | 621.00 KiB/s, done.
Total 22 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hecheng11223/git_-test.git
 * [new branch]      master -> master

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push origin master:master
Everything up-to-date
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push --set-upstream origin master
Everything up-to-date
branch 'master' set up to track 'origin/master'.

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push
Everything up-to-date
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -vv
* master b357906 [origin/master] Merge branch 'dev'

2.

从远程仓库克隆

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study
$ git clone [email protected]:hecheng11223/git_-test.git hello-git
Cloning into 'hello-git'...
remote: Enumerating objects: 22, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 22 (delta 3), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (22/22), done.
Resolving deltas: 100% (3/3), done.

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git-log
*   b357906 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
*   c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

从远程仓库抓取和拉取

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git fetch
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 229 bytes | 76.00 KiB/s, done.
From gitee.com:hecheng11223/git_-test
   b357906..c9b33a5  master     -> origin/master

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git-log
* c9b33a5 (origin/master, origin/HEAD) add file03
*   b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
*   c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git merge origin/master
Updating b357906..c9b33a5
Fast-forward
 file04.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file04.txt

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 208 bytes | 69.00 KiB/s, done.
From gitee.com:hecheng11223/git_-test
   c9b33a5..418f89f  master     -> origin/master
Updating c9b33a5..418f89f
Fast-forward
 file05.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file05.txt

hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git-log
* 418f89f (HEAD -> master, origin/master, origin/HEAD) add file05
* c9b33a5 add file03
*   b357906 Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
*   c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01

标签:git,一篇,OJQFEH00,LAPTOP,博客,hecheng,Git,master,MINGW64
From: https://www.cnblogs.com/he-cheng/p/17559598.html

相关文章

  • Django博客开发教程:创建项目
    我们对需求和数据库都进行分析了之后,我们就开始来创建我们的项目。教程是在windows10操作系统下,用的Python3.6和django2.1.1,开发工具为pycharm。打开我们的Pycharm,新建一个项目。说明:1为项目保存路径,myblog为项目名。2为选择使用的虚拟环境软件,这里选virtualenv。3为虚拟环境......
  • 1、Django博客开发教程:开发前的准备
    开发前的准备:1、安装好Python环境。Python3安装详细步骤2、安装好virtualenv虚拟环境。virtualenv虚拟环境安装方法3、安装好Pycharm开发工具。 ......
  • Django-4.2博客开发教程:数据库操作-页面动态展示数据库中的数据(十)
    1、数据准备工作首先增加2篇文章用于展示数据。 我用的mysql数据库,使用pycharm的DBBrowser进行数据查询。双击blog库下面对应的文章表,则显示当前数据。 2、查询数据并动态展示models.py里的类就是一个模板,在views.py引入并实例化。即将值查询出来并赋值到一个对象,在页......
  • git从远程仓库下载分支
    试一下在github仓库获取地址输入命令建立本地链接和链接远程仓库之后就可以下载源码了 ......
  • 博客园设置Silence主题
    博客园设置Silence主题博客园的提供了一百多个默认主题,但我还是找不到一个干净、简洁、美观的。了解到博客园可以自定义主题,于是在网上搜寻相关内容,终于被我发现了Silence——一个专注于阅读的博客园主题。Silence介绍Silence是一个专注于阅读的博客园主题界面简洁优雅,响应式......
  • git操作误区
    好久没有使用git工具,对git的理解产生了误区,在此来记录一下。对于git管理,我以为是本地一套管理,远程另一套管理。实际上,从远程拉下代码,和远程进行关联后,只要本地新建分支并有所改动,即可创建远程分支并上传。操作步骤:gitclone [email protected]:xxx/your.gitgitremoteaddor......
  • 【开发中】Git常用命令详解
    基于平时工作的场景,整理了使用频率较高的一些命令和参数,作为一个Git命令的备忘录。gitclone概述:将远程Git仓库克隆到本地,自动将远程仓库的所有分支和历史记录复制到本地。格式:gitclone[-b<name>]<repository>[<directory>]参数:-b<name>,等同--branch<name>不将新创......
  • 一篇不错的优化的文章
    刷到大佬的文章浅论OI中的卡常技巧(基本完成)-知乎(zhihu.com)对其中一些东西问了AI,存一下有关bitset有关if-else和switch-case位运算要加括号保险。 不要无脑inline和register 循环的顺序并发计算与循环展开不要默认开defineintlonglong ......
  • git工具使用
    1.git工作机制介绍1、git工作流程从远程仓库克隆代码到本地仓库从本地仓库中checkout代码然后进行代码修改在提交代码前先将代码提交到暂存区提交到本地仓库,本地仓库中保存修改的各个历史版本修改完成后,将代码push到远程仓库和团队共享代码2、git的版本库、工作目录、......
  • git-bash命令
    Sometimesit’saboutdoingtherightthing,evenifit’spainfulinside.有时候就是要做对的事,哪怕内心万分痛苦。01git-bash命令代码说明ls#llist查看当前目录下的内容cd#changedirectory更改运行目录(文件名不区分大小写)pwd#printworkingdirec......