Create a local branch from remote branch that already exist in git repository


There will be a need when we want to create a new branch in local based on remote branch (not master) in repository. For instance, we have three branch:

1
2
3
1. Master
2. Development
3. New-branch

This “New-branch” already exist in our remote git repository. Then, we need to make sure if we can see this remote repository in our local.

1. Compare local and remote branches repository
When you’re doing “git branch -a” but you don’t see the same things in remote repository, then something wrong here.
eg:

1
2
– master
– development

This mean our remote repository doesn’t detected in our local. Try with:

1
git fetch && git branch -a

If you don’t see this remote branch, that’s mean you doesn’t have fetch into remote git.
Just add your remote git into local by:

1
2
git remote add origin git@github.com:<YOUR-GITHUB>/<your-project>.git
git fetch

Now you will see all branch in remote repository in your local branch list.

2. Create a new local branch from remote branch

1
git checkout –track -b <your-local-branch> <your-remote-branch>


Now you can get local branch same with the remote.

3. Clean deleled remote repository
Sometimes remote repository being deleted and you may have wrong remote references.
You can clean this things by:

1
git remote prune origin

4. Git cleaning local tags branch that already deleted and doesn’t have remote repository
This is happen if you’re using git flow and you see git checkout with all old repositories history.

1
2
git tag -l | xargs git tag -d
git fetch

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.