git常见命令和常见问题解决


Git 是一种分布式版本控制系统,在开发过程中,我们通常需要用到 Git 来管理代码。下面我们来看看Git 的一些常用命令:

常见命令

  1. 查看状态
git status
  1. 添加文件到暂存区
git add <file>

将指定文件添加到暂存区。也可以使用 git add . 命令将当前目录下的所有更改添加到暂存区。

  1. 提交更改
git commit -m "提交信息"

将暂存区中的修改提交到本地仓库,其中 -m 参数表示提交信息。
在这里插入图片描述

  1. 查看提交日志
git log

显示所有提交的历史记录,包括提交哈希、作者、日期和提交信息。空格键/回车键(向下滚动一行) 按q键推出。

  1. 查看差异
git diff

比较工作目录和暂存区之间的差异。也可以使用 git diff <commit1> <commit2> 来比较两个提交之间的差异。

  1. 创建分支
git branch <branch-name>

创建一个新分支。

  1. 切换分支
git checkout <branch-name>

切换到另一个分支。如果分支不存在,可以使用 git checkout -b <branch-name> 创建并切换到新分支。

  1. 合并分支
git merge <branch-name>

将指定分支合并到当前分支。

  1. 删除分支
git branch -d <branch-name>

删除指定分支。

  1. 远程仓库操作
  • 添加远程仓库
    git remote add <remote-name> <url>
    
  • 查看远程仓库列表
    git remote -v
    
  • 从远程仓库拉取更改
    git pull <remote-name> <branch-name>
    
  • 推送更改到远程仓库
    git push <remote-name> <branch-name>
    
  • 删除远程分支
    git push <remote-name> --delete <branch-name>
    

此外,Git 还有很多高级命令和选项。这里列举了一些最常用的,更多信息您可以访问 官方文档
中文推荐教程

  • 相关问题:git push 出现的openssl问题
git config --global http.sslBackend "openssl" 
git config --global http.sslVerify "false"
git config --global --unset http.proxy
git config --global --unset https.proxy

问题

问题1(git push相关)

问题:fatal: unable to access ‘https://github.com/…………/’: Recv failure: Connection was reset

  1. 在电脑的搜索栏中,输入 代理

在这里插入图片描述

  1. 然后手动修改代理服务器,

在这里插入图片描述

  1. 将代理服务器关闭,然后保存。(当前使用的电脑是win11,和win10差别不大)

在这里插入图片描述

然后重新提交git push origin main main

然后报错

问题2(git push相关)

fatal: unable to access ‘https://github.com…………’: Failed to connect to github.com port 443 after 21077 ms: Couldn’t connect to server

  1. 解决
git config --global --unset http.proxy
git config --global --unset https.proxy

问题3(git push相关)

fatal: unable to access ‘https://github.com…………’: OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 0

解决:参考网上方法:解除SSL认证

git config --global http.sslVerify "false"

然后还是出现了

fatal: unable to access 'https://github.com…………: Failed to connect to github.com port 443 after 21077 ms: Couldn’t connect to server

可能因为科学上网了,关闭之后

! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/…………
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use ‘git pull’ before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.

解决方法

git pull origin master --allow-unrelated-histories

删除github的仓库

网上的文章有说github的仓库删除,还是会被保留文件,所以建议还是不要把太过机密的信息提交到github上,或者还有其他相关的方法。

  1. 找到想要删除的仓库的位置,在project的上方的导航栏,最左边显示的是code,最后一个就是settings
    在这里插入图片描述
  2. 点击setting,然后下拉到最后,就可以看到删除仓库的操作了。
    在这里插入图片描述
  3. 然后按照流程走就行,这个比较简单,记得要输入密码,所以提前准备好密码,如果你能够记住最好啦。

如果你联系输入两个仓库,第一次输入密码之后,第二次删除就不需要输入密码了。

vercel的项目删除,在项目的settings 中,也是在最后

github新创建本地仓库的操作

在这里插入图片描述

https://github.com/XX/xx.git

git@github.com:XX/XXX.git

…or create a new repository on the command line

echo "# blog" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:Zopt/blog.git
git push -u origin main

…or push an existing repository from the command line

git remote add origin git@github.com:Zopt/blog.git
git branch -M main
git push -u origin main
## error: remote origin already exists.
git remote rm origin

然后遇到

git@github.com: Permission denied (publickey). Could not read from remote repository

解决::https://blog.csdn.net/u013250861/article/details/130761369

具体:

原因分析
Permission denied (publickey) 没有权限的publickey ,出现这错误一般是以下两种原因

客户端与服务端未生成 ssh key
客户端与服务端的ssh key不匹配
找到问题的原因了,解决办法也就有了,重新生成一次ssh key ,服务端也重新配置一次即可。

客户端生成ssh key
ssh-keygen -t rsa -C “470812087@qq.com”
470812087@qq.com改为自己的邮箱即可,途中会让你输入密码啥的,不需要管,一路回车即可,会生成你的ssh key。(如果重新生成的话会覆盖之前的ssh key。)

然后再终端下执行命令:

ssh -v git@github.com

最后两句会出现

No more authentication methods to try.

Permission denied (publickey).

然后到 ssh-agent -s 出现问题 在bash中操作

unable to start ssh-agent service, error :1058

解决:https://blog.csdn.net/weixin_39370315/article/details/133440677

在powershell中操作,解决

继续

在操作

ssh-add ~/.ssh/id_rsa
~/.ssh/id_rsa: No such file or directory  ##报错

## 解决 该路径C:\Users\Administrator/.ssh/id_rsa
## 上面的PS  ssh-keygen -t rsa -C "youremail"
## Generating public/private rsa key pair.
## Enter file in which to save the key (`C:\Users\Administrator/.ssh/id_rsa`):

显示了密钥存储的路径为:C:\Users\Administrator/.ssh/id_rsa

所以命令改为

ssh-add C:\Users\Administrator/.ssh/id_rsa
Identity added: C:\Users\Administrator/.ssh/id_rsa (youremail.com) ##回复 成功

相关的问题:因为不是管理员权限的问题

$ ssh-add ~/.ssh/id_rsa
出现Could not open a connection to your authentication agent.
这时可以使用:ssh-agent bash 命令,然后再次使用ssh-add ~/.ssh/id_rsa_name这个命令就没问题了。
——————————————

原文链接:https://blog.csdn.net/u013250861/article/details/130761369

拷贝C:\Users\Administrator/.ssh/路径下的 id_rsa.pub 文件中的密钥信息,就是一堆字母数字:以开头ssh-rsa 以你的邮箱结尾

在github的账户的settings 的SSH and GPG keys 中添加 New SSH key ,然后输入密码,添加成功。
在这里插入图片描述- 添加成功,github 的导航栏下方提示

在这里插入图片描述

  • 电脑端验证:

验证ssh -T git@github.com
Hi yourgithubNickName! You’ve successfully authenticated, but GitHub does not provide shell access.

github添加SSH连接设置

https://blog.csdn.net/qq_32618327/article/details/104418818

github的中文文档

https://docs.github.com/zh

git提交缓慢的问题

解决办法1:https://www.cnblogs.com/virus1996/p/9493956.html

 ping github.com #没问题  20.205.243.166
 ping github.global.ssl.fastly.net #请求超时 
 ##解决失败

解决办法2:https://segmentfault.com/a/1190000040644345

ignored File……添加不了
在这里插入图片描述

Ctrl + ALT + a会全部提交 如 git add . 效果

TODO:查看webstorm的快捷设置

查看当前git的配置

git config --global --list

配置本地的路径

‪C:\Users\Administrator.gitconfig

报错:error: remote origin already exists.

1、先删除远程 Git 仓库
$ git remote rm origin
2、再添加远程 Git 仓库
git remote add origin (这儿跟原来链接的仓库)

git的配置命令

git config user.name “youName”
git config user.email youemialName@example.com

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值