# 工作中git常用命令

# 常见的命令

# git branch

查看当前分支信息

# git checkout -b origin/

切换与远程分支一样信息并创建

# git branch -D

删除某个分支

# git add .

提交改动到缓存区, 'git add .'提交所有改动, 'git add <文件>'提交指定文件

# git checkout <文件>

丢失某个文件的改动

# git reset HEAD <文件>

取消某个文件在缓存区

# git commit -m ""

提交commit信息

# git push

'git push origin '添加到远程,如果本地分支与远程存在关联关系,可
以省略'git push origin'或者'git push'

# git push --set-upstream origin

设置本地分支追踪远程分支

# git rebase -i HEAD~n

合并多个commit

# git cherry-pick

提某个commit到这个分支上

# git rebase origin/

将分支提前到指定分支

# git blame

查看某个文件某行谁提交的代码

# git branch -m old_branch new_branch

重命名分支

# git cherry-pick commit_id

挑选commit点

# git diff

查看改动差异

# 工作需要使用的命令

# git commit --amend

使用场景:提交一个mr之后发现有问题,修改之后不需要改变commit信息。需要使用命令
同样也可以用于需改某次commit提交信息

git commit --amend
git push origin <feat-branch-name> -f
1
2

# git reset --hard origin/develop

使用场景:本地代码不知道改动了多少,需要保持与远程一致

git fetch --all
git reset --hard origin/develop
git pull
1
2
3

# drop commit

使用场景:需要删除某个commit

git log 查看需要删除的某个commit
找到前一个commit
git rebase -i commitId
将需要删除的commit,pick改为drop
git log 可以查看已经删除
1
2
3
4
5

# git commit --amend

使用场景:修改已经提交的commit信息

git commit --amend
进入编辑模式,修改commit信息
git push -f
1
2
3

git reset --soft HEAD~1 使用场景:本地提交代码报错,远程有人提交了代码,测回本次提交

测回提交
git rest --soft HEAD~1
//返回缓存
git stash
git pull origin master
git stash pop
git commit -m ""
git push
1
2
3
4
5
6
7
8

# 配置命令

# git --help

帮助命令

# git log

最近的提交信息

# history

可以查看shell里面的执行过什么命令

# git remote -v

查看git仓库

# 相关问题

# 1.一台电脑如何配置多个SSH KEY,多个用户身份提交代码

使用场景:当想在同一台电脑上用多个用户身份提交代码,例如:如果公司项目代码在两个不同git库上面。或者(在公司电脑上用公司用户名提交代码,用私有用户名提交自己的代码到github上,可以使用下面的方法。)

1.生成第一个ssh key
ssh-keygen -t rsa -C "your.email@example.com"
参数:-t = The type of the key to generate  密钥的类型  -C = comment to identify the key 用于识别这个密钥的注释
 
按照提示完成三次回车,即可生成 ssh key。通过查看 ~/.ssh/id_rsa.pub 文件内容,获取到你的 public key

2.主要用户的可以gi全局配置一个用户名密码邮箱
$ git config --global user.name 用户命
$ git config --global user.password 密码

3.×××关键了×××,第二个用户配置。生成第二个ssh key
$ ssh-keygen -t rsa -C "your.email@example.com"

Generating public/private rsa key pair.
Enter file in which to save the key (/home/xxx/.ssh/id_rsa): 
×××这里不能选择默认位置,会覆盖第一个用户的。××× 输入'id_new_rsa',回车 

第二个ssh key位置$ cat ~/.ssh/id_new_rsa.pub 

4.配置基于Host的差异配置
vim ~/.ssh/config

Host gitlab.xxx.com
    HostName        gitlab.xxx.com
    User            user
    IdentityFile    ~/.ssh/id_new_rsa # 根据自己的private key目录修改
Host github.com
    HostName        github.com
    User            user
    IdentityFile    ~/.ssh/id_rsa_github # 根据自己的private key目录修改

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

1650177744425.png

也可以直接修改当前项目下的.git/config文件 1650177670455.png

# 2.git config全局配置

# git config命令的–global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

# 1.查看git配置信息
$ git config --list

# 2.查看git用户名、密码、邮箱的配置
$ git config user.name
$ git config user.password
$ git config user.email

# 3.设置git用户名、密码、邮箱的配置
$ git config user.name "xinyu"
$ git config user.password "123456"
$ git config user.email "916333567@qq.com"


# 4.设置git用户名、密码、邮箱的配置(全局配置)
$ git config --global user.name 用户命
$ git config --global user.name xinyu
$ git config --global user.password 密码
$ git config --global user.password abc0506abc
$ git config --global user.password 邮箱
$ git config --global user.email "916333567@qq.com"


# 5.修改git用户名、密码、邮箱的配置(跟设置语法一样,没有用户名就添加,有了用户名就修改)
$ git config user.name "xinyu"
# 4.修改git用户名、密码、邮箱的配置(全局配置)
$ git config --global user.name "xinyu"

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

为什么需要配置git用户名,邮箱?

因为这些内容会出现在你的每一个提交(commit)里面的,像下面这样:

$ git log #我们用git log查看当前仓库的提交(commit)日志
commit 71948005382ff8e02dd8d5e8d2b4834428eece24
Author: author <916333567@qq.com>
Date: Thu Jan 20 12:58:05 2011 +0800
Project init
下面的这两行命令就是设置用户名和email:

$ git config --global user.name author #将用户名设为author
$ git config --global user.email 916333567@qq.com #将用户邮箱设为author@corpmail.com

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

# 3.ssh连接所生成的known_hosts出现的问题(生成了ssh 可以,导致旧的信息无法认证)

Host key verification failed. fatal: Could not read from remote repository. 删除旧的公钥信息,下次链接要全部重新经过认证

ssh-keygen -f "/home/user/.ssh/known_hosts" -R (ip或者域名)
1

# 相关质料

gitee添加ssh key (opens new window)

全量分析

评 论:

上次更新时间: 4/17/2022, 2:44:26 PM