How do I switch my fork to a different Github branch? - github

I made fork a branch of a Github repository. I made changes to my fork.
A new branch was added to the repository. I want to merge the changes from the new branch to my fork, preserving my changes to my fork.
I don't own the original repository.
How do I do this?

#add the remote to your fork.
git remote add upstream https:/github.com/whoever/whatever.git
#update your personal fork while keeping changes (swap master with the branch you want).
git pull upstream master --rebase --autostash
This should add the remote repository to your git repo (your fork) and then you tell it to fetch the changes from that branch and add your own changes on top.

Related

Github Desktop: Rebase vs Merge Commit, to keep a fork up to date

My work flow to contribute to a repo, is
Create a fork of the upstream
Clone the fork to my local
Push changes to my fork
Create a PR to merge my change into upstream
Sometimes while I am working on my change, other devs might commit their changes into the upstream, so I want to do a rebase which will bring my fork up to date. But I have noticed that in the below scenario, Rebase and Merge Commit gave me different response,
The way I tested it is as follow:
I created a fork of the upstream
I cloned my fork to my local using GitHub Desktop, and set current branch to my fork branch
I then made a change in upstream/master branch and committed it
when I clicked "Rebase current branch", it tells me the current branch is up-to-date as below:
when I clicked "Merge into current branch", it detects a change in the upstream:
upstream/master is the branch in the upstream, and muti/master is the branch in my fork.
My question is, why rebase doesn't detect any change, while the merge and commit does?
Note first that if you have access to the upstream repository, you don't have to fork it in order to push a fix branch and initiate a PR (Pull Request).
You can do all that directly in the upstream repository.
Second, the rebase would only work if the current branch has a divergent history from upstream/master.
If the current branch HEAD is an ancestor of upstream/master, the rebase (of the current branch on top of upstream/master) would be a no-op.
But a merge (of upstream/master into the current branch) would not, since from the point of view of the current branch, there is one commit (done in upstream/master) that it does not have.

Sync my Pull request with someone else commit some changes - Github

I sent a PR and someone else made some changes so it has 3 more commits and now I need to sync that PR with my local to continue working on some changes. How could I do to sync my local with the PR. Thanks
A pull request is basically just a designated branch on your fork of the repository. Update that branch (by rebasing its commits on top of those new commits) and the pull request will automatically update to show these changes.
First, you need access to the additional commits in your own fork of the project. To do so, you can add the upstream repository as an additional remote and fetch the commits from there:
git remote add <new-remote-name> <upstream-url.git>
git fetch <new-remote-name> <upstream-branch>
Then you can rebase your changes on top of the upstream changes:
git checkout <your-pr-branch>
git rebase <new-remote-name>/<upstream-branch>
git push <origin> <your-pr-branch>
You need to rebase your branch to reflect all the merged work of others in your PR. Here is how you can do this:
Let's say upstream is the name of the remote repository which you have forked, and origin is the name of your forked repository.
git pull --rebase upstream master
git push origin master
There can be merge conflicts sometime during rebasing. In that case, check the files in which conflicts are there using:
git status
The name of the files which are in red color are the files having conflicts. Resolve the conflict manually. After resolving, add the files using:
git add <filename> or //for each conflicted file
git add --all //to add all files at once
Once again check the status using the git status command. If all the files are shown in green that means they are resolved and you can now continue your rebase. Run the below command for continuing your rebase:
git rebase --continue
Once done, simply push the changes to your forked repo using:
git push -f origin master
Note: In case, you have not set the remote url of your upstream and origin, add them using following commands:
git remote add upstream <upstream-url>
git remote add origin <origin-url>

How to update the branched forked repo?

How can I update a repo on github using git bash which is forked and after fork I have created another branch on it.
I want to update that branch.
I have create a branch locally with the aim of pushing to your remote fork.

Github: fork into new branch

I need to fork the branch from PrestaShop:develop into a different branch and not username:develop
How can this be done?
Fork = clone repository of another user on Github, and make GitHub know about it
1) Fork the repo as usual with the fork button
2) If you want to rename the branch
git branch -m old_branch_name new_branch_name
git push origin :old_branch_name
git push origin new_branch_name
If you want to rename not branch but the whole repository - see, How.
If you want to have both branches in one repository, it is not a GitHub feature named fork, it is branching: branch ..., or checkout -b:
git checkout -b new_branch old_branch
git push -u origin new_branch
In that case you should not cope with GitHub forks to create a new branch from yours existing one.

I've fork a repo in github. How could I fetch all changes from the original repo?

In this case, I've fork grocery-CRUD repo from github.
I make some changes, and submit a pull request.
The author accept it, and do some other modification.
How could I fetch the newest modification to my grocery-CRUD repo?
Thanks
You add his repo as a remote (git remote add <his repo> <repo spec>).
Then, you pull changes from his repo (git pull <his repo>)
Then, you push the changes to your repo (git push origin)
git pull git://github.com/scoumbourdis/grocery-crud.git
This will fetch the changes and merge them with your current branch in your local repository.
If you want to get it on your remote you need to push
git push origin