Git创建本地远程仓和局域网远程仓

Git创建本地远程仓和局域网远程仓

先来看图(图片来自网络):

image

图片来源地址:https://cloud.tencent.com/developer/article/2070991

这个远程仓我们常见的有github,gittee,gitlab等。能不能在本地磁盘建一个远程仓呢?当然可以,远程仓并不要求一定要离我们很远,本地也可以的嘛!

123走起!

1.首先在F盘创建一个文件夹:CodeRepository

2.在CodeRepository目录下创建一个文件夹Project01,然后再运行git bash;执行命令: git  init 回车,就可以在该目录初始化一个仓库,并作为远程仓

PS F:\CodeRepository\project01> git init
Initialized empty Git repository in F:/CodeRepository/project01/.git/
PS F:\CodeRepository\project01>

3. 在该目录下创建一个readme.md 文件,并添加到git,然后提交 

PS F:\CodeRepository\project01> git add .\readme.txt
PS F:\CodeRepository\project01> git commit -m "first creat"
[master (root-commit) 52014f9] first creat
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme.txt

4.查看分支,并创建2~3个分支(后面有用),这里创建了Branch_A、Branch_B、Branch_C.

PS F:\CodeRepository\project01> git branch -v
* master 52014f9 first creat
PS F:\CodeRepository\project01> git branch Branch_A
PS F:\CodeRepository\project01> git branch Branch_B
PS F:\CodeRepository\project01> git branch Branch_C
PS F:\CodeRepository\project01> git branch -v
  Branch_A 52014f9 first creat
  Branch_B 52014f9 first creat
  Branch_C 52014f9 first creat
* master   52014f9 first creat
PS F:\CodeRepository\project01>

5. 在E盘创建文件WorkArea,并在该文件夹下克隆远程仓,并查看远程仓信息。

   这里注意当我们克隆远程仓但本地时,它会自动关联克隆的远程仓。可用git remote -v 查看

PS E:\WorkArea\project01> git clone F:\CodeRepository\project01\ ./
Cloning into '.'...
done.
PS E:\WorkArea\project01> git remote -v
origin F:\CodeRepository\project01\ (fetch)
origin F:\CodeRepository\project01\ (push)
PS E:\WorkArea\project01>

6. 同样我们也可以直接给本地仓添加远程仓,并查看。 (这一步是当我们有本地仓后需要关联一个远程仓时可以手动添加)

这里会用到命令git remote add <shortname> <url> 其中shortname是给远程仓起一个名, url 是远程仓的地址

git remote -v  命令用于查看本地仓绑定的远程仓信息。 

PS E:\WorkArea\project01> git remote add remote_F F:\CodeRepository\project01\
PS E:\WorkArea\project01> git remote -v
remote_F        F:\CodeRepository\project01\ (fetch)
remote_F        F:\CodeRepository\project01\ (push)
PS E:\WorkArea\project01>

7. 拉取远程仓,添加远程仓后拉取远程仓第一次要将远程分支合并到本地分支并处理冲突 使用命令 git pull --rebase origin main  

PS E:\b_ESP32_YU_2025\a_ESP32_S3_ProYu\Project01_Yu> 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 (from 0)
Unpacking objects: 100% (3/3), 184 bytes | 0 bytes/s, done.
From F:\CodeRepositories\esp32_codes\esp32s3
* [new branch] V0001A -> origin/V0001A
* [new branch] V0001B -> origin/V0001B
* [new branch] V0001C -> origin/V0001C
* [new branch] main -> origin/main
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> main
PS E:\b_ESP32_YU_2025\a_ESP32_S3_ProYu\Project01_Yu> git pull --rebase origin main
From F:\CodeRepositories\esp32_codes\esp32s3
* branch main -> FETCH_HEAD
Successfully rebased and updated refs/heads/main.
PS E:\b_ESP32_YU_2025\a_ESP32_S3_ProYu\Project01_Yu> git pull origin mian
fatal: couldn't find remote ref mian

 8. 添加本地修改,并提交到远程仓  

PS E:\WorkArea\project01> git add .\test.c
warning: in the working copy of 'test.c', LF will be replaced by CRLF the next time Git touches it
PS E:\WorkArea\project01> git commit .\test.c -m "Modify01"
warning: in the working copy of 'test.c', LF will be replaced by CRLF the next time Git touches it
[master f23ee36] Modify01
 1 file changed, 1 insertion(+)
 create mode 100644 test.c
PS E:\WorkArea\project01> git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 298.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To F:\CodeRepository\project01\
   52014f9..f23ee36  master -> master
PS E:\WorkArea\project01> 

这里有一点要注意就是我们要把远程仓切换到其他分支,不然会提交失败。这就是我们在开头创建其他分支的原因。

//git 其他命令

Git工作流程

一般工作流程如下:

从远程仓库中克隆 Git 资源作为本地仓库。

  1. 从本地仓库中checkout代码然后进行代码修改
  2. 在提交前先将代码提交到暂存区。
  3. 提交修改。提交到本地仓库。本地仓库中保存修改的各个历史版本。
  4. 在修改完成后,需要和团队成员共享代码时,可以将代码push到远程仓库。

通常在本地工作区使用时用到下面的命令就基本够了:

git 提交 全部文件

1、git add .

    git add xx 命令可以将xx文件添加到暂存区,如果有很多改动可以通过get add -A .来一次添加所有改变的文件。注意 -A 选项后面还有一个句点。git add -A 表示添加所有内容,git add . 表示添加新文件和编辑过的文件不包括删除的文件;git add -u 表示添加编辑或者删除的文件,不包括新添加的文件。

2.、git commit -m “提交注释”

3、git push origin 分支名称,一般使用: git push origin master或者git push origin main

4、正常来说,这三步就够了。

进阶 克隆远程仓库的指定分支使用

   git clone -b <branchname> --single-branch <url> <dstdir>

  这里 branchname  是要克隆的分支名称

          url  远程仓地址

         destdir  目标目录。

5、检出本地仓的某一分支使用命令:git checkout origin <branchname> <url>

   branchname 是分支名称

   url  是要存放检出文件的地址。

  

 

 

 

 

 

posted @ 2025-08-15 17:33  koomee  阅读(156)  评论(0)    收藏  举报