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

文章目录

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

常见命令

  1. 查看状态

    git status

  2. 添加文件到暂存区

    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 复制代码
git pull origin master --allow-unrelated-histories

删除github的仓库

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

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

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

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

github新创建本地仓库的操作

BAHS 复制代码
https://github.com/XX/xx.git

[email protected]:XX/XXX.git

...or create a new repository on the command line

bash 复制代码
echo "# blog" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin [email protected]:Zopt/blog.git
git push -u origin main

...or push an existing repository from the command line

BASH 复制代码
git remote add origin [email protected]:Zopt/blog.git
git branch -M main
git push -u origin main
bash 复制代码
## error: remote origin already exists.
git remote rm origin

然后遇到

[email protected]: 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 "[email protected]"

[email protected]改为自己的邮箱即可,途中会让你输入密码啥的,不需要管,一路回车即可,会生成你的ssh key。(如果重新生成的话会覆盖之前的ssh key。)

然后再终端下执行命令:

ssh -v [email protected]

最后两句会出现

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中操作,解决

继续

在操作

bash 复制代码
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

所以命令改为

bash 复制代码
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 [email protected]

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

bash 复制代码
 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的配置

bash 复制代码
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 [email protected]

相关推荐
belldeep11 小时前
如何阅读、学习 Git 核心源代码 ?
git·学习·源代码
我不是秃头sheep12 小时前
Git安装教程及常用命令
git
sduwcgg21 小时前
git经验
git
麻雀无能为力21 小时前
git的使用
git
算法歌者1 天前
Visual Studio 项目 .gitignore 文件指南
git·visual studio
江边垂钓者1 天前
git cherry-pick和git stash命令详解
git
Lw老王要学习1 天前
Linux架构篇、第五章git2.49.0部署与使用
linux·运维·git·云计算·it
爱学习的张哥1 天前
专栏项目框架介绍
git·fpga开发·udp·ddr·gt收发器
Aric_Jones1 天前
lua入门语法,包含安装,注释,变量,循环等
java·开发语言·git·elasticsearch·junit·lua
Sapphire~1 天前
odoo-049 Pycharm 中 git stash 后有pyc 文件,如何删除pyc文件
ide·git·pycharm