首页 > 其他分享 >git踩坑指南

git踩坑指南

时间:2022-09-22 13:22:54浏览次数:33  
标签:指南 pull git hint master push config

git踩坑指南

You've successfully authenticated, but GitHub does not provide shell access.

如果你是用仓库的http地址进行链接的,可以不管它!只要显示了Hi,bisa!这种话就说明你远程链接仓库成功了,但你要是想push通过这个链接push代码上去,推荐采用ssh进行连接

采用ssh包括两个方面:

  1. 让你本地机生成ssh密钥,和github账号上new ssh(建立链接)<搜索关键词:“GitHub配置ssh”>
  2. 本地链接你remote的仓库时,用git remote add origin [email protected]:巴拉巴拉(也就是用仓库的ssh地址进行remote),这一步成功是不进行提示的

如果你已经设置了http的链接,现在执行git remote add origin git@...报错error: remote origin already exists. 不要担心,这个意思是说你当前本地仓库中已经在链接一个远程仓库,重新设置就行。只要执行:

git rm remote origin

git remote add origin git@...就行

error: failed to push some refs to 'github.com:巴拉巴拉'

在执行git push -u origin master时,报错提示:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'github.com:bisa42/learnOSTEP.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

看到它推荐我们在push前先pull一遍代码,这很可能是因为当你与其他人合作共同写一份仓库的时候,其他人对这个仓库做了改动,但你的本地库没有更新其他人的操作,此时你直接将自己的本地库push到github上,就有几率损坏别人的更新。(当你用两个机子更新同一个仓库也会如此,即使这俩机子都由你本人操作&&机子中的本地库内容相同)

可以通过下面的命令来解决(这里的master只是仓库的分支名,请更换成你的分支):

git pull origin master

fatal: Need to specify how to reconcile divergent branches.

如果你跟我一样是个倒霉蛋,在执行了上面的命令后又出现了新的问题,那先喝口水冷静一下,查看报错信息:

 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

事实上,在这些报错出现之前,我能看到它还是成功更新了在缓存区中的文件,只不过还有点小问题罢了

image-20220922123530122

报错信息的翻译:

提示:您有不同的分支,需要指定如何协调它们。
提示:您可以通过在之前某个时间运行以下命令之一来做到这一点
提示:你的下一招:
提示:
提示:git config pull.rebase false 	# 合并(默认策略)
提示:git config pull.rebase true  	# Rebase
提示:git config pull.ff only	 	# 仅快进
提示:
提示:可以将“git config”替换为“git config——global”来设置默认值
提示:首选所有存储库。你也可以传递——rebase,——no-rebase,
提示:或命令行上的——ff-only,以覆盖配置的默认per
提示:调用。
fatal:需要指定如何协调不同的分支。

本来我要做的就是一个小仓库,并不需要考虑分支问题,所以我先尝试了提示中的默认策略:

git config pull.rebase false

结果此路不通。

当我再一次尝试git push -u origin master时,得到下面的报错:

To github.com:bisa42/learnOSTEP.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'github.com:bisa42/learnOSTEP.git'
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.

翻译:

提示:更新被拒绝,因为您当前分支的尖端位于其远程分支的后面
提示:推荐在push代码前使用类似于git pull的命令

好吧,我们现在要恢复一下分支状态(估计是因为合并分支前pull的,但现在的分支已经被我合并<pull.rebase false>了)

先查看一下最近3次的commit版本

git log -3

选择自己想回退到的commit,执行:

git reset --hard yourcommit

image-20220922125246548

fatal: refusing to merge unrelated histories

哈哈没想到吧,这是第三关!如果你进行了上面的操作,发现自己还是不能成功的git pull origin master,并且报错形如这个小标题,那么可以执行

git pull origin master --allow-unrelated-histories

这个问题可能会被翻译成“拒绝合并不相关的历史”。其实它是说你的分支(或者是两个库)没有建立过关系,在push/pull/merge的时候都可能会出现,但解决都是一个思路->加上--allow-unrelated-histories

退出编辑commit的界面

commit用的是nano文本编辑器,修改完成后按ctrl+x,再敲个大写Y,然后回车就能做到保存退出

切换成vim

什么?不想要这个编辑器?那就git config --global core.editor vim来换成vim吧~


可怕的是这篇文竟然、、<未完待续>!

标签:指南,pull,git,hint,master,push,config
From: https://www.cnblogs.com/bisa/p/16718884.html

相关文章

  • CI持续集成系统环境---部署Gitlab环境完整记录
    ​最近在看CI/CD集成的相关部分,发现几篇好文,转载分享一波。 来源网络:[原创]CI持续集成系统环境---部署Gitlab环境完整记录-散尽浮华-博客园Gitlab是一个代码托管......
  • Chrome 插件开发指南和实践
    看完这篇文章你会学到Chrome插件可以做什么Chrome插件整体架构如何开发Chrome插件(Popup和Devtools)如何使用前端框架(React/Vue)进行开发如何调试插件如何使用Pu......
  • git mv 命令详细操作流程
    gitmv命令移动改名操作如果在工作区进行移动,git会发现一个文件被删除了,另一个文件被创建了。测试:可见,还需要再执行:gitrm,gitadd完成最终的改名操作。直接通过......
  • MVC学习指南(第2版) pdf
    高清扫描版下载链接:https://pan.baidu.com/s/19mTZFEIKPPDshHHU4QzTvg点击这里获取提取码 ......
  • Web安全攻防:渗透测试实战指南 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1YfLeXKh_kaXF1-bwXU28Cw点击这里获取提取码 ......
  • Github发布了Electron 1.0版本
    Github称,Electronapp就像一个微型的网页浏览器一样,具有与本地文件系统交互的功能,网页浏览器已经打包在了app中。这样,应用就可以一次编写,在各个操作系统上运行。Electro......
  • git使用教程
    1git基础1.1初始化git仓库git仓库也就是在git管理下的代码的版本库和一些git配置存放的地方,比如.git文件夹初始化git仓库有两种方式:1、将尚未进行版本控制的本地目录......
  • Git将本地仓库上传到github
    这里采用简单的描述,提供两种方式:一、连接本地文件夹和远程仓库1.使用pull——拉取github项目文件(1)进入到自己要上传的文件夹内部,然后gitbashhere(2)输入命令gitini......
  • 记录几次git相关操作
    1.无法忽略部分文件gitrm-r--cached.解释:-r递归删除所有文件的索引,删除到add.之前的状态这时候再add和commit就忽略到那部分文件了2.无法pull因为本地pyc二进制......
  • 如何使用 Git 管理配置文件
    现在很多软件的配置都可以在线同步或者支持导入导出,可以很方便的在不同设备上使用。但电脑上还有很多本地配置文件没有办法同步,夸多个设备使用时很难保持一致,换电脑也很麻......