使用Visual Studio在git中还原提交后如何重新提交代码

时间:2018-08-20 20:21:06

标签: git visual-studio

我不得不还原一个提交。现在,我修复了代码,我想用我已更改的文件以及已更改的原始文件创建请求请求。但是,只有第一次提交以来的文件才会显示在拉取请求中。我如何告诉git也更新原始文件?

我基于固定分支创建了一个新分支,但是即使在新分支中它们是正确的,它也忽略了属于原始提交的文件。

3 个答案:

答案 0 :(得分:1)

我将从上一次提交开始创建一个新分支,并执行以下操作:

  1. 还原还原,即:还原还原更改时创建的提交。
  2. 介绍新的更改。如果已经有了它们,则可以挑选这些更改。
  3. 现在,创建拉取请求:这将包括还原还原的更改和您所做的新更改。

Sidenote :如果可能的话,我总是更喜欢重置分支而不是还原更改,特别是如果我知道有一天需要重新整合它们。如果要恢复的提交是最后一次提交并且最近已推送,则重置分支是一个好主意,因此希望没人下载它。

答案 1 :(得分:0)

听起来像您可以使用squash来合并您的提交。为此,请看interactive rebase。如果您尚未推送更改,这尤其优雅,因为您基本上可以撤消两次还原提交,以避免在主存储库中造成混乱。

还请记住,您始终可以检出文件上的增量,并仅暂存所需的更改:

git checkout <branch> -- <filename>

答案 2 :(得分:0)

以下是还原提交并在单个“还原”提交中进行更改的过程:

  1. 创建示例提交

    /mnt/c/git/ConsoleApp1 (master)>echo Text > x.txt
    /mnt/c/git/ConsoleApp1 (master)>git add --all
    /mnt/c/git/ConsoleApp1 (master)>git commit -am "A commit"
    [master 07852fa] A commit
     1 file changed, 1 insertion(+)
     create mode 100644 x.txt
    
  2. 创建一个还原提交,以还原示例提交并添加额外的更改

    /mnt/c/git/ConsoleApp1 (master)>git checkout -b feature/with_revert
    Switched to a new branch 'feature/with_revert'
    /mnt/c/git/ConsoleApp1 (feature/with_revert)>git log -n1
    commit 07852fac90b5debcfdd3ea67c282c1fee77e59e6
    Author: Tymtam
    Date:   Tue Aug 21 13:28:21 2018 +1000
    
        A commit
    /mnt/c/git/ConsoleApp1 (feature/with_revert)>git revert --no-commit 07852fac90b5debcfdd3ea67c282c1fee77e59e6
    /mnt/c/git/ConsoleApp1 (feature/with_revert|REVERTING)>git status
    On branch feature/with_revert
    You are currently reverting commit 07852fa.
      (all conflicts fixed: run "git revert --continue")
      (use "git revert --abort" to cancel the revert operation)
    
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            deleted:    x.txt
    
    /mnt/c/git/ConsoleApp1 (feature/with_revert|REVERTING)>echo Text2 > y.txt
    /mnt/c/git/ConsoleApp1 (feature/with_revert|REVERTING)>git add y.txt
    /mnt/c/git/ConsoleApp1 (feature/with_revert|REVERTING)>git status
    On branch feature/with_revert
    You are currently reverting commit 07852fa.
      (all conflicts fixed: run "git revert --continue")
      (use "git revert --abort" to cancel the revert operation)
    
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            deleted:    x.txt
            new file:   y.txt
    
    /mnt/c/git/ConsoleApp1 (feature/with_revert|REVERTING)>git commit
    [feature/with_revert 902e37f] Revert "A commit" + changes
     2 files changed, 1 insertion(+), 1 deletion(-)
     delete mode 100644 x.txt
     create mode 100644 y.txt
    /mnt/c/git/ConsoleApp1 (feature/with_revert)>git log -n2
    commit 902e37f76cabe0e144e091344e8c11ab6dd5213b
    Author: Tymek Majewski <tymek@redbacktech.com>
    Date:   Tue Aug 21 13:29:30 2018 +1000
    
        Revert "A commit" + changes
    
        This reverts commit 07852fac90b5debcfdd3ea67c282c1fee77e59e6.
    
    commit 07852fac90b5debcfdd3ea67c282c1fee77e59e6
    Author: TymTam
    Date:   Tue Aug 21 13:28:21 2018 +1000
    
        A commit