New to github. Fork or branch? - github

I'm new to github. I just downloaded the Windows GUI github version. I want to contribute to someone's project. Should I fork the original repo to my repo and start from there or should I branch off from the original repo? How do I merge it back to the original repo later? How to do this on the GUI or do I need to use Git shell?

Unless the original author lets you push to his repo, you won't be allowed to do it. The usual process to contribute to a project you don't own is to:
create a fork of the repo
clone the fork you've just created to work on it locally
create a branch to add your contribution
add commits to this branch
push it to your forked repo on github
send a pull request to the original repo on github so that the author can fetch the branch from your repo, test it, and integrate it if he likes it.
This is explained when you click on the giant 3 - Fork a repository link on Github's home page

Related

How to open a pull request on github for an improvement in a bitbucket fork?

I cloned a github repository into our Bitbucket account. Similar to https://gist.github.com/sangeeths/9467061.
I fixed a bug in my repository located in bitbucket. I'd like to open a pull request on the original github repository that fixes the bug.
If my fork were located on github, I would simply follow these instructions, but since my fork is on bitbucket, I don't know where to start.
How can I open a pull request from my bitbucket repo to the original github repo ?
You can:
clone the original GitHub repository to a different folder
add your local BitBucket repo as a remote
fetch your fix branch (make sure you fix is done in its own branch, not master)
create a PR using the cli/cli GitHub command line interface gh pr create
That is:
git clone https://github.com/original/repo
cd repo
git remote add bb ../yourLocalBitbucket/repo
git fetch bb
git checkout bb/fix
gh pr create
The gh pr create command will do the work for you:
When the current branch isn’t fully pushed to a git remote, a prompt will ask where to push the branch and offer an option to fork the base repository.

"Disconnect" forked Git repos in VSTS

In VSTS, I forked a repository to develop a separate product from the original repo.
When I make a PR from a branch into master in my forked repo, VSTS defaults to merging into the original repo. I have to be sure not to mistakenly merge into the original repo with every PR.
VSTS seems to think that I may want to merge changes from my forked repo into the original one. I have no plans to do so. How do tell VSTS to 'disconnect' my forked repo from the original?
No, there isn’t such feature in VSTS, there is the user voice that you can vote: Allow option of converting forks to repos
Work-around
As a work-around (in Visual Studio) until it's fixed:
Pull the repo.
Delete the remote repo.
Create a new remote repo with the same name.
Push the repo.
You might have to create a temporary remote repo (named temp if you'd like) in order to be able to delete the remote repo. You can delete the temporary remote repo, named "temp", after you've pushed to the new remote repo.
You'll most-likely break anything (pull requests, work items) linked to the repo and also have to re-apply any policies and security stuff.
repo = the faulty fork in VSTS.
temporary remote repo = a temporary tepo created if you cant delete the fork repo.
new remote repo = the new repo to be used instead of the fork.
It seems like one should disconnect the old remote origin and set the upstream before pushing. Maybe the push with the --set-upstream overwrites that?
Here's what I'm did:
git clone ACCOUNT#vs-ssh.visualstudio.com:v3/ACCOUNT/PROJECT/FORK-NAME NEW-NAME
git remote rm origin
Create the new repository on VSTS for NEW-NAME
git push --set-upstream ACCOUNT#vs-ssh.visualstudio.com:v3/ACCOUNT/PROJECT/NEW-NAME develop
This worked for me to change from a fork to a repo. I can see the full history and open changesets from the web viewer. I did this in Azure DevOps (formerly Visual Studio Team Services AKA VSTS). I did not test against GitHub.
Pull requests and pushes are lost. Commits are still linked
I'm not sure if you need to do the push multiple times for different branches. My fork only has a develop branch anyway. I only create a master branch so git flow doesn't complain.

Update all branches on forked repo with github.com?

I have a forked repo. The original repo has been updated and I need to pull in these changes. I would prefere to use github.com for this rather than the command line if possible.
From the github.com page of my fork I have a button called Compare which allows me to compare my branches with the branches in the original repo, and merge if there are changes. This should do what I want except that the changes I need to update are in a new branch that isn't in my repo.
IMHO, the best way to do this is by installing the Pull app in GitHub.
After forking a repository, you can then enable the app for the
repository, and then configure the app for the forked repository. 🙂

Eclipse Egit: Update a github fork to merge changes of master repo?

I have forked a github repo. Now my worked repo is out of sync with the original repo.
I understand that I need to do a merge, but how to do this with Eclipse git is a bit unclear to me.
Question: How do I update my fork to include changes made to the original repo made by the owner?
Edit 1: I've added another remote. But I don't see any Pull buttons on the context menu:
You do the merge on your local machine and then push the result from your local machine to your own github repo. There is no way to update your github repo directly on the server.
To do that, add that original github repo as a new remote. This is described in the egit user manual. Choose "configure for fetch" instead and give it the URI of the original repo. Afterwards you can simply use the context menu "Pull" on that new remote node to fetch and merge all the new stuff. After finishing the merge, you "Push to upstream", which is your own github repo.
Try the second url under upstream to push your changes. The first one ist just for fetching (see the green/red arrows ;-)

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.