王大拿
知道的越多,不知道的也就越多! 只要学不死,就往死里学!!!

说明:
本文所有的操作都是在windows 7 64 位系统上安装git 进行的操作;git的版本为2.31.1.windows.1

image

关于git的安装基本都是下一步;

1.新建代码库

1.1在当前目录新建一个 Git 代码库

$ git init

image

1.2新建一个目录,将其初始化为 Git 代码库

$ git init [project-name]

image

1.3下载一个项目和它的整个代码历史

$ git clone [url]

image

2.配置

Git 的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。

2.1设置提交代码时的用户信息

$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

image

2.2编辑 Git 配置文件

$ vim ~/.gitconfig

image

2.3显示当前的 Git 配置

$ git config --list

image

2.4显示当前的 Git 配置

$ cat ~/.gitconfig

image

3.增加/删除文件

3.1添加指定文件到暂存区

$ git add [file1] [file2] ...

image

3.2添加指定目录到暂存区,包括子目录

$ git add [dir]

image

3.3添加当前目录的所有文件到暂存区

$ git add .

3.4删除工作区文件,并且将这次删除放入暂存区

$ git rm 1.txt 2.txt
error: the following files have changes staged in the index:
    1.txt
    2.txt
(use --cached to keep the file, or -f to force removal)

3.4.1直接删除文件

$ git rm -fr 1.txt

3.4.2停止追踪指定文件,但该文件会保留在工作区

$ git rm --cached [file]
$ git rm --cached 1.txt 2.txt
rm '1.txt'
rm '2.txt'

3.5改名文件,并且将这个改名放入暂存区

$ git mv [file-original] [file-renamed]

image

4.代码提交

4.1提交暂存区到仓库区

$ git commit -m [message]

image

4.2提交暂存区的指定文件到仓库区

$ git commit [file1] [file2] ... -m [message]

image

4.3提交工作区自上次 commit 之后的变化,直接到仓库区

$ git commit -a

4.4提交时显示所有 diff 信息

$ git commit -v

image

4.5使用一次新的 commit,替代上一次提交如果代码没有任何新变化,则用来改写上一次 commit 的提交信息

$ git commit --amend -m [message]

5.分支

5.1列出所有本地分支

$ git branch

image
*表示当前分支

5.2列出所有远程分支

$ git branch -r

image

5.3列出所有本地分支和远程分支

$ git branch -a

image

5.4新建一个分支,但依然停留在当前分支

$ git branch [branch-name]
image

5.5新建一个分支,并切换到该分支

$ git checkout -b [branch]

image

5.6新建一个分支,指向指定 commit

5.6.1查看提交的日志

$ git log

commit 2cb44e585aab4d1b2370a3e42dc6d4070fe217dc (HEAD -> DevOps_RC, origin/master, master, DevOps_Release, DevOps_Dev)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 11:03:12 2021 +0800
    修改了ReadMe.md文件,添加了一行说明

commit 89d1d5b198a77deb9096e11b02aec7187221f384
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:59:09 2021 +0800
:...skipping...
commit 2cb44e585aab4d1b2370a3e42dc6d4070fe217dc (HEAD -> DevOps_RC, origin/master, master, DevOps_Release, DevOps_Dev)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 11:03:12 2021 +0800
    修改了ReadMe.md文件,添加了一行说明

commit 89d1d5b198a77deb9096e11b02aec7187221f384
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:59:09 2021 +0800
    添加User文件夹

commit b54962a30b090f02a39fc3c6185750cf07b068ea
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:58:30 2021 +0800
    添加忽略文件

commit e76012d77857841d344ac04893a158e75e8088fc
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:56:46 2021 +0800
    添加Readme.md文件

5.6.2新建一个分支,指向指定 commit ID

$ git branch [branch] [commit]
$ git branch DevOps_V0.3FixBug  89d1d5b198a77deb9096e11b02aec7187221f384

image

5.7新建一个分支,与指定的本地分支建立追踪关系

$ git branch --track [branch1] [branch2]
$ git branch --track DevOps_RC1 DevOps_RC

image

5.7.1DevOps_RC,增加一次提交

image

5.7.2DevOps_RC,再增加一次提交

image

5.8切换到指定分支,并更新工作区

5.8.1切换到建立追踪的分支DevOps_RC1

$ git checkout [branch-name]
$ git checkout DevOps_RC1

image

5.8.2从建立追踪的分支拉取最新的变化

$ git pull
image

5.9新建一个分支,与指定的远程分支建立追踪关系

5.9.1将本地的所有分支及信息全部推送到远程仓库

$ git push -u origin *:*
说明:origin,查看是否配置origin;默认克隆时,自动建立;
image

5.9.2查看origin信息

$ git remote -v
origin  http://192.168.145.88/devops/devops.git (fetch)
origin  http://192.168.145.88/devops/devops.git (push)

5.9.3新建一个分支,与指定的远程分支建立追踪关系

$ git branch --track [branch] [remote branch]
$ git branch --track DevOps_RC2 remotes/origin/DevOps_RC1

image

5.9.4切换到建立追踪关系的新分支DevOps_RC2

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

5.9.5使用 git pull 更新本地分支(会拉取建立追踪关系的分支代码)

$ git pull
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
Updating f7ed8ad..c11bb1a
Fast-forward
 Readme.md | 1 +
 1 file changed, 1 insertion(+)

5.10合并指定分支到当前分支

$ git merge [branch]
$ git merge DevOps_V0.3FixBug

image
提示文件存在冲突,冲突解决后再提交

5.10.1解决合并产生的冲突

按照冲突提示,修改冲突文件 Readme.md
$ vim Readme.md
image

5.10.2编辑修改解决冲突

5.10.2.1冲突提示

image

5.10.2.2冲突解决

image

5.10.2.3查看当前状态

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

You have unmerged paths.#有未合并的分支
  (fix conflicts and run "git commit")#修复冲突并且运行git commit
  (use "git merge --abort" to abort the merge) #使用 git merge --abort终止合并

Unmerged paths:#未合并的分支
  (use "git add <file>..." to mark resolution)
        both modified:   Readme.md

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

此处我的处理措施是,把合并结果加入当前分支
image

5.11选择一个 commit,合并进当前分支

$ git cherry-pick [commit]

5.11.1选择别的分支(DevOps_RC1)的一个commit

$ git log

image

5.11.2切换到另一个项目合并一个提交至此分支的分支

5.11.2.1切换分支(DevOps_Dev)

$ git checkout DevOps_Dev
Switched to branch 'DevOps_Dev'
Your branch is up to date with 'origin/DevOps_Dev'.

5.11.2.2将选定的commit,合并进当前分支

$ git cherry-pick 900d39d30f2487897c1fc09f71c4bd41e1c0cf7a

image

5.11.2.3查看当前状态

$ git status

image

5.11.2.4解决冲突

image

5.12删除分支

$ git branch -d [branch-name]

image

5.13删除远程分支

5.13.1查看当前分支的详细信息

$ git branch -av

image

5.13.2删除远程分支

$ git push origin --delete DevOps_RC2

image
可以看到远程的此分支已经删除了
image
可以看到远程的此分支已经删除了
image

5.13.3 删除远程分支

$ git branch -dr remotes/origin/DevOps_RC1

image

$ git branch -av

image

6.标签

6.1列出所有 tag

$ git tag
image
$ git tag -l
DevOps_Dev_V1.0
Dev_V1.0
V0.3FixBug
$ git tag --list
DevOps_Dev_V1.0
Dev_V1.0
V0.3FixBug

6.2新建一个 tag 在当前 commit

$ git tag [tag]
$ git tag V0.3FixBug

6.3新建一个 tag 在指定 commit

$ git tag [tag] [commit] -m [message]
$ git tag Dev_V1.0 89d1d5b198a77deb9096e11b02aec7187221f384 -m "初始开发基线"

6.4新建一个tag在当前commit,提交信息为:文件内容

$ git tag DevOps_Dev_V1.0 -F ReleaseNotes.md

6.5查看 tag 信息

$ git show [tag]
$ git show DevOps_Dev_V1.0

image

6.6提交指定 tag到远程仓库

$ git push [remote] [tag]

$ git push origin V0.3FixBug

image

6.7提交所有 tag

$ git push [remote] --tags

$ git push origin --tags

image

6.8基于某个tag创建分支并切换至该分支

$ git checkout -b [branch] [tag]
$ git checkout -b DevOps_RC3 V0.3FixBug
image

7.查看信息

7.1显示有变更的文件

$ git status

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

You are currently cherry-picking commit 900d39d.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
        deleted by us:   3.txt

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

7.2显示当前分支的版本历史

$ git log

commit 2cb44e585aab4d1b2370a3e42dc6d4070fe217dc (HEAD -> DevOps_Dev, origin/master, origin/DevOps_Release, origin/DevOps_Dev, master, DevOps_Release)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 11:03:12 2021 +0800
    修改了ReadMe.md文件,添加了一行说明
commit 89d1d5b198a77deb9096e11b02aec7187221f384 (origin/DevOps_V0.3FixBug)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:59:09 2021 +0800
    添加User文件夹
commit b54962a30b090f02a39fc3c6185750cf07b068ea
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:58:30 2021 +0800
    添加忽略文件
commit e76012d77857841d344ac04893a158e75e8088fc
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:56:46 2021 +0800
    添加Readme.md文件

7.3显示 commit 历史,以及每次 commit 发生变更的文件

$ git log --stat

commit 1c6e4116363e0dce9a931eeaad1d1dcd470bb673 (HEAD -> DevOps_RC3, tag: V0.3FixBug, origin/DevOps_V0.3FixBug, DevOps_V0.3FixBug)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 14:07:01 2021 +0800
    修复了V0.3FixBug
 Readme.md | 1 +
 1 file changed, 1 insertion(+)
commit 89d1d5b198a77deb9096e11b02aec7187221f384 (tag: Dev_V1.0)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:59:09 2021 +0800
    添加User文件夹
 Users/Wahaha.Framework.Service.Users/Encryption.cs  |  100 ++
 Users/Wahaha.Framework.Service.Users/Group.cs       |  215 ++++
 Users/Wahaha.Framework.Service.Users/IUser.cs       |   86 ++
 .../IUserClientService.cs                          |  152 +++
 .../Wahaha.Framework.Service.Users.csproj           |   97 ++
 .../Properties/AssemblyInfo.cs                     |   36 +
 Users/Wahaha.Framework.Service.Users/User.cs        |  266 +++++
 Users/Wahaha.Framework.Service.Users/UserClient.cs  |   16 +
 .../UserClientService.cs                           |  901 +++++++++++++++
 .../Wahaha.Framework.Service.Users/UserContract.cs  |  207 ++++
 Users/Wahaha.Framework.Service.Users/UserServer.cs  | 1171 ++++++++++++++++++++
 .../DesignTimeResolveAssemblyReferencesInput.cache |  Bin 0 -> 6960 bytes
 ...ework.Service.Users.csproj.FileListAbsolute.txt |    6 +
 ...vice.Users.csprojResolveAssemblyReference.cache |  Bin 0 -> 22983 bytes
 .../obj/Debug/Wahaha.Framework.Service.Users.dll    |  Bin 0 -> 38912 bytes
commit 1c6e4116363e0dce9a931eeaad1d1dcd470bb673 (HEAD -> DevOps_RC3, tag: V0.3FixBug, origin/DevOps_V0.3FixBug, DevOps_V0.3FixBug)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 14:07:01 2021 +0800
    修复了V0.3FixBug
 Readme.md | 1 +
 1 file changed, 1 insertion(+)
commit 89d1d5b198a77deb9096e11b02aec7187221f384 (tag: Dev_V1.0)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:59:09 2021 +0800
    添加User文件夹
 Users/Wahaha.Framework.Service.Users/Encryption.cs  |  100 ++
 Users/Wahaha.Framework.Service.Users/Group.cs       |  215 ++++
 Users/Wahaha.Framework.Service.Users/IUser.cs       |   86 ++
 .../IUserClientService.cs                          |  152 +++
 .../Wahaha.Framework.Service.Users.csproj           |   97 ++
 .../Properties/AssemblyInfo.cs                     |   36 +
 Users/Wahaha.Framework.Service.Users/User.cs        |  266 +++++
 Users/Wahaha.Framework.Service.Users/UserClient.cs  |   16 +
 .../UserClientService.cs                           |  901 +++++++++++++++
 .../Wahaha.Framework.Service.Users/UserContract.cs  |  207 ++++
 Users/Wahaha.Framework.Service.Users/UserServer.cs  | 1171 ++++++++++++++++++++
 .../DesignTimeResolveAssemblyReferencesInput.cache |  Bin 0 -> 6960 bytes
 ...ework.Service.Users.csproj.FileListAbsolute.txt |    6 +
 ...vice.Users.csprojResolveAssemblyReference.cache |  Bin 0 -> 22983 bytes
 .../obj/Debug/Wahaha.Framework.Service.Users.dll    |  Bin 0 -> 38912 bytes
 .../obj/Debug/Wahaha.Framework.Service.Users.pdb    |  Bin 0 -> 77312 bytes
 ...tedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs |    0
 ...tedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs |    0
 ...tedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs |    0
 .../GroupTests.cs                                  |   51 +
 .../Wahaha.Framework.Service.UsersTests.csproj      |   99 ++
 .../Properties/AssemblyInfo.cs                     |   36 +
 .../DesignTimeResolveAssemblyReferencesInput.cache |  Bin 0 -> 6571 bytes
 ....Service.UsersTests.csproj.FileListAbsolute.txt |   17 +
 ...UsersTests.csprojResolveAssemblyReference.cache |  Bin 0 -> 38740 bytes
 .../Debug/Wahaha.Framework.Service.UsersTests.dll   |  Bin 0 -> 5120 bytes
 .../Debug/Wahaha.Framework.Service.UsersTests.pdb   |  Bin 0 -> 11776 bytes
 ...tedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs |    0
 ...tedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs |    0
 ...tedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs |    0
 30 files changed, 3456 insertions(+)
commit b54962a30b090f02a39fc3c6185750cf07b068ea
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:58:30 2021 +0800
    添加忽略文件
 .gitignore | 1 +
 1 file changed, 1 insertion(+)
commit e76012d77857841d344ac04893a158e75e8088fc
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:56:46 2021 +0800
    添加Readme.md文件
 Readme.md | 1 +
 1 file changed, 1 insertion(+)
(END)

7.4显示某个文件的版本历史,包括文件改名

$ git log --follow [file]

$ git log --follow Readme.md
commit 1c6e4116363e0dce9a931eeaad1d1dcd470bb673 (HEAD -> DevOps_RC3, tag: V0.3FixBug, origin/DevOps_V0.3FixBug, DevOps_V0.3FixBug)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 14:07:01 2021 +0800
    修复了V0.3FixBug

commit e76012d77857841d344ac04893a158e75e8088fc
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:56:46 2021 +0800
    添加Readme.md文件

$ git whatchanged [file]

$ git whatchanged Readme.md
commit 1c6e4116363e0dce9a931eeaad1d1dcd470bb673 (HEAD -> DevOps_RC3, tag: V0.3FixBug, origin/DevOps_V0.3FixBug, DevOps_V0.3FixBug)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 14:07:01 2021 +0800
    修复了V0.3FixBug
:100644 100644 6c1b17d 82f1ecb M        Readme.md
commit e76012d77857841d344ac04893a158e75e8088fc
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:56:46 2021 +0800
    添加Readme.md文件
:000000 100644 0000000 6c1b17d A        Readme.md

7.5显示指定文件相关的每一次 diff

$ git log -p [file]

$ git log -p Readme.md

commit 1c6e4116363e0dce9a931eeaad1d1dcd470bb673 (HEAD -> DevOps_RC3, tag: V0.3FixBug, origin/DevOps_V0.3FixBug, DevOps_V0.3FixBug)
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 14:07:01 2021 +0800
    修复了V0.3FixBug
diff --git a/Readme.md b/Readme.md
index 6c1b17d..82f1ecb 100644
--- a/Readme.md
+++ b/Readme.md
@@ -1 +1,2 @@
 this is my first add
+修改了V0.3FixBug的Bug
commit e76012d77857841d344ac04893a158e75e8088fc
Author: zhangsan <zhangsan@263.com>
Date:   Sat Jul 10 10:56:46 2021 +0800
    添加Readme.md文件
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..6c1b17d
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1 @@
+this is my first add

7.6显示指定文件是什么人在什么时间修改过

$ git blame [file]

$ git blame Readme.md

image

7.7显示暂存区和上一个 commit 的差异

$ git diff --cached []

$ git diff --cached 89d1d5b198a77deb9096e11b02aec7187221f384

image

**不写commitID 默认是最新的commit ID **

image

7.8显示工作区与当前分支最新 commit 之间的差异

$ git diff HEAD

image

7.9显示两个分支之间的差异

$ git diff [first-branch]...[second-branch]
$ git diff DevOps_RC3 DevOps_Dev

diff --git a/3.txt b/3.txt
new file mode 100644
index 0000000..13137df
--- /dev/null
+++ b/3.txt
@@ -0,0 +1 @@
+我想测试一下cherry-pick [commit]
diff --git a/Readme.md b/Readme.md
index 561f60a..8b12de6 100644
--- a/Readme.md
+++ b/Readme.md
@@ -1,3 +1,2 @@
 this is my first add
-修改了V0.3FixBug的Bug
diff --git a/3.txt b/3.txt
new file mode 100644
index 0000000..13137df
--- /dev/null
+++ b/3.txt
@@ -0,0 +1 @@
+我想测试一下cherry-pick [commit]
diff --git a/Readme.md b/Readme.md
index 561f60a..8b12de6 100644
--- a/Readme.md
+++ b/Readme.md
@@ -1,3 +1,2 @@
 this is my first add
-修改了V0.3FixBug的Bug
-测试一下git diff
+this is my first commit -a
diff --git a/ReleaseNotes.md b/ReleaseNotes.md
new file mode 100644
index 0000000..1a4e89a
--- /dev/null
+++ b/ReleaseNotes.md
@@ -0,0 +1,26 @@
+Version:(Dev_v1.0)
+
+Introduction:
+       welcome to use NCF package , using it , you can develop your personal control-application code  quickly.
+
+       more information http://www.devops.com
+
+       this version is from V0.3FixBug
+
+
+System Requirements:
+       - CPU:  2C
+       - OS:   windows7 64位
+       - Memory:  1G
+       - Hard-disk:  40G
+       - Others:
+Runtime Environment
+
+V0.3FixBug
+       - 修改Demo程序bug
+
+Dev_v1.0
+       - 建立DevOps平台项目
+       - 建立DevOps项目
+       - 需要.netframework4.6.1支持
+       - 需要mysqlV5.1.5支持
(END)

7.10显示两次commit之间的差异

$ git diff 304db4dfdd ab794ed848

image

7.11显示某次提交的元数据和内容变化

$ git show [commit]

$ git show ab794ed848f8cbe0f4b1be065555f5e0b1124d5d

image

7.12显示某次提交发生变化的文件

$ git show --name-only [commit]

$ git show --name-only b54962a30

image

7.13显示某次提交时,某个文件的内容

$ git show [commit]:[filename]

$ git show 304db4dfdd:Readme.md

image

7.14显示所有的提交

$ git reflog

image

8.远程同步

8.1拉取远程仓库的所有变动

$ git fetch [remote]
$ git fetch origin

image

8.2显示所有远程仓库

$ git remote -v
image

8.3显示某个远程仓库的信息

$ git remote show [remote]
$ git remote show origin

warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
* remote origin
  Fetch URL: http://192.168.145.89/devops/devops.git
  Push  URL: http://192.168.145.89/devops/devops.git
  HEAD branch: master
  Remote branches:
    DevOps_Dev        tracked
    DevOps_RC         tracked
    DevOps_RC1        tracked
    DevOps_RC3        tracked
    DevOps_Release    tracked
    DevOps_V0.3FixBug tracked
    master            tracked
  Local branches configured for 'git pull':
    DevOps_Dev        merges with remote DevOps_Dev
    DevOps_RC         merges with remote DevOps_RC
    DevOps_RC1        merges with remote DevOps_RC1
    DevOps_RC2        merges with remote DevOps_RC1
    DevOps_Release    merges with remote DevOps_Release
    DevOps_V0.3FixBug merges with remote DevOps_V0.3FixBug
    master            merges with remote master
  Local refs configured for 'git push':
    DevOps_Dev        pushes to DevOps_Dev        (up to date)
    DevOps_RC         pushes to DevOps_RC         (up to date)
    DevOps_RC1        pushes to DevOps_RC1        (up to date)
    DevOps_RC3        pushes to DevOps_RC3        (fast-forwardable)
    DevOps_Release    pushes to DevOps_Release    (up to date)
    DevOps_V0.3FixBug pushes to DevOps_V0.3FixBug (up to date)
    master            pushes to master            (up to date)

8.4增加一个新的远程仓库,并命名

$ git remote add [shortname] [url]

$ git remote add DevOps_MaTest http://192.168.145.89/devops/devops_matest.git

$ git remote -v

DevOps_MaTest   http://192.168.145.89/devops/devops_matest.git (fetch)
DevOps_MaTest   http://192.168.145.89/devops/devops_matest.git (push)
origin  http://192.168.145.89/devops/devops.git (fetch)
origin  http://192.168.145.89/devops/devops.git (push)

8.5取回远程仓库另一分支的变化,并与本地分支进行合并

$ git pull [remote] [branch]

8.5.1合并之前DevOps_RC1文件信息展示

cat Readme.md

image

8.5.2切换至要从远端仓库拉取的另一分支DevOps_V0.3FixBug进行编辑修改

image

8.5.3将修改推送到远程仓库

$ git push origin DevOps_V0.3FixBug:DevOps_V0.3FixBug

8.5.4切换至要拉取此分支的分支

$ git checkout DevOps_RC1

image

8.5.6拉取另一分支的最新变化至本地的当前分支

$ git pull origin DevOps_V0.3FixBug

image
看到括号内的变化(DevOps_RC1|MERGING)表示正在进行合并操作,此时需要查看当前分支的状态,使用git status

8.5.7查看当前分支的合并状态

$ git status

image

8.5.8根据提示解决当前合并冲突

$ git commit -m "DevOps_V0.3FixBug分支合并至DevOps_RC1"

image

8.6上传本地指定分支到远程仓库

$ git push [remote] [branch]
$ git push origin DevOps_V0.3FixBug:DevOps_V0.3FixBug

warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 414 bytes | 103.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for DevOps_V0.3FixBug, visit:
remote:   http://192.168.145.89/devops/devops/-/merge_requests/new?merge_request%5Bsource_branch%5D=DevOps_V0.3FixBug
remote:
To http://192.168.145.89/devops/devops.git
   1c6e411..69d2d0f  DevOps_V0.3FixBug -> DevOps_V0.3FixBug

8.7强行推送当前分支到远程仓库,即使有冲突

$ git push [remote] --force

8.7.1查看当前远程分支DevOps_Dev文件信息

image

8.7.2切换至要推送的当前分支DevOps_RC1查看文件信息

image
通过8.7.1和8.7.2的两幅图的对比,发现此文件的第三行存在冲突;

8.7.3强行推送当前分支DevOps_RC1至远程分支DevOps_Dev

$ git push origin DevOps_RC1:DevOps_Dev --force

$ git push origin DevOps_RC1:DevOps_Dev --force
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 383 bytes | 127.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for DevOps_Dev, visit:
remote:   http://192.168.145.89/devops/devops/-/merge_requests/new?merge_request%5Bsource_branch%5D=DevOps_Dev
remote:
To http://192.168.145.89/devops/devops.git
 + 3d9be30...0cedce7 DevOps_RC1 -> DevOps_Dev (forced update)

8.7.4 切换到DevOps_Dev

$ git checkout DevOps_Dev

image

8.7.5 按照提示拉取最新的分支信息与本地进行合并

$git pull
image

8.7.6查看当前分支DevOps_Dev的状态

$ git status

image
按照提示解决所有冲突,并提交;
image

8.7.7进行比较(当前分支的文件与远程仓库拉取下来的文件做比较)

image

8.7.8冲突解决

image

image

image

8.7.9按照提示进行提交和推送

image

8.8推送所有分支到远程仓库

$ git push [remote] --all

8.8.1新建分支4,5,6

$ git branch  DevOps_RC4
$ git branch  DevOps_RC5
$ git branch  DevOps_RC6

image

8.8.2查看当前的远程分支

$ git branch -r

image

8.8.3 推送所有新建分支到远程仓库

$ git push origin --all

image

8.8.4 查看远程分支

$ git branch -r

image

9.撤销

9.1版本回滚

$ git checkout [commit]

$ git checkout 3c37654e45fa5d0585b676cd7e66d01651e63ccc
Note: switching to '3c37654e45fa5d0585b676cd7e66d01651e63ccc'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 3c37654 添加了需求规格说明书

image

9.2基于当前的commit 创建分支并进行跳转

$ git switch -c temp

image

9.3重置暂存区与工作区,与上一次 commit 保持一致

$ git reset --hard

9.4重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变

$ git reset [commit]
$ git reset f1a4a707f9cb

image

9.5重置当前分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 一致

$ git reset --hard [commit]
$ git reset --hard 3c37654e45fa
image

9.6重置当前 HEAD 为指定 commit,但保持暂存区和工作区不变

$ git reset --keep [commit]
$ git reset --keep 6771f4be20d7

======================================================================
创作不易,本人热衷开源共享 《Git常用命令超级详细》
======================================================================

转载请附上链接:
https://www.cnblogs.com/cndevops/p/14993331.html

posted on 2021-07-10 18:20  DevOps_SRE  阅读(1146)  评论(7编辑  收藏  举报