In Xcode, how do I switch branches, merge changes, and choose which changes to merge?

Viewed 2377

I'm quite new to Xcode, and I wanted to figure out version control before I added a change that could possibly screw up my project. I'm working on this project alone and have a Git repository saved on my mac, so I don't think I need to link it with a GitHub account. I right-clicked, selected "branch from master", and called it morePictures because of the changes I wanted to make. Now all of the code I have in 'ViewController.swift' has moved to morePictures and when I look at it it shows the master having the none of my code in it with morePictures having all of it. It shows the changes and I'm pretty sure that I have to select to keep or discard certain changes but I don't know how.

In summary, I just want to be able to keep my working code on a master branch, switch between branches, and be able to choose changes to keep and discard when I merge the two.

Here are some pictures of what it looks like now:

master branch

morePictures branch

1 Answers

I would recommend doing it in the terminal, but if you never used git in the terminal it might be better using Xcode's UI.

#1 Terminal

First make sure, that all of your changes are committed. Then you have to go to your project folders. cd Your/Project/Directory or write cd and drag and drop your you project folder.
Then you have to check on which branch you are on.

git status

If you are already on branch master everything is fine, else you have to change your branch to master:

git checkout master

Now you can merge your branches:

git merge morePictures

That's it!

#2 Xcode

If you feel more comfortable using Xcode's UI, you can do it like this:

First make sure, that all of your changes are committed, then you can right-click the master branch and select Checkout...

Image Checkout Now you have to confirm the checkout.

You current Branch is now 'master'.

Next you can right-click your second branch and select Merge ... into ...

Image Merge

Confirm that again and you branch master should be up to data!

Related