如何在 Xcode 中恢复以前版本的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14298474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:41:57  来源:igfitidea点击:

How to restore previous version of code in Xcode

xcodecommitrestore

提问by Victor Engel

I'm working on an Objective C project in Xcode and need to restore to a previous version that was committed in source control. How do I do that? I can find information on how to commit and push but no information on how to restore to an old version.

我正在 Xcode 中处理一个 Objective C 项目,需要恢复到在源代码管理中提交的先前版本。我怎么做?我可以找到关于如何提交和推送的信息,但没有关于如何恢复到旧版本的信息。

回答by mlstudent

You cannot revert the entire project at once in Xcode, but you could do it from the terminal; just cd into the folder of your project and type git log to find the hash you're looking for, then do git checkout [hash].

您无法在 Xcode 中一次还原整个项目,但可以从终端进行;只需 cd 进入您的项目文件夹并键入 git log 以查找您要查找的哈希,然后执行 git checkout [ hash]。

回答by Hackless

Quote from Apple's documentation at https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/Save_and_Revert_Changes_to_Files/ManageChanges.html

引自 Apple 的文档https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/Save_and_Revert_Changes_to_Files/ManageChanges.html

"Compare File Versions to Revert Lines of Code Choose View > Version Editor > Show Comparison View to compare versions of files saved in a repository. Use the jump bars to choose file versions based on their position within a repository. Each jump bar controls the selection for the content pane above it. To display a version, browse through the hierarchy to find it, then click to choose it. Shaded areas indicate changes between versions." "You can use the version timeline to choose file versions based on their chronological order. Click the timeline viewer icon (../art/TimeLineIcon_2x.png) in the center column to display the timeline between the two editing panes. Move the pointer up or down through the timeline to browse the available versions. When you find the version you want, click the left or right indicator triangle to display that version in the corresponding editor pane. You can edit the current working copy of the file in the version editor. If you want to revert changes between versions, you can copy code from an older version and paste it into the current version."

“比较文件版本以还原代码行 选择“视图”>“版本编辑器”>“显示比较视图”以比较存储库中保存的文件的版本。使用跳转条根据它们在存储库中的位置选择文件版本。每个跳转条控制选择用于其上方的内容窗格。要显示版本,请浏览层次结构以找到它,然后单击以选择它。阴影区域表示版本之间的变化。"您可以使用版本时间线根据时间顺序选择文件版本。单击中心列中的时间线查看器图标 (../art/TimeLineIcon_2x.png) 以显示两个编辑窗格之间的时间线。向上移动指针或通过时间线向下浏览可用版本。当您找到所需的版本时,单击左或右指示器三角形以在相应的编辑器窗格中显示该版本。您可以在版本编辑器中编辑文件的当前工作副本。如果您想恢复版本之间的更改,您可以从旧版本复制代码并将其粘贴到当前版本中。”

The last line reveals the key: copy-and-paste.

最后一行揭示了关键:复制和粘贴。

回答by Andy

In Xcode 9 you can checkout any previous version.

在 Xcode 9 中,您可以查看任何以前的版本。

  1. Click on the source control icon in the navigator section on the left, or go to view->navigators->Show source control navigator.
  2. A list of all commits for the selected branch will be shown.
  3. Right click on the desired commit and select 'Checkout "commitID"'
  1. 单击左侧导航器部分中的源代码管理图标,或转到查看->导航器->显示源代码管理导航器。
  2. 将显示所选分支的所有提交的列表。
  3. 右键单击所需的提交并选择“结帐“commitID””

You can also directly create a branch from there which might be even more useful in some cases

您也可以直接从那里创建一个分支,这在某些情况下可能更有用

回答by MiladiuM

here is a visual method in Xcode 8

这是一个视觉方法 Xcode 8

enter image description here

在此处输入图片说明

回答by Christian Krueger

If you want to get an entire project extract of any committed version on a separate folder, try this shell script:

如果您想在单独的文件夹中获取任何已提交版本的整个项目提取,请尝试以下 shell 脚本:

#!/bin/sh
#
#  restores an old version of xcodeproject from git repository
#  into folder  $HOME/gitcopy
#  current directory must be the rootdir of the project
#  Xcode should not access the same project at this time
#
set -x
PROJECT_ROOT=.
COPY_DEST=$HOME/gitcopy
if [ -e *.xcodeproj ]
then
    git log
    echo "Which version ?"
    read x
    git checkout $x
    git checkout -b copyVersion
    cp -R $PROJECT_ROOT $COPY_DEST
    git stash
    git checkout master;
else
    echo "No xcode project root folder";
fi 

回答by Christian Krueger

I am working on Xcode 9.2 project which has several dependencies and additional file that have been incorporated into it. In the course of cleaning it up, I inadvertently trashed the project and could not figure out what to do to get it to run until I found this solution. I did go to source control on the top menu bar and discarded all changes first. Then I went to the Source Control Manager (second from the left just to the right of the Project Navigator and highlighted the commit I wanted then right clicked and chose the Checkout option which had already entered the number of the commit I wanted and it worked perfectly. Step 1 - Discard all changes Step 2 - Select Commit you want to go back to. Step 3 - Right click and select Checkout

我正在开发 Xcode 9.2 项目,该项目具有多个依赖项和已合并到其中的附加文件。在清理它的过程中,我不经意间把项目搞砸了,在找到这个解决方案之前,我不知道该怎么做才能让它运行。我确实转到顶部菜单栏上的源代码管理并首先放弃了所有更改。然后我转到源代码控制管理器(项目导航器右侧的从左数第二个并突出显示我想要的提交,然后右键单击并选择 Checkout 选项,该选项已经输入了我想要的提交编号,它运行良好. Step 1 - Discard all changes Step 2 - 选择Commit you want to go back to Step 3 - 右键单击​​并选择Checkout