Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout -b, branch already exists

Tags:

When I'm merging two branches and they can't be merged automatically Github provides these instructions:

Step 1: From your project repository, bring in the changes and test.

git fetch origin git checkout -b master origin/master git merge develop 

Step 2: Merge the changes and update on GitHub.

git checkout develop git merge --no-ff master git push origin develop 

But, in this case, the branch master already exists locally, and the line git checkout -b master origin/master returns this message:

git checkout -b master origin/master fatal: A branch named 'master' already exists. 

Is the correct thing to do in this case to replace that line with git checkout master? I've wondered this for a while, bit worried about what git checkout master might do as opposed to with -b.

like image 239
t56k Avatar asked Dec 03 '14 21:12

t56k


People also ask

How do I force checkout to another branch?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

How do I switch between branches without committing?

you can do git checkout -m <branch-name> to merge conflicts and checkout to the branch and resolve conflicts yourself, or git checkout -f <branch-name> to ignore changes.


1 Answers

If master doesn't exist, then after this line

git checkout -b master origin/master 

master will be a branch pointing to the same commit as origin/master.

If you already have a master branch, it might be out of date with origin/master, so simply writing

git checkout master 

isn't quite enough. You'll also want to run

git merge origin/master 

afterward to bring master up to date (typically this will just be a fast forward).

like image 165
Ismail Badawi Avatar answered Oct 23 '22 18:10

Ismail Badawi