Git怎样撤销一次分支的合并Merge

Git怎样撤销一次分支的合并Merge,第1张

由于太多人问怎么撤销 merge 了,于是 git 官方出了这份教程,表示在 git 现有的思想体系下怎么达到撤销 merge 的目标。

方法一,reset 到 merge 前的版本,然后再重做接下来的 *** 作,要求每个合作者都晓得怎么将本地的 HEAD 都回滚回去:

$ git checkout 行merge *** 作时所在的分支

$ git reset --hard merge前的版本号

方法二,当 merge 以后还有别的 *** 作和改动时,git 正好也有办法能撤销 merge,用 git revert:

$ git revert -m 要撤销的那条merge线的编号,从1开始计算(怎么看哪条线是几啊?) merge前的版本号

Finished one revert

[master 88edd6d] Revert "Merge branch 'jk/post-checkout'"

1 files changed, 0 insertions(+), 2 deletions(-)

这样会创建新的 mit 来抵消对应的 merge *** 作,而且以后 git merge 那个编号所代表的分支 会提示:

Already up-to-date

因为使用方法二会让 git 误以为这个分支的东西都是咱们不想要的。

方法三,怎么撤销方法二:

$ git revert 方法二撤销merge时提交的mit的版本号,这里是88edd6d

Finished one revert

[master 268e243] Revert "Revert "Merge branch 'jk/post-checkout'""

1 files changed, 2 insertions(+), 0 deletions(-)

这样就行了,可以正常 merge 了,不过可能会有很多冲突噢!!

$ git merge jk/post-checkout

Auto-merging testtxt

Merge made by recursive

testtxt | 1 +

1 files changed, 1 insertions(+), 0 deletions(-)

最后的最后,还是觉得顶上那些都麻烦爆了。

前端天天用 WebStorm 写代码的看过来: WebStorm 右键点击项目文件或文件夹,有一个:

Local History -> Show History

点开后出现一个窗口,可以看到所有的本地改动。

找到合适的那份,按左上角那个:

Revert

按钮,也就是个紫色的弯曲的小箭头就回去了。

在文件量不大的时候十分管用,强烈推荐

不同的状态,要采用不同的撤销方式, 如果你仅仅在工作区修改,还没有使用git add暂存。可以直接使用git checkout 撤销你在工作区的任何修改 如果已经暂存了,就要使用git reset *** 作了,git会有文本的形式提示你,是前进,还是后退。 如果已经

git撤销pull命令的方法和 *** 作步骤如下:

1、首先,输入1,然后按Enter键进入项目的git存储库所在的文件夹,如下图所示。

2、其次,完成以上步骤后,单击以输入命令代码“ git reflog”,然后按Enter键以执行命令,如下图所示。

3、接着,完成以上步骤后,找到需要回滚的版本并执行以下命令:git reset --hard HEAD @ {n},按Enter键确认,如下图所示。

4、然后,完成以上步骤后,选择要返回的版本,输入命令,例如:git reset --hard 61a942c,按Enter键确认,如下图所示。

5、最后,完成以上步骤后,所有步骤就都完成了,问题解决,如下图所示。

如果不小心commit了一个不需要commit的文件,可以对其进行撤销。
 
先使用git log 查看 commit日志

commit 422bc088a7d6c5429f1d0760d008d86c505f4abe  
Author: zhyq0826 <zhyq0826@gmailcom>  
Date:   Tue Sep 4 18:19:23 2012 +0800  
  
    删除最近搜索数目限制  
  
commit 8da0fd772c3acabd6e21e85287bdcfcfe8e74c85  
Merge: 461ac36 0283074  
Author: zhyq0826 <zhyq0826@gmailcom>  
Date:   Tue Sep 4 18:16:09 2012 +0800
找到需要回退的那次commit的 哈希值,
 
git reset --hard commit_id 
 
 使用上面的命令进行回退
 
以下是豆瓣的
 
苍炎的日记
 
起因: 不小新把记录了公司服务器IP,账号,密码的文件提交到了git
方法:
    git reset --hard <commit_id>
    git push origin HEAD --force
其他:
    根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:
    git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
    git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
    git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容
    HEAD 最近一个提交
    HEAD^ 上一次
    <commit_id>  每次commit的SHA1值 可以用git log 看到,也可以在页面上commit标签页里找到

如果不小心commit了一个不需要commit的文件,可以对其进行撤销。
 
先使用git log 查看 commit日志

commit 422bc088a7d6c5429f1d0760d008d86c505f4abe  
Author: zhyq0826 <zhyq0826@gmailcom>  
Date:   Tue Sep 4 18:19:23 2012 +0800  
  
    删除最近搜索数目限制  
  
commit 8da0fd772c3acabd6e21e85287bdcfcfe8e74c85  
Merge: 461ac36 0283074  
Author: zhyq0826 <zhyq0826@gmailcom>  
Date:   Tue Sep 4 18:16:09 2012 +0800
找到需要回退的那次commit的 哈希值,
 
git reset --hard commit_id 
 
 使用上面的命令进行回退
 
以下是豆瓣的
 
苍炎的日记
 
起因: 不小新把记录了公司服务器IP,账号,密码的文件提交到了git
方法:
    git reset --hard <commit_id>
    git push origin HEAD --force
其他:
    根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:
    git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
    git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
    git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容
    HEAD 最近一个提交
    HEAD^ 上一次
    <commit_id>  每次commit的SHA1值 可以用git log 看到,也可以在页面上commit标签页里找到

在eclipse里用git,撤销修改的说明如下:

Reverting changes in the working tree

Replace with File in Git Index

Changes which are not yet committed and not yet staged can be reverted for a
set of selected files Select the file(s) in the Package Explorer or an
analogous view and click Replace With > File in Git
Index

Replace with HEAD

This feature is currently not available on single file level You can use
Reset to with option hard to forcefully reset
the entire working tree of your repository back to the state of the HEAD commit
(See "Resetting your current HEAD" below) This operation will revert all
changes in the working tree and the index You can't do it on a selected set of
files using EGit yet

Replace with Previous Revision

Changes that are already staged or even committed can be "reverted" by
replacing them with a version from the previous commit Select a single resource
in the Package Explorer or an analogous view and click Replace With >
Previous Revision The repository will determine the last commit that
modified the selected resource and offer to replace the workspace resource with
the contents of this commit

This is mainly intended for "removing" single files from a commit (when
committing the reverted workspace resources, they are effectively removed from
the current commit) Even though this also works on folders and projects, the
results of replacing a folder or project with a "previous revision" may be
unexpected

Revert using quickdiff

The quickddiff feature can be used to revert individual changes to a file
You can revert by line, block (se range of changes lines) or selection Select
all text and then Revert selection to revert a whole file

Reverting changes introduced by a specific commit

Changes which are introduced by a given commit can be reverted by an
automatically created new commit on top of the currently checked out commit The
commit which is to be reverted does not have to be checked out for that

Select the commit in the History View, open the context menu and select
Revert Commit This reverts the changes that the selected
commit introduces by creating a new commit on top of the currently checked out
commit

Resetting your current HEAD

Git offers the possibility to reset the HEAD of the current branch to any
other commit It optionally resets the index and the working tree to match that
commit Note that this action affects all files and folders in the entire
repository

You have the option to do a hard reset, a mixed reset and a soft reset

soft -  the HEAD points now to the new commit, the index
and the working tree are unchanged

mixed -  the HEAD points now to the new commit, the index
is updated, the working tree is unchanged

hard - the HEAD points now to the new commit, the index and
the working tree are updated

Reset to specific branch or tag

Select Team -> Reset on a project This opens a dialog
where you can select a branch or a tag

Reset to a specific commit

Select a commit in the History view and open the context menu  Here you find
the entries Hard reset, Mixed reset and
Soft reset

Revert all local and staged changes

This can be done as a special case of reset If you reset to the current HEAD
(normally the last commit on your branch) with the option hard
you overwrite the working tree and the index with the content of the HEAD You
can do this in three ways:

Select Team > Reset on a project In the dialog
select HEAD or your current branch and switch the radio button to
hard

Right click and select Reset on any branch or tag in the
Repositories view This opens a dialog which lets you decide on the reset type
Choose hard here

Open the context menu on the HEAD commit in the history view and select
Hard Reset

撤销一个“已公开”的改变
场景:你已经执行了
git
push
,
把你的修改发送到了
GitHub,现在你意识到这些
commit
的其中一个是有问题的,你需要撤销那一个
commit
方法:
git
revert
<SHA>
原理:
git
revert
会产生一个新的
commit,它和指定
SHA
对应的
commit
是相反的(或者说是反转的)。如果原先的
commit
是“物质”,新的
commit
就是“反物质”

任何从原先的
commit
里删除的内容会在新的
commit
里被加回去,任何在原先的
commit
里加入的内容会在新的
commit
里被删除。
这是
Git
最安全、最基本的撤销场景,因为它并不会
改变
历史

所以你现在可以
git
push
新的“反转”
commit
来抵消你错误提交的
commit。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13384692.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-25
下一篇 2023-07-25

发表评论

登录后才能评论

评论列表(0条)

保存