Revert to the desired commit (Xcode)

This content has 11 years. Please, read this page keeping its age in your mind.

Since the release of Xcode 5.0 git has been improved and promoted. It has its own menu bar and can be easily applied to your projects. You don’t have to develop in a team in order to use it. The use of a version control system is essential in the procedure of developing your projects.

gitXCode-Logo
As we said git has been integrated in Xcode 5 and is easy to use it. However some tasks can’t be completed within the environment of Xcode. In such cases you have to use the command line tools.

One useful task that we are going to explain is to revert in a previous commit. Lets explain the procedure using a simple example. First I think that we have to explain what git revert is. Atlasian’s git tutorial explains:

The git revert Command

The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content. This prevents Git from losing history, which is important for the integrity of your revision history and for reliable collaboration.

git-tutorial_changes-revert

Nice, now that we know what we want to do let’s say that we are working in a project.
Till now we had made 5 commits. If we open a terminal window, we can change to the project’s folder and execute the following command

git log --oneline
which reveals a list of commits displaying the hash code and the description (all that we need).

gitRevert1

After that stage, imagine that we continue the coding doing some changes. Let’s for example delete some UIApplication’s Delegate methods like:

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

from AppDelegate.m and then we commit again. To do that we choose Source Control/Commit… from the menu and the following window will appear. We type an relevant commit message and tap to the commit button.

gitRevert2

Now, if we check the git log we will take as a respond the entire list of commits

gitRevert3

At this point we realise that everything we did in the last commit was wrong and we decide that we want to return to the previous commit. Till the release of Xcode 5.1 you can’t accomplish this via GUI. Don’t panic, it is easy to do it using command line. As you have the terminal open you just have to use the git revert command.

git revert hashCode
other syntax could be
git revert HEAD~3 (not for our example) which make use of the HEAD variable and means: Revert the changes specified by the fourth last commit in HEAD and create a new commit with the reverted changes.
(check this article from gitguys.com if you cann’t understand)

So, in our example if we want to revert the last commit we should type

git revert 09641e3gitRevert4
when you click enter, a vi editor will appear in the terminal window showing the commit message. Just accept the default by typing 😡 which will save and exit the commit message file.

After that, if we execute the git log command again we will discover that a new commit has beed added that revert the project to the desired point.
gitRevert5

However, be careful to that

if you’ve published the work, you probably don’t want to reset the branch, since that’s effectively rewriting history. In that case, you could indeed revert the commits. With git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don’t rewrite any history.

taken from Jefromi

Special credits to Felipe Laso Marsetti (@airjordan12345) for his great articles in “IOS 7 by tutorials” ebook.

One thought on “Revert to the desired commit (Xcode)

Comments are closed.