How to switch git branches in vscode? - visual-studio-code

I have GitHub repository with multiple branches, and I want to commit and push changes to a specific branch, how can I switch from "master*" branch to another branch?
I tried to switch by clicking the branch name in the left bottom side of the screen and it didn't switch.

Access the "Source Control" tab on the left side of VSCode
Click on the "three small dots" next to the refresh button
Click on the "Checkout to..." option
Choose the branch you want to switch to
You can also try this:
you do git branch branch_name then git checkout branch_name

// it will create new branch and switch to that branch
git checkout -b branch-name
// it will directly switch to previously created branch
git checkout branch-name

for switching between the branches. If you have already more than one branch.
git switch <branch-name> //name of the branch where you want to switch
or,
git checkout <branch-name>
for creating the branch you can use
git branch <branch-name> //Name of the branch you want to create
and for creating and switching to that branch simuntaneously.
git switch -c <branch-name>

Related

How do I access git code locally in a branch?

I like to edit git code that resides in branches. Every time I git checkout into the master, the windows system hides the branch code. So the only way to access that branch code is to git checkout back into it. Is there a way to direct a local editor to open code that's in a hidden branch w/o checkout?
To create a branch in git, open your command line terminal, and type:
git branch
This will show you all of your current branches. Then, to create a new branch, enter
git checkout -b <branch-name>

Github - How to merge to remote feature branch

My team mate created a feature branch from our master branch. I created my own feature branch based off of team mate's feature branch. i.e.
Team mate:
git checkout master
git checkout -b teammateA-feature-branch
What I did:
git checkout teammateA-feature-branch
git pull
git checkout -b teammateB-feature-branch
I made my changes into teammateB-feature-branch and committed them.
Now, how would I create a PR such that my committed changes gets merged to team mate's remote branch i.e. teammateB-feature-branch merging into teammateA-feature-branch??
Any thoughts??
You can do this locally using Git bash:
git checkout teammateA-feature-branch
git merge teammateB-feature-branch
Then, push changes to teammateA-feature-branch:
git push origin teammateA-feature-branch:teammateA-feature-branch
OR
If these branches are available remotely (and locally), you can do the following:
Go to the pull requests tab in your repository.
Click on the Compare button.
Compare teammateA-feature-branch with teammateB-feature-branch.
Create your pull request.
Merge your changes.

Is there any way to clone a specific brach in git desktop?

I tried but its cloning whole repository. I wanted to clone only specific branch in that repository
Thanks in advance
You only can clone a whole repository except a branch, the code is:
git clone -b mybranch --single-branch git://sub.domain.com/repo.git
If you already have the repository cloned, you can refresh the branch you want, if you have forked the project you can refresh the specific branch you want. the code is:
git checkout 'branch if you want to change to refresh it'
git pull <URL of REPOSITORY> 'branch that you want to refresh it'
git commit -m "message"
git push upstream 'branch that you want to refresh it'

Restore branch deleted from GitHub

I had a branch on a GitHub project that I merged into master. I then clicked the 'delete branch' button on GitHub, and thought I was all set.
Turns out I wasn't, and I want to restore/reactivate the branch. I did not delete the branch on my local respository, nor did I run any git fetch/pull afterward. Just clicked the delete button on GitHub.
Wanted to sound out what a good next step should be. Thinking of doing git push from my local box but wasn't sure what the repercussions might be, would the remote repo on GitHub squawk about a dead branch being brought back, etc.
If you didn't remove your branch from your local machine, and you got rights to push to GitHub, you can restore it on Github by pushing it again
git checkout localBranchName
git push origin localBranchName
It doesn't matter if you make a fetch from Github, git wont remove your local branch until you explicitly tell it to do so with
git branch -D localBranchName
In fact, even if you had removed your local branch, if you merged it previously with master, you can restore it locally. You have to go to the last commit, prior to the merge and branch from there. Something like this for example:
git checkout master
git checkout -b localBranchName
git reset --hard HEAD~1 ( 1 is the number of commits you want to undo )
The second command will create a new branch pointing to your last commit on master
The third command will the last commit undoing (only on that branch ) the merge with master.
Another thing you can do is use "git reflog". That command is very usefull since it will show each time you moved between branches and/or commits.
Go to your list of commits. Find the commit with the merge and click on the pull request number (the number prefixed with #). This will direct you to a page with info about the merge and a button with the label 'Restore branch'. Click that and it is restored.
It looks like there is a "Restore branch" button now that shows up in place of the "Delete branch"

Fork from a branch in github

Is there a way to fork from a specific branch on GitHub? … For example, moodle has many branches (1.9, 2.0 … and so on). Can a clone be performed of just branch 1.9 and not the master branch always? Is it possible to clone a specific branch onto my PC?
I don’t know a native way yet, but you can do it following this recipe:
Fork the repository in question (called ‘upstream’) on the GitHub website to your workspace there.
Run the GitHub desktop application and clone the repository onto your PC.
Use the GitHub desktop application to open a shell in the repository. (The git commands are not available from the default PowerShell unless you configure that manually.)
Set the source repository as upstream:
git remote add upstream https://github.com/{user}/{source-repo}.git
Fetch the full upstream repository. (Right now, you only have a copy of its master branch.)
git fetch upstream
Make your file system copy the branch you want and give it any name:
git checkout upstream/{branch-in-question}
git checkout -b temporary
Publish your repo using the GitHub desktop application.
On the GitHub website, open your repository and click ‘settings’.
Change the “Default branch” to ‘temporary’. (Just change the drop-down menu, you don’t need to click the “Rename” button.)
Go back to your repository, go to the ‘branches’ tab, now you can delete the “master” branch.
Delete the master branch on your shell and make a new master branch:
git branch -d master
git branch master
git checkout master
git -d temporary
Once more, publish your repo using the GitHub desktop application.
On the GitHub website, open your repository and click ‘settings’.
Change the “Default branch” back to the (new) ‘master’ branch.
Go back to your repository, go to the ‘branches’ tab, now you can delete the “temporary” branch.
This should be what you were looking for. Perhaps GitHub will provide a more convenient way to do this in future (e.g., clicking “Fork” from a project’s branch results in exactly this behaviour).
Cloning means that you create a copy of the whole repository in your account including all branches and tags. However you are free to switch and track branches however you like.
No command line needed. Just create a new branch in your forked repository in GitHub. GitHub will ask you if you want to clone/mirror this new branch from the upstream repository. You can give any name to the new branch.
Yes, you can clone the single branch. For example, you have a branch named release1.0. If you would like to clone this branch into your pc then use the following line of code:
$ git clone git#bitbucket.org:git_username/git_repository_example -b release1.0 --single-branch
For those who don't like working with command-line. Here is a simple guide using the desktop client for GitHub:
Click the fork button of the repo on GitHub.com:
Make sure you have the desktop client installed
Click this button:
Clone the repo
In the desktop client, select the desired branch
Select the branch you'd like to work on and you're done
I'm posting here the method I've used.
Like the OP I wanted to only copy/fork one branch. But couldn't find an easy way.
in your repo create a new branch. It doesn't need to have the same name as the branch you want to fork
once created, verify that it is the selected branch, and click "Compare"
reverse the order of comparison (I have a userscript for that, see my profile if it's something you want to test).
the "base" repository must be yours, with the branch you've created
the "head" repository is the original, and the branch is the branch you want to fork
hit "create pull request" and continue until the PR is applied
That's it. You have the branch forked.
I'm using bitbucket but I'm sure this would work for GitHub as well.
Create a new repository
Checkout the branch using GitExtensions
Click Push to open the Push dialog
Set the destination URL to the new repository
Set the destination branch to "master"
Push
Your new repository will have the full history of the one branch only (not all branches like forking will have).
A fast, alternative approach is to create your own new repo.
Go to https://github.com/new and make a new repo. Do not initialize with README.
Scroll down to get your git remote
Then:
git remote rm origin
git config master.remote origin
git config master.merge refs/heads/master
// Run code from above image
git push --set-upstream origin yourbranchname
You will have a new repo with the original repo's code and a branch that can be made into a pull request.
SOLUTION:
For remote repository on GitHub and local repository
After fork all branches to your GitHub repository, you can delete Redundant branches in your GitHub repository.
And then you can only clone the branches you need to local.
Step One
Step Two
Only For local repository
git clone -b <branch name> --single-branch <repository>
If you want to further save your disk space, you can clone remote repository without history:
git clone -b <branch name> --depth 1 <repository>
notice: --depth implies --single-branch unless --no-single-branch is given.
https://git-scm.com/docs/git-clone
Switch to the branch you need in source repo
Click "Fork". You'll get forked master and the branch you're in.
I don't know how it works with more branches, but for my needs worked pretty well.