Git 常见问题
.gitignore
规则不生效?
只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore
是无效的
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
怎样生成 SSH 公钥?
ssh-keygen
#Generating public/private rsa key pair.
#生成公钥/私钥rsa密钥对
#Enter file in which to save the key (/root/.ssh/id_rsa):
#输入密钥文件保存的位置
#Created directory '/root/.ssh'.
#创建目录 "/root/.ssh"
#Enter passphrase (empty for no passphrase):
#输入密码(无密码短语为空)
#Enter same passphrase again:
#再次输入相同的密码
#Your identification has been saved in /root/.ssh/id_rsa.
#您的身份已保存在 /root/.ssh/id_rsa
#Your public key has been saved in /root/.ssh/id_rsa.pub.
#您的公钥已保存在 /root/.ssh/id_rsa.pub
#The key fingerprint is:
#SHA256:hnACpdf1dOno2Zis+1WMgBqXIhDQpFjZ0jPp7vnUyjw root@d157f72da45c
#The key's randomart image is:
#+---[RSA 2048]----+
#|.+**.. . . .. |
#|.o++*. . = .. |
#|o .o=o= + oo |
#| ..= * ...o |
#| . o So =. o |
#| . o * .. |
#| . .. .. . |
#| o+E.. . |
#| .=..o. |
#+----[SHA256]-----+
如何退至指定提交版本?
# commitId之后提交的commit都去除
git reset --hard [commitId]
git pull
报错?
$ git pull
# 错误信息
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'
# 解决方法
git add -u
git commit
引用
- pull会使用git merge导致冲突,需要将冲突的文件resolve掉 git add -u, git commit之后才能成功pull.
- 如果想放弃本地的文件修改,可以使用git reset --hard FETCH_HEAD,FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull. 注意:git merge会形成MERGE-HEAD(FETCH-HEAD) 。git push会形成HEAD这样的引用。HEAD代表本地最近成功push后形成的引用。
$ git pull
# 错误信息
You are not currently on a branch, so I cannot use any
'branch.<branchname>.merge' in your configuration file.
Please specify which remote branch you want to use on the command
line and try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
# 解决方法
git checkout -b temp
git checkout master
# 错误信息
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: '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
git push
# 错误信息
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 解决方法
git push -f
# 错误信息
you need to resolve your current index first
# 解决方法
git reset --merge
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统