1

Im using Xcode 9.2 with bitbucket for version control. I have the past 4-5 hours worked in the master branch. I realise now that I want to keep the work that I have done today but not part of the master branch but rather as a new branch.

I have been able to push the existing version to the new branch, but it required me to commit to master first (locally).

Now I want to revert the master back to the version from yesterday, and commit this at the master. This is where the problem occur. I am able to revert back to the previous committed version by checkout the commit - but then I am not on the master branch anymore, and I can't commit to master or push to master.

How can I revert back to the master to a previous build, and basically remove the changes done the past 4-5 hours?

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 1
    open up terminal, change directory to your project and run `git stash` command, after you stash the changes, you should be free of the changes you made, make new branch and in terminal write `git stash apply`. Easy, simple, not time consuming :) – Dominik Bucher Feb 03 '18 at 21:27
  • 1
    To remove the last commit from a branch permanently, use `git reset --hard HEAD^`. Use with care, double check. – Till Feb 03 '18 at 21:27

1 Answers1

3

For the next time you realise before you commit:

  1. Open up terminal
  2. cd ${path to your project}
  3. run git stash command
  4. When you stash the not commited changes, now you should be safe to switch and make new branch
  5. now change to the new branch and apply stash: git stash apply
  6. Enjoy your changes.

Since I am confused as hell and this happens to me from time to time, I created alias in my .bash_profile file to delete the local commits by simply making one command in terminal:

alias fixFuckup="git reset HEAD~1"

simply run the command and you will be reset to the state before the commits. Actually this resets one commit.

git reset HEAD~1

to reset to the state before doing lot of commits, prefer using this.

git reset --hard HEAD^

for clarifying the changes please take further look at this:

what is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'?

Dominik Bucher
  • 2,120
  • 2
  • 16
  • 25
  • Well rather close Xcode since you will get notified about file changes and there you go with options Revert or Keep Xcode version... – Dominik Bucher Feb 03 '18 at 21:37
  • Is it important to ensure that the HEAD in Xcode is the version that I want to keep as master? Before running git stash in terminal? – Chris_1983_Norway Feb 03 '18 at 21:40
  • 1
    When I run "git stash" I get "No local changes to save" – Chris_1983_Norway Feb 03 '18 at 21:42
  • 1
    Git stash serves just as command unrelated to any branch or so whatever. It just saves your changes you didn't commit and applies when it's needed. The reason you get this answer is because you already commited the changes. So now you need to just branch those changes, commit them and then checkout develop and reset it... Maybe consider using better Version control software than the Xcode native one. I use terminal, but I started with SourceTree... – Dominik Bucher Feb 03 '18 at 21:44
  • The HEAD is now the commit I want (0f93f3e) - If I run "git reset --hard HEAD^" will the HEAD become the new master? – Chris_1983_Norway Feb 03 '18 at 21:55
  • 1. Download source tree 2. You will see what's going on in there it's better because I cannot understand what is your probelm actually right now... – Dominik Bucher Feb 03 '18 at 22:03
  • It worked perfectly. Basically I checked out the master to make it the head. Then I used "git reset --hard HEAD^" in terminal. Now Master reverted back to the commit before the current, exactly what I wanted – Chris_1983_Norway Feb 03 '18 at 22:03
  • Okay that's great :) When you would push the unwanted commited changes, you could reset them back with force push (if you have rights to do force push.) Just for your info. – Dominik Bucher Feb 03 '18 at 22:05