🔵Git命令

本文最后更新于:2024年4月25日 上午

git练习

克隆远程

1
2
git clone < URL | SSH >                                        # 克隆主分支
git clone -b < baranchName > < URL | SSH > < baranchName > # 克隆指定分支

新建GitHub仓库

登录GitHub,点击new新增一个仓库

Repository name仓库名

Public (其他人可见)
Pravite (其他人不可见)

点击Create repository创建仓库

关联本地仓库

打开仓库

点击code,复制HTTPS地址

克隆远程仓库至本地

1
git clone <复制的地址> # 克隆远程仓库至本地

此时目录下新增一个HelloWorld文件夹,仓库已克隆至本地

1
2
3
└─HelloWorld
|─.git # 为隐藏文件夹
└─README.md

更新本地仓库

HelloWorld文件夹中新建一个hello.txt文件,写入你好!!!

1
2
3
git add hello.txt # 更新`hello.txt`至git仓库

git add . # 或使用该命令更新所有变动至仓库
1
git status        # 查看仓库提交状态
1
git commit -m "<更改说明>"

gitcommit规范

《Git提交规范》

上传至远程仓库

1
git push origin main

撤销push

查看要回退的版本

git log查看版本号commit,按q退出

若要撤销auto updata提交,即要回退到完善提交

复制完善分支的commit

1
2
3
4
5
6
7
8
9
10
11
12
13
git log

# > commit 42686caaa16005cddd24e229578585e60d3c6805
# > Author: Jon-a-than
# > Date: Sun Jan 9 18:06:18 2022 +0800
# >
# > auto updata
# >
# > commit db07e120277c2a8765ee2e1f003f8c97004d8c6a
# > Author: Jon-a-than
# > Date: Sun Jan 9 11:22:32 2022 +0800
# >
# > 完善

版本回退

使用reset回退

--soft不会改变本地代码

--hard会改变本地代码至回退版本

1
git reset <commit> --soft
1
git reset db07e120277c2a8765ee2e1f003f8c97004d8c6a --soft

强制push

直接push是会失败的,要加上强制指令--force

1
git push origin <分支名> --force

修改最近一次的commit(已push)

编辑

输入git commit --amend进入编辑窗口,输入i开始编辑commit信息

1
git commit --amend

编辑好之后,按键Esc退出编辑模式,输入:wq保存并退出编辑。git push -f

1
git push --force

GPG

生成GPG密匙

打开Git Bash

1
2
3
4
5
6
7
8
9
gpg --full-generate-key
# 按提示生成GPG密匙
# 期间会让你设置一个密码

gpg --list-secret-keys --keyid-format LONG
# 获取密匙列表

gpg --armor --export 134A2A9D3D473233
# 获取指定ID的密匙

细节如下,已用符号加强提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
> MINGW64 ~/Desktop
> 💲 gpg --full-generate-key
> gpg (GnuPG) 2.2.29-unknown; Copyright (C) 2021 Free Software Foundation, Inc.
> This is free software: you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law.
>
> Please select what kind of key you want:
> (1) RSA and RSA (default)
> (2) DSA and Elgamal
> (3) DSA (sign only)
> (4) RSA (sign only)
> (14) Existing key from card
> Your selection? 1 💬
> RSA keys may be between 1024 and 4096 bits long.
> What keysize do you want? (3072) 4096 💬密匙长度
> Requested keysize is 4096 bits
> Please specify how long the key should be valid.
> 0 = key does not expire
> <n> = key expires in n days
> <n>w = key expires in n weeks
> <n>m = key expires in n months
> <n>y = key expires in n years
> Key is valid for? (0) 5y 💬设置过期时间,0不过期
> Key expires at Wed May 12 18:21:00 2027
> Is this correct? (y/N) y 💬
>
> GnuPG needs to construct a user ID to identify your key.
>
> Real name: Jon-a-than 💬
> Email address: 2140145186@qq.com 💬
> Comment:
> You selected this USER-ID:
> "Jon-a-than <2140145186@qq.com>"
>
> Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o 💬
> We need to generate a lot of random bytes. It is a good idea to perform
> some other action (type on the keyboard, move the mouse, utilize the
> disks) during the prime generation; this gives the random number
> generator a better chance to gain enough entropy.
> We need to generate a lot of random bytes. It is a good idea to perform
> some other action (type on the keyboard, move the mouse, utilize the
> disks) during the prime generation; this gives the random number
> generator a better chance to gain enough entropy.
> gpg: /.gnupg/trustdb.gpg: trustdb created
> gpg: key 134A2A9D3D473233 marked as ultimately trusted
> gpg: directory '.gnupg/openpgp-revocs.d' created
> gpg: revocation certificate stored as '.gnupg/openpgp-revocs.d/56633.rev'
> public and secret key created and signed.
>
> pub rsa4096 2022-05-13 [SC] [expires: 2027-05-12]
> 566E49046FC9EB2A7291BF22134A2A9D3D473233
> uid Jon-a-than <2140145186@qq.com>
> sub rsa4096 2022-05-13 [E] [expires: 2027-05-12]
>
>
> MINGW64 ~/Desktop
> 💲 gpg --list-secret-keys --keyid-format LONG
> gpg: checking the trustdb
> gpg: marginals needed: 3 completes needed: 1 trust model: pgp
> gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
> gpg: next trustdb check due at 2027-05-12
> /.gnupg/pubring.kbx
> ---------------------------------
> sec rsa4096/📌134A2A9D3D473233⚡ 2022-05-13 [SC] [expires: 2027-05-12]
> 566E49046FC9EB2A7291BF22134A2A9D3D473233
> uid [ultimate] Jon-a-than <2140145186@qq.com>
> ssb rsa4096/59E8AC1273A5F64D 2022-05-13 [E] [expires: 2027-05-12]
>
>
> MINGW64 ~/Desktop
> 💲 gpg --armor --export 134A2A9D3D473233
> -----BEGIN PGP PUBLIC KEY BLOCK-----
>
> mQINBGJ+MUcBEADF5NvSklklkCrMPe2snKarVRGD5WaA0krkaFF6JmvgA3Jr+aki
> bNqkU5p0wUkcH8XbbU18xTxQry0M4AfBCDEjKcu3WjBoRD47njYOjp2T7VSNzBKE
> 0jUdoU0UzUuy92vmZ8dqaTV8Il/Y8jniLnkq7KN+9k6tZere8B3/l4ja0DmWJjIU
> IjJWOhUte6tP23sT7pd/e2jQwJ1v0FggJdKZDmY2E91oMeBAhsd/FXZmesvBP5hX
> 5A5Kf+wXtAog0YL1q2EBt5FLglTRGFCacAHAU3vm4Z+OquX2fpwUg9vu1ovQsF2G
> FKS0MB3DgqFkNRb4uK7BFZsuLWqws4fPG1uw8wfg1eFhl5owwOM8UTMrI+hfvK5T
> SghkHaF1zm5mls+pVS9cltZiEAT9ecuYsoiH6rcSkD20zpP8MouKujOfDhgl9ppt
> RZnXa2RpBZIFYLqIpjCMS5uAz2Z/Aq74z779h4dmcWhreFpAstfN6LKe3qztJpDE
> eCtQ5nwMfZDK4qWV0SeCjTPXEro8JGUc3RmYuuZsrHpxdQQlZI7UKMKykhJkSMnL
> Eq1LDUY3l+B+ZKd6N8G2p0rhX/93/kjMgUydq6SQKtsrFLZjuqFaMNpOOPGmkBn1
> i/y1gSHPiyc8FGZhuootBbe32kkklnH7klRM8/I9W3xKeb11zCrIIYU7+wARAQAB
> tB5Kb24tYS10aGFuIDwyMTQwMTQ1MTg2QHFxLmNvbT6JAlQEEwEIAD4WIQRWbkkE
>
> -----END PGP PUBLIC KEY BLOCK-----

GitHub设置GPG

gpg --armor --export 134A2A9D3D473233后的内容均复制

粘贴至新GitHub新建的GPG

本地Git开启GPG

1
2
git config --global user.signingkey 134A2A9D3D473233
git config --global commit.gpgsign true # 全局开启

GPG取消

对当前仓库取消

1
git config commit.gpgsign false

Git升级

1
git update-git-for-windows

🔵Git命令
https://qingshaner.com/Git命令/
作者
清山
发布于
2021年11月18日
许可协议