Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 4

How to switch to another branch

Solved! Go to Solution.

 

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
master
remotes/heroku/master
remotes/origin/branch2
remotes/origin/master
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:
server.js
Please commit your changes or stash them before you switch branches.
Aborting
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>

3 Replies
Copilot Lvl 3
Message 2 of 4

Re: How to switch to another branch

Solution
Commander Lvl 1
Message 3 of 4

Re: How to switch to another branch

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.

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

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

git add .
git commit -m "10th commit"

4. 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

5. 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.

 

-Gabriele-

Commander Lvl 2
Message 4 of 4

Re: How to switch to another branch

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:
server.js

- Mark