Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Switching branch

Tags:

git

branch

There is somthing I don't get yet with git. It is branch. So let say that I have a local repository A which I clone from a remote one B. So now A have the master branch checked out.

So when I push from A it goes to B master.

B is just a clone on github, a clone of C.

From time to time in other to get in sync I pull from C master branch.

But now C master branch is quite broken for the time being. Since from A I had pull from C my local A is also bugy.

So I would like from A to pull the C stable branch. How do you guys usually do in this situation?

Do you create a new branch on A and pull from C. But since A have the C master change I need to revert it first...

like image 975
mathk Avatar asked Jan 28 '11 09:01

mathk


People also ask

What command is used to switch branches?

The git checkout command allows you to switch branches by updating the files in your working tree to match the version stored in the branch that you wish to switch to. You can think of it as a way of switching between different workspaces.

How do I switch to a previous branch?

Use the git switch - (Or git checkout - ) to switch to the previous branch you were working with. This is pretty similar to the cd - command, which is used to switch to the previous directory.

What happens when you switch git branches?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.


2 Answers

git fetch C
git checkout C/stable-branch
git checkout -b myCopy

Then myCopy is a local (copied) branch of C's stable one.

like image 84
poke Avatar answered Oct 17 '22 18:10

poke


In two lines:
git fetch C
git checkout -b myCopy -t C/stable-branch

myCopy is now a local branch of C/stable-branch, and is tracking it, so you can do git push and git pull without a refspec.

like image 24
urschrei Avatar answered Oct 17 '22 16:10

urschrei