How to switch to another branch

I am using git checkout master, but when i do git branch -a , it is still pointing to branch2.

PS C:\Node\projects\n-5-10-workflow-test> git branch -a
* branch2
PS C:\Node\projects\n-5-10-workflow-test> git checkout master
error: Your local changes to the following files would be overwritten by checkout:
Please commit your changes or stash them before you switch branches.
PS C:\Node\projects\n-5-10-workflow-test> git add .
warning: LF will be replaced by CRLF in server.js.
The file will have its original line endings in your working directory.
PS C:\Node\projects\n-5-10-workflow-test> git commit -m “10th commit”
[branch2 b5c7811] 10th commit
1 file changed, 1 insertion(+), 2 deletions(-)
PS C:\Node\projects\n-5-10-workflow-test> git push origin master
Everything up-to-date
PS C:\Node\projects\n-5-10-workflow-test> git commit -m “11th commit”
On branch branch2
nothing to commit, working tree clean
PS C:\Node\projects\n-5-10-workflow-test>

try this

If you are in branch2 you need to push to branch2 and not in master, with git push origin master you push your local master to origin. If the changes are made in branch2 you had to push to branch2:

git push origin branch2

then you can switch to master with:

git checkout master

In order:

  1. The first command you run is:

    git branch -a

That show the list both remote-tracking branches and local branches.

  1. You tried switch to local master using checkout, but there are changes in the branch2 so the checkout command was aborted.

  2. You added and committed the changes in branch2 using:

    git add .
    git commit -m “10th commit”

  3. Now the error , you pushed to origin master the changes, but there are no changes made on local master so the command:

    git push origin master

Gived the output:

Everything up-to-date

And this is right, because your precedent commit was made on branch2. You need to push branch2 to origin, with this:

git push origin branch2
  1. Last command didnt have sense to run, because there are no staged files.

After push to origin branch2 you can run:

git checkout master

and it will be succesfull.

Hope to solve your problems.



Exactly what @wabri says; your checkout gets aborted because Git could then possible destroy files. See this line:

error: Your local changes to the following files would be overwritten by checkout: