How do I rename branch on the GitHub website? - github

I don't know how to run command line stuff. I just don’t have the environment.
So I'm trying to rename a branch on the GitHub website. It was, by default, named patch-1.
Is it possible to rename it on the site?

I just did it without downloading any code to my laptop and using only the GitHub site.
The solution looks the same as #swcool’s, but I want to add about the default branch.
In my case, the name of the renaming branch did not exist.
Change the default branch (to the old branch you want to rename)
Create a new branch (with a new name)
This action will copy all the contents of the default branch (the branch with the old name) to the new branch (with a new name). At this time, you have two branches with the same code.
Change the default branch (to the new one with a new name)
Delete the old branch

I think you can, just create a new branch with the new name, and delete the old one on github.
More detail you can see here.

It is not possible to rename a branch from the Github website. You will need to do the following -
Setup your Git Environment
Follow this - https://help.github.com/articles/set-up-git
Rename branch locally & on Github
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote

If you don't want to install Git, clone the repo, rename the branch locally and push it back to GitHub, you can use the GitHub API for references:
create a new branch where the old one is:
POST /repos/:owner/:repo/git/refs
{
"ref": "refs/heads/newBranchName",
"sha": "<SHA1 of old branch>"
}
delete the old branch:
DELETE /repos/:owner/:repo/git/refs/heads/oldBranchName
That way, you will have "renamed" (create+delete) the branch without having git locally.
And, as commented by user3533716 below, use the GitHub API for listing branches to get those branch SHA1:
GET /repos/:owner/:repo/branches

Since Jan., 19th 2021, you now can rename a branch directly on github.com:
Support for renaming an existing branch:
You can now rename any branch, including the default branch, from the web.
If you've been waiting to rename your default branch from master to main, we now recommend doing so using this feature.
When a branch is renamed:
Open pull requests and draft releases targeting the renamed branch will be retargeted automatically
Branch protection rules that explicitly reference the renamed branch will be updated
Note: admin permissions are required to rename the default branch, but write permissions are sufficient to rename other branches.
To help make the change as seamless as possible for users:
We'll show a notice to contributors, maintainers, and admins on the repository homepage with instructions for updating their local repository
Web requests to the old branch will be redirected
A "moved permanently" HTTP response will be returned to REST API calls
An informational message will be displayed to Git command line users that push to the old branch
This change is one of many changes GitHub is making to support projects and maintainers that want to rename their default branch.
Branch names will not change unless the maintainer explicitly makes the change, however this new rename functionality should dramatically reduce the disruption to projects who do want to change branch names.
To learn more about the change we've made, see github/renaming.
To learn more, see Renaming a branch.

To rename a branch on the Github website, just go to your repo's home page, click on where it says "branches"
Then, find the branch you're interested in, click the pencil button
and from there, you can rename your branch.

If you want a GUI based solution - download the Git Client "GitKraken". It supports doing this from UI by right-clicking on the branch name and choosing "rename [branch name]".

Related

What is the best practice to get the updates from another repo without making any PR after changes?

I'd like to know how to proceed in GitHub where I could to be able to get the updates from the original repo but prevent opening a PR after each time I push a change made by myself?
The concept I want to apply this is to use a blog template for my GitHub pages. I'd like to get the feature for the future if the contributors would make any but at the same time, I'd like to prevent pushing anything to the original repo as a PR since those commits wouldn't include anything related to making a contribution to the project.
PRs aren't generated automatically, you need to explicitly create them from a branch.
You can fork a repo and work on it, and when needed, fetch and rebase from the original repo you forked from. As long as you don't explicitly use this repo to create PRs on the original repo, you should be fine.
EDIT - Adding some details as per the last comment:
Assume there's a repo called something owned by someone. You can start off by forking it to youruser using the GitHub UI. Then you can clone your fork and work on it:
git clone https://github.com/youruser/something.git
In order to get the recent changes from the original someone/something repo, you need to set it up as a remote. By convention you'd call this remote your "upstream", but you can really give it any name you choose:
git remote add upstream https://github.com/someone/something.git
Once you've added it as a remote, you can fetch from it and rebase on top of it:
git fetch upstream && git rebase upstream/main
(note that using the main branch is just an example. You can of course rebase on top of any branch in the remote repo)
I think it's not possible because when you clone or fork that repo, from that time, you start to add your own content to it since it's your personal blog. So you cannot keep getting the features from main repo. Maybe you can try rebase but I'm not sure if it works for this case. Or you can add those features to your repo by your own whenever you need them.

GitHub - Pull changes from a template repository

I have created a Template Repository in GitHub and then created some repositories based on the template. Since they were created, there have been updates to the template that I want to pull into those repositories.
Is this possible?
On the other repositories you have to add this template repository as a remote.
git remote add template [URL of the template repo]
Then run git fetch to update the changes
git fetch --all
Then is possible to merge another branch from the new remote to your current one.
git merge template/[branch to merge] --allow-unrelated-histories
https://help.github.com/en/articles/adding-a-remote
I will link to the same location as HRK44 but my answer is very different.
https://help.github.com/en/articles/creating-a-repository-from-a-template
Although forks and templates are mentioned in the same section, they are very different.
One of the differences mentioned in the link is:
A new fork includes the entire commit history of the parent repository, while a repository created from a template starts with a single commit.
This basicly means that you will not be able to pull new changes from the template as your git histories are very different and are not based on the same thing.
If you do use the method mentioned in the accepted answer, you will have very hard manual merges that will result in changes to all of the files received from the template, even if they werent changed since the first time you created that repo from that template.
In short, creating a repo from a template (using only master branch) is the same process as:
git clone template
cd folder
rm -rf .git
git init
git remote add origin <new repo url>
git add .
git commit -m "Initial Commit"
git push -u origin master
A few other things that are not (surprisingly) copied when creating a repo from a template:
(Unless github fix this at a later point)
Repo configurations (allowed merge types, permissions etc)
branch rules
So when using this at your organization, make sure to set all repo configurations on the newly created repo.
If you want to merge changes from a template into your project, you're going to need to fetch all of the missing commits from the template, and apply them to your own repo.
To do this, you're going to need to know the exact commit ID that you templated from, and you're going to need to know the commit ID of your first commit.
ORIGINAL_COMMIT_ID=<commit id from original repo you templated from>
YOUR_FIRST_COMMIT=<first commit id in your repo>
YOUR_BRANCH=master
Next you're going to need add the template as a remote, and fetch it.
git remote add upstream git#github.com:whatever/foo.git
git fetch upstream
And finally, you need to rebase all of the commits you're missing onto your branch
git rebase --onto ORIGINAL_COMMIT_ID YOUR_FIRST_COMMIT YOUR_BRANCH
What this is doing it basically creating a branch off of ORIGINAL_COMMIT_ID, then manually applying all of the commits on your original branch, onto this new branch.
This leaves you with what you would have had, if you had forked.
From here, you can git merge upstream/master just as if you had forked.
Once you've completed your merge, you'll need to use git push --force to push all of the changes up to the remote. If you're working with a team, you'll need to coordinate with everyone when doing this, as you're changing the history of the repo.
Note: It's important to note that this is only going to apply to one branch. If you have multiple feature branches, you'll need to perform the same steps to each one.
#daniel's answer also did not work for me because of the unrelated histories problem mentioned in #dima's answer. I achieved the desired functionality by doing the following:
Copy the URL for the template repository you wish to use to create a new repository. (ex: https://github.com/<username>/my-template.git)
Use GitHub Importer to make a new repository based on the template repository.
This solves the unrelated histories problem because it preserves the entire commit history of the template repository.
You need to use the Importer because you cannot fork your own repository. If you want to use someone else's template repository, you can just fork theirs.
Then, add the template repository as a remote.
git remote add template https://github.com/<username>/my-template.git
After you make new commits to the template repository, you can fetch those changes.
git fetch template
Then, merge or rebase. I recommend to merge on public repos and rebase on private repos.
To merge
git checkout <branch-to-merge-to>
git merge template/<branch-to-merge>
To rebase
git checkout <branch-to-merge-to>
git rebase upstream/<branch-to-merge>
NOTE: When rebasing, you must
git push origin <branch-name> --force
in order to override your old commits on your remote branch. This is why I recommend to rebase only on private repos.
I approached this differently as fetch & merge was not ideal as lot of files diverge across template and downstream projects. I only needed the common elements to sync.
lets says we have the below folder structure locally:
repos
├── template_repo
└── downstream_repo
1. Now create a git patch from the parent folder (repos):
git diff --no-index --diff-filter=d --output=upstream_changes.patch -- downstream_repo/some_common_path template_repo/some_common_path
NOTE - the order of the paths matters!, downstream_repo comes first! (interpret this as "what are the changes we need to make to downstream_repo to make it same as template_repo"; look at the --name-status output, it will hopefully make sense.)
--no-index option generates git diff based on filesystem paths. Path can be a single file or a folder.
--diff-filter=d will ignore any files that are in the downstream_repo but not in the template_repo. This only applies when diffing folder paths.
You can use --stat, --name-status to see what the patch will contain.
Review the generated patch.
2. Change to downstream_repo folder and apply the patch
git apply -p2 ../upstream_changes.patch
explanation for -p<n> option from the official doc:
Remove leading path components (separated by slashes) from
traditional diff paths. E.g., with -p2, a patch against a/dir/file
will be applied directly to file. The default is 1.
Using -p2 will drop a/downstream_repo and b/template_repo from the diff paths allowing the patch to apply.
This is the reason for starting with above illustrated folder structure.
Once the patch is applied, rest of the process should be familiar.
All of these options are clearly explained in git diff and git apply official docs.
Another option is to create a patch from the necessary commits and move the patch to a new project
git format-patch -1 HEAD
Insert a patch
git am < file.patch
details are here
I ran into this same issue. I have 10+ projects all created from the same template project (react-kindling) and using git alone wasn't sufficient, as it would pull in changes to the template that I didn't want in my child projects.
I ended up creating an npm utility for updating child projects from template starter projects. You can check it out here:
LockBlocks
It's been a real life saver. Pulling changes from the template is a heck of a lot easier now.
This works too:
git remote add template git#github.com:org/template-repo.git
git fetch --all
git merge template/main --allow-unrelated-histories

How to work on a project fork when having the original in Eclipse?

I'm trying to create a pull request on GitHub for project "original/QWERTY" so I forked the repo to "Mark/QWERTY". In Eclipse, I already have a repository set up for "original/QWERTY" and that project is in my workspace, named QWERTY.
Now if I create a new repository pointing at "Mark/QWERTY", I'll have two projects with the same name and both Eclipse and me won't like it.
I thought that it should be possible to have a branch or another remote under a repository and switch between them instead of having two copies (I mean just store the diffs). The problem is that they are different projects on GitHub so I'm not sure how to do it.
What is the correct way to set up two GitHub projects to create a pull request from my fork to the original one in Eclipse with EGit?
The usual workflow for forked repositories is to have a single local repository with a single work directory that is configured to fetch and push from/to multiple remote repositories.
With this setup, you can switch between branches that originate from different remote repositories.
The Fork a repo documentation of GitHub explains this setup when using CLI Git. Most of it should also apply to repositories hosted elsewhere.
Using the EGit documentation, it should be possible to translate these instructions into the corresponding actions in EGit.
How to manage multiple remotes with EGit is documented here: https://wiki.eclipse.org/EGit/User_Guide#Remote_Repositories
Using the information from Rudiger's comment and answer and my trial and error with branches I made my own steps. This picture also helps with terminology.
First, do these 2 things in any order:
fork the original project in the github website so now you have the original and the fork. They have the same code and branches.
create a local repository pointing to the original repo on github. Let's say you decided to select only the master branch.
Remotes: you get a new remote I'll call origin (default). configure its fetch if it's not done for you, the default specification is +refs/heads/*:refs/remotes/origin/*. This ref spec maps all the repo branches to Remote Tracking branches with the same name. If you want to only fetch the master branch then use +refs/heads/master:refs/remotes/origin/master.
Branches: you get a "Remote Tracking" branch called origin/master and a local branch called master with configuration of "Remote: origin" and "Upstream Branch: refs/heads/master". You will be working under the local master as it's the only branch right now.
Now you want to be able to push to your fork so you can create PR. You can and already did pull from the original to keep getting updates from other people's work.
Right click on "Remotes" and create a new remote, I'll call it fork (call it whatever you need). Configure its push.
the URI points to your fork the same way the origin Remote URI points to the original.
The ref mapping maps the branches. Go to "Advanced" and click "Add All Branch Specs" if it isn't done for you. You should get the spec refs/heads/*:refs/heads/*. It's easy to work with this spec but you can change it to whatever you need.
Create a local branch (right click -> switch to -> new branch) whose source is the local branch named master and the branch name is whatever suits what it does. it can be the master branch or a new branch that let's say fixes a bug, so bug 123. You do not have a Remote Tracking branch because those are used for pulls. If you also pull from fork then you will need to configure that in the Remote fork and get a remote branch.
Now you are working on a local branch bug 123 (you can see a checkmark next to it). Fix the bug in your code and in the Git Staging view you should see the files changed and the title is <Repository name> [bug 123]. Make sure you are going to commit/push to the correct branch! Stage whatever you need and commit (adds the changes to the local branch bug123) and push (creates a branch on the github repo called bug 123 if you stayed with the default spec).
Now go to the GitHub repo page of either the original or the fork and the UI will tell you that you can create a PR. From there GitHub will guide you.
Once the PR is merged into the master branch of the original on GitHubm, you will want to fetch from the master.
Right click on the Remote origin or its fetch "subdir" and choose fetch. The will fetch any changes in all the remote branches because the fetch spec we used maps all the branches (we used the * character).
That's it. Continue to switch to a local branch which maps to your fork based on the updated master, fix bug, commit and push, create PR, wait for merge into the original, fetch and pull from the original.

git push to remote and lose all history

I have a git project that I'm about to push to SourceForge. The origin for my local repo is a shared file system repo that gives me a backup facility.
When I eventually add SF as another remote I just want to push the latest (= versioned) commit to it as the starting base of my code on that repo, and not include all the previous commits that contain possibly rubbish/sensitive/embarrassing code.
My question is similar to this one, except that question was about just leaving out some of the history - I want to leave out all of the history and have the latest commit to become the starting point of the project code on SF. Importantly, having done this, I want "push to upstream" to continue to work even though origin and SF will be different.
Is this possible? Incidentally I'm doing this through Eclipse ie. eGit.
Update
My original question should have been clearer, although the answers so far have helped clarify exactly what I'm trying to achieve.
I want just consolidated commits pushed to SF, representing the published versions.
This is what I want to do:
[master] A--B--C--D--E--F--G--H--I... --> push to origin (private)
\ \
[public] V1----------V2... --> push to public remote repo
#michas's answer starts me off with V1 on branch public, but I can't figure out how to continue to extend this branch with subsequent version commits. I've experimented with rebase but can't get the result I want.
It sounds like you want to start with a new repo. Why don't you just delete or rename your old repo and create a brand new one. Then copy all of your files in, commit them, and push.
Well, you cannot push the current commit, as this commit contains the whole "rubbish" history.
However you can create a new commit with the same content but without any history.
git checkout --orphan fresh # create a new branch called `fresh` without any history
git commit # add your work as a new commit
git diff fresh master # the both branches should contain the same content (assuming you original branch was called `master`)
git log # verify the current branch does not contain any history
git push sf fresh # push that branch
git push sf fresh:master # (or you might want to call that branch master on sf)
The answer provided by #michas didn't allow me to subsequently maintain the branch with consolidated history. This required the use of git merge --squash. The scheme I eventually came up with was similar to the one described here.
Just tidying up so the question has an upvoted answer.

Branch created by someone else is not visible

There is a branch in github that I can access via Eclipse/eGit. A colleague then created a branch off of that existing branch, but that new branch is not visible to me in Eclipse or from the command line. What do I need to do to make that branch-off-of-a-branch visible to me?
Your colleague needs to push it to Github, then you need to do a fetch to get it from Github. Then, if you want to work on that same branch yourself, you need to create a local branch based on it, which, in recent Git versions, can be done simply via git checkout <simple branch name>.
Update: So when you have trouble fetching remote refs, you should check that your fetch refspec looks something like +refs/heads/*:refs/remotes/origin/*, or you won't see the whole picture.