仓库创建且管理
- (1)创建新的仓库工作目录
D:\code\gitpro\mybook
- (2)初始化工作目录为仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook
$ git init
输出:
Initialized empty Git repository in D:/code/gitpro/mybook/.git/
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
- (3)在当前的仓库下新增文件且添加文件到【暂存区】
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ touch xiaozhong.txt
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ nano xiaozhong.txt
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git add xiaozhong.txt
输出:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git add xiaozhong.txt
warning: LF will be replaced by CRLF in xiaozhong.txt.
The file will have its original line endings in your working directory
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
图:
image.png
- (4)查看当前的仓库的状态
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: xiaozhong.txt
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
- (5)commit 提交到仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git commit -m "第一次提交仓库"
[master (root-commit) b1aee96] 第一次提交仓库
1 file changed, 1 insertion(+)
create mode 100644 xiaozhong.txt
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
nothing to commit, working tree clean
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
当前状态信息显示,说明当前仓库没有什么最新的更新或没有添加或修改的!
关于git status下的颜色:
绿色:表示已添加到暂存区
红色:表示当前工作去的文件被修改
- (6)修改已添加到暂存区的文件后,查看状态
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: xis.py
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
提示可以使用git add 更新到暂存在
(use "git restore <file>..." to discard changes in working directory)
modified: xis.py
提示可以使用git restore 进行回滚到之前工作区的版本,把修改的去除
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
- (7)回滚之前的版本信息
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git restore xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
(8)建议提交的时候,把【新增】的文件和【修改】的文件分两次进行提交仓库
(9)两次修改文件,第一次修改提交暂存区,第二次修改不添加到暂存区,然后直接的提交仓库的情况
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ nano xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git add .
warning: LF will be replaced by CRLF in xis.py.
The file will have its original line endings in your working directory
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ nano.exe xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git commit -m "第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景"
[master 335f2c7] 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
1 file changed, 1 insertion(+)
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (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: xis.py
no changes added to commit (use "git add" and/or "git commit -a")
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
添加二次修改后的再查看
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git add .
warning: LF will be replaced by CRLF in xis.py.
The file will have its original line endings in your working directory
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git commit -m "第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景"
[master 9e410bc] 第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
1 file changed, 1 insertion(+), 1 deletion(-)
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
nothing to commit, working tree clean
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
仓库操作历史
- (1)查看提交仓库的历史
$ git log
或单号的输出
$ git log --pretty=oneline
9e410bc8cb85da860431f7d2e1bb36946cd4f704 (HEAD -> master) 第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
335f2c7a319fb95820674720ce90342d9d0236d1 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
输出:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git log
commit 9e410bc8cb85da860431f7d2e1bb36946cd4f704 (HEAD -> master)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:36:46 2021 +0800
第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
commit 335f2c7a319fb95820674720ce90342d9d0236d1
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:35:00 2021 +0800
第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
commit 271a8c03b15d65ce783075163248697c968869e8
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:22:09 2021 +0800
第二次提交
commit b1aee9677faf7031b697734f4f798168adb6a0d6
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 15:47:47 2021 +0800
第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
仓库版本检出
image.png- (1)检出指定的版本
335f2c7a319fb95820674720ce90342d9d0236d1 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git
git.exe git-flow git-receive-pack.exe
git-askyesno.exe git-gui.exe git-upload-archive.exe
git-credential-helper-selector.exe gitk git-upload-pack.exe
gitdll.dll gitk.exe GitWCRev.exe
gitdll32.dll git-lfs.exe GitWCRevCOM.exe
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git checkout b1ae
Note: switching to 'b1ae'.
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 b1aee96 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((b1aee96...))
$
image.png$ git checkout b1ae b1ae 是版本库库的 缩短的最签名的几个字符~~
- (2)回滚后查看的版本历史:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((b1aee96...))
$ git log --pretty=oneline
b1aee9677faf7031b697734f4f798168adb6a0d6 (HEAD) 第一次提交仓库
- (3)检出当前最新的版本回来:
$ git checkout master
操作输出:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((b1aee96...))
$ git log --pretty=oneline
b1aee9677faf7031b697734f4f798168adb6a0d6 (HEAD) 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((b1aee96...))
$ git checkout master
Previous HEAD position was b1aee96 第一次提交仓库
Switched to branch 'master'
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git log --pretty=oneline
9e410bc8cb85da860431f7d2e1bb36946cd4f704 (HEAD -> master) 第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
335f2c7a319fb95820674720ce90342d9d0236d1 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
仓库版本操作管理-版本打标签
- (1)给版本打标签
操作:
$ git tag v1
$ git checkout v1 回滚最新的打了标签的版本(第四版本)
$ git checkout v1^ 回滚的最新大佬表的再上一个版本信息(第三版本)
输出:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git log --pretty=oneline
9e410bc8cb85da860431f7d2e1bb36946cd4f704 (HEAD -> master) 第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
335f2c7a319fb95820674720ce90342d9d0236d1 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git tag v1
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git log --pretty=oneline
9e410bc8cb85da860431f7d2e1bb36946cd4f704 (HEAD -> master, tag: v1) 第si次提交。两次文件的操作之后,第二次的操作提交暂存
区的场景
335f2c7a319fb95820674720ce90342d9d0236d1 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git checkout v1^
Note: switching to 'v1^'.
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 335f2c7 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((335f2c7...))
$ git log --pretty=oneline
335f2c7a319fb95820674720ce90342d9d0236d1 (HEAD) 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((335f2c7...))
$
- (2)检出某个版本给某个版本打标签:
操作:
$ git checkout v1^
$ git tag v1-beta
$ git log --pretty=oneline
$ git checkout v1
$ git log --pretty=oneline
输出:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$ git checkout v1^
Previous HEAD position was 9e410bc 第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
HEAD is now at 335f2c7 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((335f2c7...))
$ git tag v1-beta
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1-beta))
$ git log --pretty=oneline
335f2c7a319fb95820674720ce90342d9d0236d1 (HEAD, tag: v1-beta) 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1-beta))
$ git checkout v1
Previous HEAD position was 335f2c7 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
HEAD is now at 9e410bc 第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$ git log --pretty=oneline
9e410bc8cb85da860431f7d2e1bb36946cd4f704 (HEAD, tag: v1, master) 第si次提交。两次文件的操作之后,第二次的操作提交暂存区
的场景
335f2c7a319fb95820674720ce90342d9d0236d1 (tag: v1-beta) 第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
271a8c03b15d65ce783075163248697c968869e8 第二次提交
b1aee9677faf7031b697734f4f798168adb6a0d6 第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$
- (3)按标签检测版本:
$ git checkout v1
$ git checkout v1-beta
- (4)查看有打了表情的版本 标签:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$ git tag
v1
v1-beta
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$ git log master -all
error: switch `l' expects a numerical value
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$ git log master --all
commit 9e410bc8cb85da860431f7d2e1bb36946cd4f704 (HEAD, tag: v1, master)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:36:46 2021 +0800
第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
commit 335f2c7a319fb95820674720ce90342d9d0236d1 (tag: v1-beta)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:35:00 2021 +0800
第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
commit 271a8c03b15d65ce783075163248697c968869e8
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:22:09 2021 +0800
第二次提交
commit b1aee9677faf7031b697734f4f798168adb6a0d6
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 15:47:47 2021 +0800
第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$
仓库版本操作管理-撤销操作
- (1) 恢复到最新的版本
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook ((v1))
$ git checkout master
Switched to branch 'master'
- (2) 修改文件,但是不提交暂存在,只是再工作去修改文件的情况的恢复到没修改前的文件
操作:
$ nano xis.py
$ git checkout xis.py
$ git status
输出:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ nano xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (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: xis.py
no changes added to commit (use "git add" and/or "git commit -a")
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git checkout xis.pyo
error: pathspec 'xis.pyo' did not match any file(s) known to git
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git checkout xis.py
Updated 1 path from the index
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
nothing to commit, working tree clean
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
-
(3) 修改文件,且提交到了暂存区后,恢复到没修改前的文件
image.png
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git add .
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git restore --staged xis.py
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (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: xis.py
no changes added to commit (use "git add" and/or "git commit -a")
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
- (3) 修改文件,且commit 到了仓库去,撤销操作
操作:
$ git add .
$ git commit -m "第五次提交测试"
$ git log
$ git revert HEAD
输出:
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git add .
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git commit -m "第五次提交测试"
[master c111737] 第五次提交测试
1 file changed, 2 insertions(+)
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git log
commit c111737698fda691bc4766650b5da1785038e831 (HEAD -> master)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 17:49:03 2021 +0800
第五次提交测试
commit 9e410bc8cb85da860431f7d2e1bb36946cd4f704 (tag: v1)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:36:46 2021 +0800
第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
commit 335f2c7a319fb95820674720ce90342d9d0236d1 (tag: v1-beta)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:35:00 2021 +0800
第三次提交。两次文件的操作之后,第二次的操作修改不提交暂存区的场景
commit 271a8c03b15d65ce783075163248697c968869e8
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:22:09 2021 +0800
第二次提交
commit b1aee9677faf7031b697734f4f798168adb6a0d6
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 15:47:47 2021 +0800
第一次提交仓库
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
指定回滚某个版本:
指定版本的标识符 :This reverts commit 1fdb0a52fe3e72bbb44cf16cd275877ecfa72252.
指定版本的标识符 : This reverts commit aa7d3424dec6fb2c31be250b896d38880b9054df.
This reverts commit c111737698fda691bc4766650b5da1785038e831.
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$ git log
commit 866fc98a58653843e2b7c60e9a160f7a7f02ff38 (HEAD -> master)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 17:59:52 2021 +0800
第6次提交测试
commit 2547e95ddf3c6d2bee789fd15ca6a80ab4fe428c
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 17:58:54 2021 +0800
Revert "Revert "Revert "第五次提交测试"""
This reverts commit 1fdb0a52fe3e72bbb44cf16cd275877ecfa72252.
commit 1fdb0a52fe3e72bbb44cf16cd275877ecfa72252
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 17:51:09 2021 +0800
Revert "Revert "第五次提交测试""
This reverts commit aa7d3424dec6fb2c31be250b896d38880b9054df.
commit aa7d3424dec6fb2c31be250b896d38880b9054df
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 17:50:19 2021 +0800
Revert "第五次提交测试"
This reverts commit c111737698fda691bc4766650b5da1785038e831.
commit c111737698fda691bc4766650b5da1785038e831
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 17:49:03 2021 +0800
第五次提交测试
commit 9e410bc8cb85da860431f7d2e1bb36946cd4f704 (tag: v1)
Author: zyx <308711822@qq.com>
Date: Tue Jan 12 16:36:46 2021 +0800
第si次提交。两次文件的操作之后,第二次的操作提交暂存区的场景
commit 335f2c7a319fb95820674720ce90342d9d0236d1 (tag: v1-beta)
Author: zyx <308711822@qq.com>
mayn@DESKTOP-16CKEN1 MINGW64 /d/code/gitpro/mybook (master)
$
网友评论