0

Sounds like a silly question, so let me explain:

My iOS app project was on branch 'master'. I created a branch called 'iOS 7' and did all my work for the iOS 7 upgrade on that branch. Now I basically want the 'iOS 7' branch to become the master branch, so that I'm back to working on 'master'. I tried merging and there were just way too many conflicts that it wasn't worth the trouble of resolving them. All I care about now is the 'iOS 7' branch, I don't care about the rest.

The obvious solution is to just bin the .git directory and start over with a new repository from where I'm at with 'iOS 7', but I wondered if there was a way to do this within git. Basically, some way to say, "look, this branch is now the truth, make it the master branch. Ignore whatever is currently in 'master'"

EDIT: I should add that I'm the only person using this repo, and it only exists locally on my Mac. There is no remote repo.

EDIT 2: As per the answer I accepted, this question is not actually about merging at all, since nothing is being merged, which is why I put 'merge' in quotes. I used the word 'merge' only because that is the term generally used when referring to bringing changes in a branch back to the master. Usually that would imply an actual merge, my question is how to avoid that merge. The accepted answer does that perfectly (as would several of the other answers).

4 Answers 4

2

master is just yet another branch name. So you can easily rename your branches:

git branch -m master master-old git branch -m "iOS 7" master 
Sign up to request clarification or add additional context in comments.

4 Comments

+1 This is a better solution than accepted answer given that all branches exist locally only.
@ansh0l Why is this "better"? It retains a pointer to a dead branch, meaning that useless objects can never be garbage collected. The question explicitly states that he wants to discard the old state; deleting the old branch is the most correct way to achieve this.
@meagar OP says his iOS app project was on 'master'. and he has created branch 'iOS 7' for iOS 7 upgrade. To me, this means he has original commit history on the master branch, and corresponding to ios7 updates on the new branch. Since he hasn't mentioned how he created ios7 branch, assuming that the new branch was checked out from master and preserves the commits of the master branch is an incorrect assumption. (He wouldn't be facing merge issues in first place if he did that - he has not worked on master branch since, and so the branches wouldn't have diverged completely).
@meagar He also suggests starting over with a new repository from where he is at with 'iOS 7', and thus appears relatively new to git to me. I would hence prefer renaming the branch over deleting it permanently (for the off case of him not completely knowing the consequences of deleting). As for the GC, that's lower preference than information loss. Of course, since he stated that he doesn't care about the old state; deleting the old branch is a very very valid solution, but IMO its not the most correct solution.
1

No part of what you describe is in any way related to merging. You're the only developer, and you simply want to move a branch, completely discarding its old state? Just delete your master branch, and recreate it at the head of your ios7 branch.

# Make sure we're starting from the correct branch $ git checkout ios7 # Delete the old 'master' pointer $ git branch -d master # Create a new 'master', pointing at the current commit $ git checkout -b master 

The last command creates a new branch called master pointing to the current commit (the head of ios7) and then checks it out.

8 Comments

git checkout -B master ios7
@jthill ios7 is redundant, the default starting point is the current branch if non is specified.
So, the explicit ios7 checkout, the ios7 in git checkout ios7, isn't redundant, but the ios7 in git checkout -B master ios7 is?
@jthill Yes; they're mutually redundant. If you use one, you don't need the other.
See, that was kinda the point: you can do the whole job in one command.
|
1

You can tell Git to automatically favor one side of the merge using the strategy options ours or theirs (see Merge Strategies). In your case, if you are on the master branch and want to merge the iOS 7 branch, do this:

git merge --strategy-option=theirs "iOS 7" 

Comments

1

If you're on branch master:

git reset --hard 'iOS 7' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.