首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在git中回滚上一次提交中文件中的更改

问如何在git中回滚上一次提交中文件中的更改
EN

Stack Overflow用户
提问于 2009-08-29 00:16:35
回答 4查看 2.3K关注 0票数 1

我做了3个'git承诺‘,但我没有做'git推动’。

代码语言:javascript
复制
1. commit 1
2. commit 2
   touches fileA
   touches fileB
   touches fileC
3. commit 3

所以我怎么能

  1. 回滚我在提交2的b文件中所做的更改?(我不能再做'git结账‘了-- fileB,因为我已经'git提交’了,我如何回滚我的更改呢?
  2. 在fileC中进行更改,并将其作为提交2的一部分?我想我可以不时地修改文件,然后运行一个'git重基-i HEAD~2‘,对吗?
EN

回答 4

Stack Overflow用户

发布于 2009-08-29 00:49:24

这应该是可行的:

代码语言:javascript
复制
1. git rebase -i HEAD~2
2. in your editor, select the following:

edit 9b86592 commit 2
pick f3907cb commit 3

3. at this point roll back the changes you made in fileB, for example with
   `git checkout <version_you_want>` or by manually editing the file
4. make the changes in fileC you want to be part of commit 2
5. `git add fileB fileC`
6. `git commit --amend`
7. `git rebase --continue`

当git尝试应用提交3时,可能需要解决合并问题。在解决这些问题后,再次运行git rebase --continue。

票数 5
EN

Stack Overflow用户

发布于 2009-08-29 00:27:46

使用git rebase -i HEAD~2并编辑第二次提交。

票数 2
EN

Stack Overflow用户

发布于 2009-08-29 07:30:42

假设你在树枝上,拥有一棵干净的树:

代码语言:javascript
复制
# checkout incorrect commit
git checkout <sha1_of_commit2>

# revert the changes to fileB by checking out the parent version
git checkout HEAD^ -- fileB

# make an amended commit
git commit --amend

# go back to master
git checkout master

# transplant the changes since the bad commit onto the amended commit
git rebase --onto HEAD@{1} <sha1_of_commit2>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1349998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档