GitHub错误commit回滚失败

    xiaoxiao2022-07-12  184

    先发三个reset参考文章:

    1.https://www.cnblogs.com/jenkin1991/p/9604054.html

    2.https://www.imooc.com/video/17968/0

    3.https://blog.csdn.net/yxlshk/article/details/79944535

    指针参考文章

    4。https://segmentfault.com/a/1190000004071116

    5.https://ask.csdn.net/questions/192942?sor

     

    第一次在GitHub上commit失败,想撤销,但是又撤销不掉。使用的是Sourcetree进行撤销,报错,命令行撤销,依旧报错。最后参考三篇文章得以解决。

     

    思路是:

    方法一:

    1.git config --global user.name "yourname",git config --global user.email myemail@qq.com 设置全局用户名和邮箱,

    2.ssh-keygen -t rsa -C "这里换上你的邮箱",生成秘钥,

    3.打开.ssh/目录查看密钥,并粘贴到GitHub上

    4.测试 ssh -T git@github.com  如果提示: You've successfully authenticated, but GitHub does not provide shell access. 就可以尽情的Reset了(git reset --hard 目标版本号)

    5.reset之后一定要使用强制推送,否则依旧报错git push -f

     

    方法二:

    1.2.3.4完成后,使用指针

    例如现在版本库为: commit4 commit3 commit2 commit1 我想删除commit2和commit3,变为: commit4 commit1 请问如何办到? 本地版本库

    答案:先移动HEAD指针 git reset --soft HEAD~3 完了之后直接 git commit

    --hard 会直接把暂存区和工作区的内容都更新至commit1, 要用--soft,只移动HEAD指针,工作区和暂存区还都是commit4版本的,这时候直接commit,就ok了

    然后依旧git push -f

     

     

     

     

     

     

     

    下面是转载的

    转载1:

    楼主下载git闲置了一段时间,某天心血来潮突然想也写一个项目,就打算用git来管理代码。

    当楼主在github上建好仓库以后,就准备开始clone,当楼主去找/Users/***/.ssh/id_rsa.pub发现没有(cd ~/.ssh这里可以先简单的检测下,没有就重新生成),

    然后就百度了一下,用ssh-keygen -t rsa -C “你的邮箱”这个命令直接生成,之后在/Users/***/.ssh/目录下就生成了两个文件,

    到这里的时候,楼主以为就可以搞定了,然后就开始去clone,

    直接复制/Users/***/.ssh/id_rsa.pub的内容,然后在github的SSH and GPG keys新建了一个,填上title粘贴上内容,然后回到终端git clone “你的地址”

    结果就报了这个

    Cloning into 'xxxx'... git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

    Please make sure you have the correct access rights and the repository exists.

    然后楼主各种百度,发现解决这个问题的办法,就是复制/Users/***/.ssh/id_rsa.pub的内容到github上新建的keys上啊,为什么就不行呢?

    。。。

    。。。。

    。。。。。

    各种百度,各种查,真的是一万个草泥马啊。。。。。

    于是楼主就决定开始重头再来,把刚才生成的/Users/***/.ssh/*,全部删除掉,

    第一步,先验证一下用户名邮箱,git config --global --list,楼主之前就放了一个错误,没有验证就直接复制内容去粘贴了,一般第一次安装都是没有的。

    第二步,git config --global user.name "yourname",git config --global user.email myemail@qq.com 设置全局用户名和邮箱,

    第三步,ssh-keygen -t rsa -C "这里换上你的邮箱",生成秘钥,

    接着以为这次总算可以OK了,然后又复制了一遍之前的操作,接着去clone,

     

    啃爹啊,结果还是报

     

    Cloning into 'xxxx'... git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

    Please make sure you have the correct access rights and the repository exists.

     

    心碎有没有,楼主感觉自己都快奔溃了,到底是为什么呢?

    心里一直反复的说要镇定要镇定,然后又是各种百度,最后终于在一位老哥的博客上找到答案了尼玛的太啃爹了

    这里提供一下地址https://blog.csdn.net/chaoyueziji123/article/details/54669555

    首先测试一下

    ssh git@github.com Warning: Permanently added the RSA host key for IP address '13.250.000.000' to the list of known hosts. PTY allocation request failed on channel 0 Hi ******! You've successfully authenticated, but GitHub does not provide shell access.

    结果是这样的,大概意思就是这个IP地址RSA主机密钥永久的添加到了你的主机列表中,您已经成功地进行了身份验证,但GITHUB不提供shell访问。

    解决办法:

    1.ssh -v git@github.com

    测试ssh连接是否成功,我的现实如下,跟老哥的不太一样,可以连起来看

     

    OpenSSH_7.6p1, LibreSSL 2.6.2 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 48: Applying options for * debug1: Connecting to github.com port 22. debug1: Connection established. debug1: identity file /Users/faye/.ssh/id_rsa type 0 debug1: key_load_public: No such file or directory debug1: identity file /Users/faye/.ssh/id_rsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/faye/.ssh/id_dsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/faye/.ssh/id_dsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/faye/.ssh/id_ecdsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/faye/.ssh/id_ecdsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/faye/.ssh/id_ed25519 type -1 debug1: key_load_public: No such file or directory debug1: identity file /Users/faye/.ssh/id_ed25519-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_7.6 debug1: Remote protocol version 2.0, remote software version libssh_0.7.0 debug1: no match: libssh_0.7.0 debug1: Authenticating to github.com:22 as 'git' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256@libssh.org debug1: kex: host key algorithm: ssh-rsa debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8 debug1: Host 'github.com' is known and matches the RSA host key. debug1: Found key in /Users/faye/.ssh/known_hosts:1 debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey after 134217728 blocks debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering public key: RSA SHA256:7ym7jzOz5gT7p8h1Xw4f8a7Jq/0a4yjcyWMRO1BVyL0 /Users/faye/.ssh/id_rsa debug1: Server accepts key: pkalg ssh-rsa blen 279 debug1: Authentication succeeded (publickey). Authenticated to github.com ([13.229.188.59]:22). debug1: channel 0: new [client-session] debug1: Entering interactive session. debug1: pledge: network debug1: Sending environment. debug1: Sending env LANG = zh_CN.UTF-8 debug1: Sending env LC_CTYPE = zh_CN.UTF-8 PTY allocation request failed on channel 0 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 Hi ! You've successfully authenticated, but GitHub does not provide shell access. debug1: channel 0: free: client-session, nchannels 1 Connection to github.com closed. Transferred: sent 3020, received 1788 bytes, in 0.8 seconds Bytes per second: sent 3684.0, received 2181.1 debug1: Exit status 1

    然后下面两步就关键了,

    先使用 ssh-agent -s

    SSH_AUTH_SOCK=/var/folders/c6/_lb54v1d4v14_0j4_nt_1vj00000gp/T//ssh-B57Gx7MencHN/agent.32676; export SSH_AUTH_SOCK; SSH_AGENT_PID=32677; export SSH_AGENT_PID; echo Agent pid 32677;

     

    再使用 ssh-add ~/.ssh/id_rsa

    Identity added: /Users/faye/.ssh/id_rsa (/Users/faye/.ssh/id_rsa)

     

    最后测试 ssh -T git@github.com

     

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

    当显示这一句的时候,就万事大吉了,接着就可以愉快的去clone了,

     

    Cloning into '*****'... remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done.  

    到这里楼主想说,git这个东西还是很值得去研究的,但是楼主比较懒散,一般只会搞明白怎么去使用就好了

    所以以楼主的经验就是一步一步搞

    第一步,git config --global --list 验证邮箱

    第二步,git config --global user.name "yourname",git config --global user.email myemail@qq.com 设置全局用户名和邮箱,

    第三步,ssh-keygen -t rsa -C "这里换上你的邮箱",生成秘钥,

    第四步,到git仓库,添加秘钥,

    第五部,ssh -T git@github.com 测试一下通不通,不通就是ssh-agent -s  ssh-add ~/.ssh/id_rsa 操作这两步

    最后clone,药到病除!!!!

     

     

     

     

     

     

     

    转载2:

    一、问题描述

        在利用github实现多人合作程序开发的过程中,我们有时会出现错误提交的情况,此时我们希望能撤销提交操作,让程序回到提交前的样子,本文总结了两种解决方法:回退(reset)、反做(revert)。

    二、背景知识

        git的版本管理,及HEAD的理解

        使用git的每次提交,Git都会自动把它们串成一条时间线,这条时间线就是一个分支。如果没有新建分支,那么只有一条时间线,即只有一个分支,在Git里,这个分支叫主分支,即master分支。有一个HEAD指针指向当前分支(只有一个分支的情况下会指向master,而master是指向最新提交)。每个版本都会有自己的版本信息,如特有的版本号、版本名等。如下图,假设只有一个分支:

     

    三、解决方法

    方法一:git reset

    原理: git reset的作用是修改HEAD的位置,即将HEAD指向的位置改变为之前存在的某个版本,如下图所示,假设我们要回退到版本一:

     

    适用场景: 如果想恢复到之前某个提交的版本,且那个版本之后提交的版本我们都不要了,就可以用这种方法。

    具体操作:

    1. 查看版本号: 可以使用命令“git log”查看:

    也可以在github网站上查看:

    2. 使用“git reset --hard 目标版本号”命令将版本回退: 

    再用“git log”查看版本信息,此时本地的HEAD已经指向之前的版本: 

    3. 使用“git push -f”提交更改: 此时如果用“git push”会报错,因为我们本地库HEAD指向的版本比远程库的要旧: 

     所以我们要用“git push -f”强制推上去,就可以了:

    在github图形化界面上看,远程库的HEAD也已经指向目标版本: 

    回退成功!

    方法二:git revert

    原理: git revert的作用通过反做创建一个新的版本,这个版本的内容与我们要回退到的目标版本一样,但是HEAD指针是指向这个新生成的版本,而不是目标版本。如下图所示,假设我们要回退到版本一,版本四的内容和版本一是一样的:

    适用场景: 如果我们想恢复之前的某一版本(该版本不是merge类型),但是又想保留该目标版本后面的版本,记录下这整个版本变动流程,就可以用这种方法。

    具体操作:

    1. 查看版本号: 可以通过命令行查看:

     

    也可以通过github网站图形化界面查看: 

     

    2.使用“git revert -n 版本号”反做,并使用“git commit -m 版本名”提交: (1)反做,使用“git revert -n 版本号”:  

    注意: 这里可能会出现冲突,那么需要手动修改冲突的文件。而且要git add 文件名。 (2)提交,使用“git commit -m 版本名”: 

    此时可以用“git log”查看本地的版本信息,可见多生成了一个新的版本: 

    3.使用“git push”推上远程库: 

    查看github上显示的远程库版本信息: 

    反做成功! 

    最新回复(0)