soarli

Git常用命令
记录背景之前配置Hexo博客期间使用过很多Git相关命令,但是一直不够系统化,碰巧前段时间遇到了一些相关问题,课余...
扫描右侧二维码阅读全文
18
2021/07

Git常用命令

记录背景

之前配置Hexo博客期间使用过很多Git相关命令,但是一直不够系统化,碰巧前段时间遇到了一些相关问题,课余时间系统学习并总结之。

Git原理分析

目录结构

工作流程

项目搭建

环境的配置

查看配置:

git config -l

查看系统配置(看不到用户配置):

git config --system --list

其实在Git/etc/gitconfig里面

查看用户配置(看不到系统配置):

git config --global --list

C:\Users\soarli\.gitconfig里面:

初始化仓库

本地仓库的搭建

git init # 在当前目录新建一个git代码库

克隆远程仓库

git clone [url]

文件的操作

文件的四状态

查看文件的状态

git status [filename] # 查看指定文件的状态
git status # 查看所有文件的状态
git add .     # 添加所有文件暂存区 
git commit -m "消息内容" # 提交暂存区的内容到本地仓库 -m 提交信息

忽略文件

*.txt  # 所有的txt文件
!lib.txt # 除了lib.txt
/temp # 根目录下的temp目录下的文件
build/ # build/目录下的文件
doc/*.txt # doc文件夹下的txt文件

项目的提交

正常提交

git add . # 提交所有内容到暂存区
git commit -m "增加了xxx" # 提交暂存区的内容到本地仓库
git push # 将本地仓库推送到远程仓库

分支管理

git branch # 列出本地分支
git branch -r # 列出远程分支
git branch [branch-name] # 新建一个分支,但是不会自动切换过去
git checkout [branch-name] # 切换到一个新的分支
git checkout -b mybranch # 创建一个新的分支并切换到该分支
git merge [branch] # 合并指定分支到当前分支
git branch -d [branch-name] # 删除分支
# 删除远程分支
git push origin --delete [branch-name]
git branch -dr
# 建立追踪关系,在现有分支与指定的远程分支之间
$ git branch --set-upstream [branch] [remote-branch]

Gitlab的官方文档

Command line instructions
You can also upload existing files from your computer using the instructions below.


Git global setup
git config --global user.name "soar li"
git config --global user.email "xxxxxx@xxxx.xxx"

Create a new repository
git clone git@gitlab.com:soarli/xxxxxxxx.git
cd xxxxxxxx
git switch -c main
touch README.md
git add README.md
git commit -m "add README"
git push -u origin main

Push an existing folder
cd existing_folder
git init --initial-branch=main
git remote add origin git@gitlab.com:soarli/xxxxxxxx.git
git add .
git commit -m "Initial commit"
git push -u origin main

Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.com:soarli/xxxxxxxx.git
git push -u origin --all
git push -u origin --tags

后续补充

09.22:硬撤销

会删除上一个commit后的所有改动(放弃所有修改)

git reset --hard HEAD

将本地代码初始化上传到gitlab仓库

首先你已经安装了git。

1.在本地代码目录,鼠标右键Git Bash Here;

2.执行git命令,此命令会在当前目录下创建一个.git文件夹,

git init

3.将项目的所有文件添加到仓库中,

git add .

4.这个命令会把当前路径下的所有文件,添加到待上传的文件列表中。如果想添加某个特定的文件,只需把.换成特定的文件名即可.

将add的文件commit到仓库,

git commit -m "init"

5.然后在gitlab或gitee或github里面创建一个代码仓库,得到一个仓库地址;

6.将本地的仓库关联到gitlab上,

git remote add origin http://192.168.0.166/java/robot.git

7.上传代码到github远程仓库,

git push -u origin master

或:git push --set-upstream origin master

8.若在远程进行分支的变动,本地需要执行:

git fetch

即可切换分支

git项目,VSCode显示不同颜色块的含义

代码里的左侧颜色标识:

红色,未加入版本控制; (刚clone到本地)

绿色,已经加入版本控制暂未提交; (新增部分)

蓝色,加入版本控制,已提交,有改动; (修改部分)

白色,加入版本控制,已提交,无改动;

灰色:版本控制已忽略文件。

git文件标识:

A: 增加的文件.

C: 文件的一个新拷贝.

D: 删除的一个文件.

M: 文件的内容或者mode被修改了.

R: 文件名被修改了。

T: 文件的类型被修改了。

U: 文件没有被合并(你需要完成合并才能进行提交)

X: 未知状态

22.04.22更新

如果本地修改时与远端提交的代码冲突而又没有merge合并,可能会出现以下报错:

xxx@VM-16-13-xxx:/xxx/www/wwwroot/xxx$ sudo git pull
Updating 7d984ca..xxx36
error: The following untracked working tree files would be overwritten by merge:
        xxx/xxx/xxx.php
        xxx/xxx/xxx.php
        xxx/xxx/xxx.php
Please move or remove them before you merge.
Aborting

解决方法:

sudo git clean -d -fx

Git pull强制覆盖本地文件

git fetch --all  
git reset --hard origin/master 

git pull

Git克隆指定分支

以仅克隆online分支为例:

git clone -b online http://10.1.1.11/service/tmall-service.git

参考资料:

https://gitee.com/all-about-git

https://www.runoob.com/git/git-basic-operations.html

https://gitlab.com/

https://www.bilibili.com/video/BV1FE411P7B3

https://www.cnblogs.com/lazyInsects/p/13094374.html

https://blog.csdn.net/qq_44409163/article/details/119304173

https://blog.csdn.net/harry5508/article/details/87784171

https://blog.csdn.net/weixin_40425059/article/details/114363322

https://blog.51cto.com/u_15072780/3928198

https://blog.csdn.net/weixin_39800144/article/details/78205617

最后修改:2022 年 10 月 21 日 09 : 17 PM

发表评论