1

I'm using Xcode 9 and in my project I started to work on a completely different feature but unfortunately I've forgotten to create a new branch before doing that. So, now I have a lot of changes which don't refer to an old branch, so I create a new one, but I can't change my current branch without committing them.

Is there some way to change the branch, while having uncommitted changes, or I don't have other choice but to commit them to the current branch?

jthill
  • 55,082
  • 5
  • 77
  • 137
Tigran Iskandaryan
  • 1,371
  • 2
  • 14
  • 41
  • Is it xcode preventing this? Because you should be able to create a new branch and checkout to it from the command line. – juanchopanza Feb 10 '18 at 22:11
  • @juanchopanza, yes. When I try to checkout a new branch, it says that there are uncommitted changes in the project and suggests to commit or to discard them – Tigran Iskandaryan Feb 10 '18 at 22:59

1 Answers1

3

Don't know Xcode 9, but from the command line, you can use the "git stash" command.

"git stash" allows you to store your changes and re-apply them latter. This way you can store them and remove them from the current branch, switch the branch and then re-apply them on the new branch:

git stash
git checkout new_branch
git stash apply
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
  • 1
    Is this even necessary? It should suffice to create a new branch and checkout to it. – juanchopanza Feb 10 '18 at 22:10
  • 1
    It never happened to me yet, but it appears that in some specific cases you can directly checkout to the other branch (in which case git won't complain of uncommitted changes): see this [answer](https://stackoverflow.com/questions/22053757/checkout-another-branch-when-there-are-uncommitted-changes-on-the-current-branch) – Xavier Guihot Feb 10 '18 at 22:25
  • Yes, if git complains that there are uncommitted changes then you have to stash. – Xavier Guihot Feb 10 '18 at 23:04
  • Ok, could you just explain steps a little bit deeper, because I've never done this before? First, I should change to the same directory as my project and after that call commands from your answer, right? – Tigran Iskandaryan Feb 10 '18 at 23:07
  • 1
    You should go to the folder of your project in your terminal. You can then type "git status" just to have an idea of which files you modified. Then "git stash", which "keeps somewhere" your changes and removes them from the branch. You can see they disappeared when doing "git status". Then change branch with "git checkout existing_branch" or "git checkout -b new_branch". And finally apply the changes to your new branch with "git stash apply". If you're not sure of what you're doing, you can probably backup your project before playing with git ;p – Xavier Guihot Feb 10 '18 at 23:18
  • @XavierGuihot, thank you for making it clear. I'll definitely backup it :) – Tigran Iskandaryan Feb 10 '18 at 23:27
  • After doing so, I couldn't open my project. Xcode was throwing an error that the file couldn't be parsed. Luckily, I had done a backup of the project before doing this – Tigran Iskandaryan Feb 10 '18 at 23:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164901/discussion-between-xavier-guihot-and-tigran-iskandaryan). – Xavier Guihot Feb 10 '18 at 23:51