如何在不丢失更改的情况下取消提交上次未按下的git提交

时间:2013-11-08 12:47:23

标签: git

有没有办法恢复提交,以便我的本地副本保持在该提交中所做的更改,但它们在我的工作副本中变为非提交的更改?回滚提交会将您带到上一次提交 - 我希望保留所做的更改,但我将它们提交到了错误的分支。

这还没有推,只是提交了。

7 个答案:

答案 0 :(得分:679)

有很多方法可以做到这一点,例如:

如果你 公开推送提交:

git reset HEAD~1 --soft   

就是这样,您的提交更改将在您的工作目录中,而LAST提交将从您当前的分支中删除。见git reset man


如果您 公开推送(在名为'master'的分支上):

git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )

正常恢复提交并按

git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this intorduces a new coomit (say, it's hash is 86b48ba) which removes changes, introduced in the coomit in question (but those changes are still visible in the history)
git push origin master

现在,如果您希望在工作副本中进行本地更改(“以便本地副本保留在该提交中进行的更改”)时进行这些更改 - 只需使用-no-commit选项恢复还原提交:< / p>

git revert --no-commit 86b48ba (hash of the revert commit).

我制作了一个小例子:https://github.com/Isantipov/git-revert/commits/master

答案 1 :(得分:10)

如果你推动了更改,你可以undo并将文件移回舞台而不使用其他分支。

git show HEAD > patch
git revert HEAD
git apply patch

它将创建一个包含最后一个分支更改的补丁文件。然后它还原更改。最后,将补丁文件应用到工作树中。

答案 2 :(得分:3)

2020简单方法:

git reset <commit_hash>

提交要保留的最后一次提交的哈希。

答案 3 :(得分:1)

大多数情况下,这发生在我将更改推送到错误的分支并稍后实现时。在大多数情况下,后续工作都是如此。

git revert commit-hash
git push

git checkout my-other-branch
git revert revert-commit-hash
git push
  1. 还原提交
  2. (创建并)签出其他分支
  3. 还原还原

答案 4 :(得分:0)

  

在运行这些命令之前,请确保备份您的更改,并将其保存在单独的文件夹中

git checkout branch_name

  

在您的分支结帐

git merge --abort

  

中止合并

git状态

  

中止合并后检查代码的状态

git reset --hard origin / branch_name

  

这些命令将重置您的更改并使代码与branch_name(分支)代码对齐。

答案 5 :(得分:0)

添加我遵循的步骤,希望对我这样的初学者有所帮助。

下图显示了我已经推送到 bitbucket 中远程分支“A”的提交。 enter image description here

从这 5 次提交中,我想保留最后 2 次提交,但前 3 次提交我希望它们推送到另一个分支“B”。

这些是我遵循的步骤:

内部分支“A”:

  1. import * as React from 'react'; import { Text, View, TextInput } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs'; import { MaterialCommunityIcons } from '@expo/vector-icons'; function HomeScreen() { const [text, onChangeText] = React.useState("Useless Text"); return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Home!</Text> <TextInput onChangeText={onChangeText} value={text} /> </View> ); } function SettingsScreen() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Settings!</Text> </View> ); } const Tab = createMaterialTopTabNavigator(); export default function App() { return ( <NavigationContainer> <Tab.Navigator tabBarPosition='bottom' tabBarOptions={{ // style: { position: 'absolute', bottom:0 }, activeTintColor: '#e91e63', inactiveTintColor: '#ee11ff', showIcon: true, indicatorStyle:{ height:0 } }}> <Tab.Screen options={{ tabBarLabel: 'Home', tabBarIcon: ({ color, size }) => ( <MaterialCommunityIcons name="home" color={color} size={24} /> ), }} name="Home" component={HomeScreen} /> <Tab.Screen options={{ tabBarLabel: 'Profile', tabBarIcon: ({ color, size }) => ( <MaterialCommunityIcons name="account" color={color} size={24} /> ), }} name="Settings" component={SettingsScreen} /> </Tab.Navigator> </NavigationContainer> ); } 用于 3 个提交中的每一个。例如,d4a3734 是图片中最后一次提交的提交哈希。 (如果您愿意,可以一次还原多个提交 - 请参阅 How to revert multiple git commits?
  2. git revert <commit-hash>

推送之后是这样的:-

enter image description here

现在,我的分支“A”中只有前 2 个提交,这正是我想要的。接下来,结帐到想要的分行。如果是新分支,请使用 git push。就我而言,我做了 git checkout -b <branchname>

内部分支“B”:

我只是挑选了我想要分支“B”的提交。就我而言,我做到了:

git checkout B

对于我还原的那 3 个提交。

(同样,作为例子,git cherry-pick <commit-hash> 其中 d4a3734 是图片中最后一次提交的提交哈希)

答案 6 :(得分:-1)

对于这种情况:“尚未推送,仅提交。” - 如果您使用 IntelliJ (或其他JetBrains IDE)和尚未推送更改,则可以执行下一步操作。

  1. 转到版本控制窗口( Alt + 9 ) - “日志”标签。
  2. 在最后一次提交之前右键单击提交。
  3. 将当前分支重置为此处
  4. 选择 Soft (!!!)
  5. 按下对话框窗口底部的Reset按钮。
  6. 完成。

    这将“取消提交”您的更改,并将您的git状态返回到上次本地提交之前的位置。你不会丢失任何改变。

相关问题