git或gitlab命令使用

一、git命令常规命令如下
在终端输入git命令,列出可用命令

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
clone             Clone a repository into a new directory
init              Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
add               Add file contents to the index
mv                Move or rename a file, a directory, or a symlink
restore           Restore working tree files
rm                Remove files from the working tree and from the index
sparse-checkout   Initialize and modify the sparse-checkout

examine the history and state (see also: git help revisions)
bisect            Use binary search to find the commit that introduced a bug
diff              Show changes between commits, commit and working tree, etc
grep              Print lines matching a pattern
log               Show commit logs
show              Show various types of objects
status            Show the working tree status

grow, mark and tweak your common history
branch            List, create, or delete branches
commit            Record changes to the repository
merge             Join two or more development histories together
rebase            Reapply commits on top of another base tip
reset             Reset current HEAD to the specified state
switch            Switch branches
tag               Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
fetch             Download objects and refs from another repository
pull              Fetch from and integrate with another repository or a local branch
push              Update remote refs along with associated objects

git使用手册,输入如下某些命令,查看具体带参的使用方法,非常重要

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help ' or 'git help '
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system

Git 的基本语法

git clone #克隆项目到本地仓库
git add # 将工作区的修改提交到暂存区
git commit # 将暂存区的修改提交到当前分支
git reset # 回退到某一个版本
git pull # 从远程更新代码
git push # 将本地代码更新到远程分支上
git reflog # 查看历史命令
git status # 查看当前修改的文件
git status --untracked-files  #查看所有新增文件和修改文件
git show # 查看差异log信息
git log # 查看提交历史
git revert # 回退某个修改

git rm --cached :已 add(tracked) 未 commit 的文件,使其回到未 add 状态(untracked)。
git rm -f : 从本地删除已 add 的文件。
git checkout – :有修改的文件回到 tracked 状态,对已 tracked 的文件撤销修改。
git reset HEAD :撤销 commit,回到 modified 状态。
git reset --soft HEAD^:撤销 commit,回到 tracked 状态。
git clean:删除所有 untracked 文件。
git clean -n:演习
git clean -f:删除当前目录下 untracked 文件,除过 .gitignore 里的文件或文件夹
git clean -f :指定路径下 
git clean -df:删除当前目录下 untracked 文件和文件夹 
git clean -xf:删除当前目录下所有 untracked 文件,不管 .gitignore 
git reset --hard && git clean -f 使本地完全回退到上次 commit

开发流程

在公司,会有项目管理,创建好master创库,在gitlab,个人需要创建个人分支,用于提交代码,并申请,合并到分支上(一般会有第一个原始项目,在此基础上,衍生出很多型号,分支代码,用于不同项目),个人可以在master创建自己的分支,管理项目人员,验证没问题,会merge 合并到具体项目的分支上。

远程仓库,本地仓库,工作区之间的联系如下图,远程仓库是公司管理的gitlab,本地仓库,是自己本机建立仓库,管理文件的,ls -a命令可以查看到.git目录,这个就是本地仓库,commit提交代码时,并未提交至网络服务器,还在本机,工作区就是project那个文件夹下,工作编写文件的区域。
git或gitlab命令使用_第1张图片

系统安装git ,输入指令:sudo apt-get install git
安装完成后,公司,需要配置,注册用户,密码,例如,

git config --global user.name "name"
git config --global user.email "name.com"

在Linux终端,创建新分支,首先新建工作区,目录文件夹,如:xxxx_project,进入这个xxxx_project目录下
输入如下命令

初始化本地仓库  		git init    //git ini 可以ls -a 看到一个.git文件夹目录,这个不能删掉,这个就是管理记录git的文件。
添加所有项目文件			git add .   或 git add filename
先提交到本地的仓库		git commit -m "你想要提交的备注信息"    注:(不是直接提交到gitlab上)
添加Gitlab远程仓库地址	git remote add origin git@192.168.10.10:youngelace/yes-web.git//git地址是master仓库的地址,主分支地址
创建新的分支			git branch sdk_2202_test
切换到新分支下			git checkout sdk_2202_test
提交到新分支到远程仓库	git push -u origin sdk_2202_test
查看所有分支			git branch -a 或git branch -r
查看提交记录			git status 

二、gitlab 拉取代码,创建个人分支

但是一般不会给开发人员权限,直接提交,需要创建个人分支,然后申请合并分支,通过或合并到分支上。
git或gitlab命令使用_第2张图片
公司会给一个网站,打开公司的gitlab仓库,在主master,点击clone,弹出复制git的网址,获取仓库地址

git或gitlab命令使用_第3张图片

第一步:在终端新建xxxx_project工程文件夹下,输入 git clone -b sdk_test gitlab仓库master网址(上面clone的网址)
将分支sdk_test 下的代码下拉至xxxx_project本地仓库,git clone 完后,可以ls -a 看到一个.git文件夹目录,这个就是管理记录git的文件。

第二步:在sdk_test 工作区里有.git文件管理器,新建个人分区 git checkout -b sdk_test _laowang,这个时候网页gitlab,可以看到新建的个人分支,

第三步:准备提交修改,增加的问件,git add . ,如果有新增文件,git add xxxxx.c,

第四部:提交修改记录到本地仓库,个人分支上,git commit -m “标签” 如git commit -m “function:update camera json file_20230105”

第五步:推送到远程仓库sdk_test 分支上, git push -u origin sdk_test _laowang

第六步:申请合并分支,将个人分支合并到某个分支,并选中删除个人分支

git或gitlab命令使用_第4张图片

提交完成后,可以看下工作区,状态,记录,输入git log ,git status,有nothing to commit, working tree clean说明提交完成,干净的
root@ubuntu:/project# git status
On branch chenguanjie/penetration_test
Your branch is up to date with ‘origin/chenguanjie/penetration_test’.

nothing to commit, working tree clean

下图是大概工作流程
git或gitlab命令使用_第5张图片
三、如何撤销 git add commit

git三种状态:
Git 有三种状态,你的文件可能 处于其中之一: 已提交(committed)、已修改(modified) 和 已暂存(staged)。

• 已修改表示修改了文件,但还没保存到数据库中。
• 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
• 已提交表示数据已经安全地保存在本地数据库中。

这会让我们的 Git 项目拥有三个阶段:工作区、暂存区以及 Git 目录。
git add 是将工作区已修改的文件提交到暂存区
git commit 是将暂存区的文件提交到Git 目录

二、add回退
1、如果执行git add之后,发现误添加了某个文件提交到了暂存区,可以通过以下命令撤回到工作区:
git reset HEAD <文件名>
2、如果想将所有暂存区的文件撤回到工作区:
git reset HEAD
三、commit回退
1、如果执行git commit之后,因为某种原因想撤销提交但仍然保留commit之前的修改,可以通过以下命令撤销提交:

将暂存区最近一次提交到Git目录的文件全部撤回到暂存区

git reset --soft HEAD^
2、如果想将git commit和git add一并撤回:

将暂存区最近一次提交到Git目录的文件全部撤回到暂存区,且将暂存区的文件全部撤回到工作区

git reset --mixed HEAD^

等同于该命令

git reset --soft HEAD^ && git reset HEAD
3、如果不想保留最近一次提交的所有修改:
git reset --hard HEAD^

三、git 版本回退操作

场景一:如果想将代码恢复到之前某个提交的版本,且那个版本之后提交的版本都不要了,就可以使用 git rest
原理: git reset的作用是修改HEAD的位置,即将HEAD指向的位置改变为之前存在的某个版本
操作:

  1. 查看版本号:git log,也可以上代码托管网页上查看history,找到需要回滚的目标版本号
  2. 使用git reset --hard 目标版本号 //命令将版本回退
  3. 使用“git push -f” //提交更改,此时如果用“git push”会报错,因为我们本地库HEAD指向的版本比远程库的要旧,用“git push -f”强制推上去。

场景二,回退某个版本,验证以前的修改功能,验证完,再回退至最新

  1. 查看版本号:git log,也可以上代码托管网页上查看history,找到需要回滚的目标版本号
  2. 使用git reset --hard 目标版本号 //命令将版本回退
  3. 使用git reset --hard 最新目标版本号

场景三:如果我们想撤销之前的某一版本,但是又想保留该目标版本后面的版本,记录下这整个版本变动流程,就可以用这种方法。
原理:我们commit了三个版本(版本一、版本二、 版本三),突然发现版本二不行(如:有bug),想要撤销版本二,但又不想影响撤销版本三的提交,就可以用 git revert 命令来反做版本二,生成新的版本四,这个版本四里会保留版本三的东西,但撤销了版本二的东西。

操作:

  1. 查看版本号:git log,也可以上代码托管网页上查看history,找到需要撤销的目标版本号
  2. 使用“git revert -n 版本号”反做,并使用“git commit -m 版本名”提交:
    (1)反做,使用“git revert -n 版本号”命令。
    注意: 这里可能会出现冲突,那么需要手动修改冲突的文件。而且要git add 文件名。
    (2)提交,使用“git commit -m 版本名”
  3. 使用“git push”推上远程库:

四、git diff 版本文件差异对比,非常强大好用

git diff 命令用于对比文件差异,能够看到哪些文件进行了修改,新增了多少行,删除了多少行,也能对比不同版本、不同tag,甚至不同commit 之间的差异,功能非常强大。

1、差异统计

常用示例:git diff --stat

–stat 参数表示统计汇总,比如对代码进行一通修改后,想了解一共修改了多少个文件,新增了多少行,删除了多少行,就可以使用 git diff --stat,将所有变更信息统计出来,如下所示:

$ git diff --stat
kernel/drive-linux/kernel/source/oss_src/nvidia/drivers/video/tegra/nvmap/nvmap_dev.c           |   15 +-
 kernel/drive-linux/kernel/source/oss_src/nvidia/drivers/video/tegra/nvmap/nvmap_heap.c          |   14 +-
 kernel/drive-linux/kernel/source/oss_src/nvidia/drivers/video/tegra/nvmap/nvmap_init.c          |    8 +-
 kernel/drive-linux/kernel/source/oss_src/nvidia/include/linux/tegra-pcie-edma.h                 |    7 -
 441 files changed, 6488 insertions(+), 8565 deletions(-)

常用示例:git diff – test.txt

– test.txt 表示具体某个文件的差异,不加 – 具体文件,显示所有差异。

2、两次提交之间的差异

(1)统计两次提交之间的差异
git diff --stat commit_id1 commit_id2

(2)统计两次提交之间某个文件的差异
git diff --stat commit_id1 commit_id2 – test.txt

(3)查看两次提交之间某个文件的差异明细
git diff commit_id1 commit_id2 – test.txt

3、两个分支的差异

统计汇总:
git diff --stat branch1 branch2
git diff --stat branch1 branch2 – test.txt

差异明细:
git diff branch1 branch2 – test.txt

远程分支也可以对比差异:
git diff --stat remotes/origin/branch1 remotes/origin/branch2
git diff --stat remotes/origin/branch1 remotes/origin/branch2 – test.txt
git diff remotes/origin/branch1 remotes/origin/branch2 – test.txt

4、两个标签(tag)的差异

统计汇总:
git diff --stat tag1 tag2
git diff --stat tag1 tag2 – test.txt

差异明细:
git diff tag1 tag2 – test.txt

5、前2次提交的某个文件差异

git diff head~ head test.txt

head表示最后一次的提交,head~表示最后一次提交之前的提交。

参考:
转载请注明:http://www.wendaok.cn/post/16744.html 成长的对话 » git diff 查看文件差异

五、开发过程中用的常用步骤命令

1、查看工作区与本地仓库文件差异,新增文件,修改文件的记录,和git diff --stat的区别是,不显示更改了多少行
git status

On branch sdk_6030
Your branch is up to date with 'origin/sdk_6030'.

Changes not staged for commit:

(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified:   platform-config/bpmp_dtsi/t23x/include/parts/uphy/3663-0001-a01-uphy.dtsi
modified:   platform-config/hardware/nvidia/platform/t23x/automotive/bct/p3663/pinmux/663-a01-gpio-default.dtsi
modified:   platform-config/hardware/nvidia/platform/t23x/automotive/bct/p3663/pinmux/tegra23x-mb1-bct-p3663-a01-pinmux.dts
modified:   platform-config/hardware/nvidia/platform/t23x/automotive/flashing/board_configs/p3663-a01.json

Untracked files:
(use "git add ..." to include in what will be committed)

firmware/bin/t23x/dce-dtb.bin
firmware/bin/t23x/tzvault/
out/
platform-config/bpmp_dt/t23x/tegra234-bpmp-2421-1099-2425-1099-emchub1067.dts0
platform-config/bpmp_dt/t23x/tegra234-bpmp-2421-1099-2425-1099-emchub1067.dts0.d
platform-config/bpmp_dt/t23x/tegra234-bpmp-2421-1099-2425-1099-mods.dts0

2、想恢复修改的文件,可以是某个文件,可以所有文件。. 是当前所有文件,如下所有修改的文件,都恢复了,但是新增的文件,是无法删除的。
git restore . 或 git checkout . 或 git restore xxxfile 某个文件 或 git checkout xxxfile 某个文件
git status
可以看到下面log,modify的文件记录都没有了,恢复了修改文件到最新一次版本的文件

On branch sdk_6030
Your branch is up to date with 'origin/sdk_6030'.

Untracked files:
 (use "git add ..." to include in what will be committed)
firmware/bin/t23x/dce-dtb.bin
firmware/bin/t23x/tzvault/
out/
platform-config/bpmp_dt/t23x/tegra234-bpmp-2421-1099-2425-1099-emchub1067.dts0
platform-config/bpmp_dt/t23x/tegra234-bpmp-2421-1099-2425-1099-emchub1067.dts0.d
platform-config/bpmp_dt/t23x/tegra234-bpmp-2421-1099-2425-1099-mods.dts0

3、删除某个文件version-nv-pdk.txt
git rm version-nv-pdk.txt
git status

On branch sdk_6030
Your branch is up to date with 'origin/sdk_6030'.

Changes not staged for commit:
 (use "git add/rm ..." to update what will be committed)
 (use "git restore ..." to discard changes in working directory)
deleted:    version-nv-pdk.txt

恢复删除,修改的文件,删除文件后
git restore . 或 git checkout . 或 git restore xxxfile 某个文件 或 git checkout xxxfile 某个文件

D	version-nv-pdk.txt
Your branch is up to date with 'origin/sdk_6030'.

5、显示某个版本的提交信息,修改记录,不带参数则默认是最新一次提交信息
输入git show 或 git show xxxx某某版本号

commit d96114cc2cf282915f250f589620f7cb0fa58643 (HEAD -> sdk_6030, /sdk_6030)
Author: 
Date:   Tue Nov 15 23:12:32 2022 +0800

Add:register i2c access interface:#0

diff --git a/samples/nvmedia/nvsipl_isxcameramodule/MAX96712cameramodNvMMAX96712_96717_ISX031.cpp b/samples/nvmedia/nvsipl_isx031_ramodule/MAX96712cameramodule/isx03MAX96712_96717_ISX031.cpp

index 357d7c9ec..5e24dff85 100755

--- a/samples/nvmedia/nvsiamodule/MAX96712cameramodule/isx031_max96717_zeeker/CNvMMAX96712_96717_ISX031.cpp
+++ b/samples/nvmedia/nvsile/MAX96712cameramodule/isx031_max96717_zeeker/CNvMMAX96712_96717_ISX031.cpp
@@ -19,6 +19,9 @@
 extern "C" {
 #include "cdi_isx031.h"
 #include "N24C64EEPROMDriver/cdi_n24c64.h"
+#include "unistd.h"
+#include "cdi_max96717f.h"
+#include "cdi_max96712.h"
 }
  

6、查看提交历史
输入git log

commit d96114cc2cf282915f250f589620f7cb0fa58643 (HEAD -> sdk_6030, /sdk_6030)
Author: Hon
Date:   Tue Nov 15 23:12:32 2022 +0800
Add:register i2c access interface:#0

commit 30b394c09fb9c951da9325474e857715b9a876aa
Author: Hong
Date:   Tue Nov 15 20:05:57 2022 +0800
Fix:disable max96717f clock output setting && disable HCG mocro setting:#0

commit 72957a0479844b520ea62de864344f710bb395f5
Author: Hong
Date:   Wed Nov 9 20:36:27 2022 +0800
Fix:fix the abnormal image problem on x8b binning mode:#0

6、回退某个版本,但是新增的未提交至仓库的文件,是不会删除放弃的。git checkou . 或 git restore . 都不会删除增加的文件,
新增一个test.c文件,
git reset --hard HEAD 或git reset --hard xxxxx某某版本

输入git log

commit d96114cc2cf282915f250f589620f7cb0fa58643 (HEAD -> sdk_6030, /sdk_6030)
Author: 
Date:   Tue Nov 15 23:12:32 2022 +0800
Add:register i2c access interface:#0

commit 30b394c09fb9c951da9325474e857715b9a876aa
Author: Hong
Date:   Tue Nov 15 20:05:57 2022 +0800
Fix:disable max96717f clock output setting && disable HCG mocro setting:#0

输入git reset --hard 30b394c09fb9c951da9325474e857715b9a876aa // commit后面30b394c09fb9c951da9325474e857715b9a876aa就是版本号

HEAD is now at 30b394c09 Fix:disable max96717f clock output setting && disable HCG mocro setting:#0

输入git log

commit 30b394c09fb9c951da9325474e857715b9a876aa (HEAD -> sdk_6030)
Author: Hong
Date:   Tue Nov 15 20:05:57 2022 +0800
 Fix:disable max96717f clock output setting && disable HCG mocro setting:#0

输入git status //可以看到版本回退到指定版本了,但是新增文件,还在,如果有想要想要彻底恢复最新一次状态,只能使用其他命令

On branch sdk_6030
Your branch is behind 'origin/sdk_6030' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)

Untracked files:
(use "git add ..." to include in what will be committed)
test.c

输入git pull 或 git reset --hard 某某版本 // 可以更新最新仓库文件到本地仓库和工作区,恢复刚才跳至版本到最新版本

remote: Enumerating objects: 419, done.
remote: Counting objects: 100% (419/419), done.
remote: Compressing objects: 100% (297/297), done.
remote: Total 419 (delta 194), reused 322 (delta 121)
Receiving objects: 100% (419/419), 2.90 MiB | 2.15 MiB/s, done.
Resolving deltas: 100% (194/194), completed with 8 local objects.
From gitlab:soc_linux/drive-linux

Updating 30b394c09..31a303d0e
Fast-forward
 .../cameramodule/MAX96712cameramodule/isx031_max96vMMAX96712_96717_ISX031.cpp      |  98 ++++++
 .../cameramodule/MAX96712cameramodule/isCNvMMAX96712_96717_ISX031.hpp      |  13 ++++
 .../devblk/cameramodule/MAX96712cameramodule/isx031_/cdi_isx031.c                |  52 +++++++++++

六、删除本地分支和远程分支

在用git开发过程中,我们在分支合并后会将分支删除。这里我们会遇到两种情况,一是本地和远程的分支都还在,另一种就是远程仓库已经删除了,但本地仓库还有备份。

1)情况一,本地和远程分支都在
这是最常见的情况了,在这种情况下,我们会先删除本地分支,再删除远程分支。

1. 删除本地分支
在git中,删除本地分支并不会影响远程仓库中的任何分支。

删除本地分支的命令:git branch -d

先列出所有本地分支
$ git branch
* feature/test1
  main

我们可以看到现在本地有两个分支,当前在这个分支上。

先切换到其他分支
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

删除这个分支
$ git branch -d feature/test1 
Deleted branch feature/test1.

注意,如果分支包含未合并的更改和未推送的提交,则该 -d标志将不允许删除本地分支。此时,如果你确定了不想要分支的内容,可以使用 -D替换 -d来强制删除此分支

2. 删除远程分支
现在我们再来看看分支情况:

$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/feature/test1
  remotes/origin/main

此时我们已经成功删除了本地仓库,但我们之前有推送过分支到远程仓库,从上面列表可知,远程仓库中还存在此分支,那我们还需要删除远程仓库中的分支。

删除远程分支的命令:git push -d

先列出所有远程分支:
$ git branch -r
  origin/HEAD -> origin/main
  origin/feature/test1
  origin/main

我们可以看到,此时远程仓库有两个分支

origin/HEAD并非分支,它指向远程服务器上的默认分支,即为origin的远程仓库中的HEAD,一般此值的指向不会改变,具体请另行Google。

删除远程分支
$ git push origin -d feature/test1
To https://github.com/***/git-practice.git
 - [deleted]         feature/test1

这时候再看看分支情况

$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

可以看到我们已经成功删除了本地和远程仓库中的分支。此时,去Github上查看时,分支也已经删除。

2)情况二,远程仓库已经删除,本地分支还在
我们去Github上直接删除这个分支,或提交合并分支后,管理员会删除提交的个人分支,
远程仓库中删除了分支,而本地仓库还保留着原来的远程分支副本。

$ git branch -a
* feature/test2
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/feature/test2
  remotes/origin/main

虽然远程仓库删除了,但是本地分支副本还在

同前文一样进行本地分支删除。

$ git branch -d feature/test2
Deleted branch feature/test2

此时再用git push -d 删除分支会提示错误:

$ git push origin -d feature/test2
error: unable to delete 'feature/test2': remote ref does not exist
error: failed to push some refs to 'https://github.com/***/git-practice.git'

当我们查看分支时,可以看到还是存在的

$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/feature/test2

此时,我们以下命令git remote show 查看远程分支和本地的对应关系:

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/***/git-practice.git
  Push  URL: https://github.com/***/git-practice.git
  HEAD branch: main
  Remote branches:
    main                              tracked
    refs/remotes/origin/feature/test2 stale (use 'git remote prune' to remove)
  Local branch configured for 'git pull':
    main merges with remote main
  Local ref configured for 'git push':
    main pushes to main (up to date)

我们可以看到main分支的状态是tracked,而feature/test2的状态是stale,
并且后面git已经提示了处理方式(use ‘git remote prune’ to remove)。

$ git remote prune origin
Pruning origin
URL: https://github.com/***/git-practice.git
 * [pruned] origin/feature/test2

再次查看,就删除掉了,

$ git branch -a 
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

你可能感兴趣的:(linux命令用法,linux工具环境搭建,git,gitlab)