问题
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.
回答1:
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].
回答2:
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
"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."
The last line reveals the key: copy-and-paste.
回答3:
In Xcode 9 you can checkout any previous version.
- Click on the source control icon in the navigator section on the left, or go to view->navigators->Show source control navigator.
- A list of all commits for the selected branch will be shown.
- Right click on the desired commit and select 'Checkout "commitID"'
You can also directly create a branch from there which might be even more useful in some cases
回答4:
here is a visual method in Xcode 8
回答5:
If you want to get an entire project extract of any committed version on a separate folder, try this shell script:
#!/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
回答6:
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
来源:https://stackoverflow.com/questions/14298474/how-to-restore-previous-version-of-code-in-xcode