美文网首页
iOS-Git撤销已经推送(push)至远端仓库的提交(comm

iOS-Git撤销已经推送(push)至远端仓库的提交(comm

作者: 下班不写程序 | 来源:发表于2018-08-08 14:31 被阅读207次

在git push之后,发现有些代码需要进行改动,这些改动在原则上不应该作为一次新的提交;或者多人合作开发时,提交的版本有异常,需要回滚到上个版本,这时,我们就需要撤销这次推送(git push)与提交(git commit),然后进行代码修改,再重新进行提交和推送;一般情况下,我们需要三步操作来达到这个目的。

1.撤销提交信息

首先,通过git log查看提交信息,以便获取需要回退至的版本号:

$ git log
commit a44822002522f2a1wdfvgbnj7cec00a7d3d15469 (HEAD -> master, origin/master, origin/HEAD)
Author: lihe5443 <*********@qq.com>
Date:   Sun Mar 4 11:14:55 2018 +0800

    correct_for_bugs

commit aa909cff2239536df14820fe086d96305b24e9f1
Author: heli3445 <*********@qq.com>
Date:   Sat Mar 3 23:43:03 2018 +0800

    简书-代码优化

我们需要撤销correct_for_bugs这次提交,所以需要回退至的版本是简书-代码优化,即需要回退至的版本号是:aa909cff2239536df14820fe086d96305b24e9f1。

然后,通过git reset –soft <版本号>重置至指定版本的提交,达到撤销提交的目的:

$ git reset --soft aa909cff2239536df14820fe086d96305b24e9f1

参数soft指的是:保留当前工作区,以便重新提交,比如我们这次是修改后重新提交 还可以选择参数hard,会撤销相应工作区的修改,一定要谨慎使用

然后,通过git log确认是否成功撤销:

$ git log
commit aa909cff2239536df14820fe086d96305b24e9f1 (HEAD -> master)
Author: heli3445 <*********@qq.com>
Date:   Sat Mar 3 23:43:03 2018 +0800

    简书-代码优化

已经成功撤销。

2.撤销提交的代码

通过git push origin master –force强制提交当前版本号,以达到撤销版本号的目的:

$ git push origin  master --force
Total 0 (delta 0), reused 0 (delta 0)
To github.com:lihe5443/myreflect.git
 + a448220...aa909cf master -> master (forced update)

必须添加参数force进行强制提交,否则会提交失败,并报错

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

报错原因:本地项目版本号低于远端仓库版本号。

3.修改代码,重新提交和推送

//修改代码,添加修改
git add .
//重新提交
git commit -m "correct_for_bugs"
//重新推送
git push origin master

End.

相关文章

  • git常用操作

    Git撤销已经推送(push)至远端仓库的提交(commit)信息 git reset --soft commit...

  • iOS-Git撤销已经推送(push)至远端仓库的提交(comm

    在git push之后,发现有些代码需要进行改动,这些改动在原则上不应该作为一次新的提交;或者多人合作开发时,提交...

  • git恢复文件

    恢复已修改未add的文件git checkout 恢复(撤销)已经提交(commit并push)到远端仓库的com...

  • Git 技巧

    重置已经 push 到服务器的 commit 终端命令:git push 远端branch +comm...

  • git撤销本地的commit

    1、在本地commit之后,Git push到远端分支之前 2、如果不想推送到远端,撤销本地的commit操作 g...

  • ObjectiveGit(二)push & pull

    push 生成一个commit之后,接下来就可以推送到远端了。push之前需要获取到远端仓库信息。由于一个本地仓库...

  • Git 撤销Commit

    撤销未push到远端的commit,但保留该commit的修改 撤销未push到远端的commit,并且丢弃该co...

  • git常用命令

    基本命令 初始化本地仓库 clone仓库: 配置 提交 撤销提交 删除文件 pull代码 push代码 冲突解决 ...

  • GIT撤销已经推送至远端仓库的信息

    有时,在git push之后,才发现还有一些代码需要进行很小的改动,这些改动在原则上不应该作为一次新的提交。这时,...

  • 发布自己的cocoapods插件

    创建本地Git仓库,并提交代码 创建GitHub远端仓库,提交本地代码至GitHub仓库GitHub创建仓库.pn...

网友评论

      本文标题:iOS-Git撤销已经推送(push)至远端仓库的提交(comm

      本文链接:https://www.haomeiwen.com/subject/xunnixtx.html