I've been looking for a way to automate a task, one manual part that needs automation is this restore branch button, is there a way to do this using Github API? I've read these two documents and couldn't find anything, Thank you.
https://docs.github.com/en/rest/reference/pulls
https://docs.github.com/en/rest/reference/branches
Following off the advice from here: git can I view the reflog of a remote?
I was able to restore a PR branch via the GitHub CLI/API by...
Getting the SHA of the HEAD of the branch to restore with:
gh pr view {pr_number} --json commits
where the last commit in the "commits" array will have a "oid" value (which I believe stands for orphan id), and that can be used to create the restored branch with
gh api repos/{owner}/{repo}/git/refs -f ref=refs/heads/{new_branch_name} -f sha={oid}
# set new_branch_name to the name of the PR branch to keep same behavior as button
and now the branch can be fetch'd when within a cloned instance of the repository:
git fetch
but is also available via API as there is now a new remote branch
Sorry if you can't use gh. This worked for me and I don't mind the dependency. If someone likes they can figure out a full REST API only way.
It used to be possible to create a branch from a gitlab issue, however this functionality is no longer there.
Does it need to be explicitly enabled somewhere?
Or is there a new way to create a branch from an issue?
This one tripped me up as well!
Per the Gitlab user documentation,
https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-new-branch-from-an-issue
The Create merge request button doesn’t display if:
A branch with the same name already exists.
A merge request already exists for this branch.
Your project has an active fork relationship.
To make this button appear, one possible workaround is to remove your
project’s fork relationship. After removal, the fork relationship
cannot be restored. This project can no longer be able to receive or
send merge requests to the source project, or other forks.
Otherwise, the button displays as indicated in the post by #Do_Nhu_Vy.
This feature is still exists. Let's see the sceenshot.
I'm working on GitLab Community Edition 8.15.3.
When I go to my project --> Issues --> select an issue (let's say which is named "Fix UI problem on IE8") --> New branch.
It creates a new branch named 41-fix-ui-problem-on-ie8 without asking me to select the original/source branch.
My 2 questions are:
What's the default original branch used by Gitlab when I create a new branch from an issue?
Is there a way to "force or activate an option on Gitlab" to give me the choice to select the source branch when I create a new branch from an issue?
When using the "New Branch" button in an issue, the branch is created off of the project's default branch. Normally that would be the master branch, but you can set the project's default branch in the project settings.
UPDATE
GitLab has added the ability to specify the source branch when creating a branch or merge request in an issue. You have to click the dropdown arrow on the "Create merge request" button, and then you'll see these options:
your answers:
What's the default original branch used by Gitlab when I create a new
branch from an issue?
When you create a new branch, by default the current branch you are in is your source branch. you can change it according your requirements.
Is there a way to "force or activate an option on Gitlab" to give me
the choice to select the source branch when I create a new branch from
an issue?
yes you can always change your source branch. if you are creating a new branch github, first select the branch you want to make source, and then create new branch it will show like this:
other wise you can use git command line Interface.. its very powerful then GUI.
see the git command line.
Given the current version of Gitlab, you can now specify the branch source when creating the branch and merge request from an existing issue:
I accidentally pushed my local master to a branch called origin on gitlab and now it is the default. Is there a way to rename this branch or set a new master branch to master?
in the GitLab Enterprise Edition 12.2.0-pre you have to use following:
Setting → Repository → Default Branch ( expand it) and change the default branch Here
In 8.0+ it looks like this was moved into the project. If you open your project and go to the gear icon on the right, then "Edit Project" you can set the default branch for the project.
To change default branch in GitLab:
1. Settings > General > General project settings > Expand
2. Default Branch > Change your project default branch
3. Save changes
For gitlab v10+ (as of Sept 2018), this has moved to settings-> repository -> default branch
As stated by #Luke this is still valid as on 4/1/2021
To change the default branch in Gitlab 7.7.2:
Click Settings in the left-hand bar
Change the Default Branch to the desired branch
Click Save Changes.
In Gitlab version v11.4.4-ee, you can:
Setting
Repository
Default Branch
Settings
General
General Project Settings
In the latest GitLab Community Edition version 9.2.2.:
You have to click on 'Settings' tab located at right most on tabs
panel after opening the project.
Under 'Settings' you will get section 'Default Branch' dropdown
which will give you all branches for the repository. Select the desired branch.
Scroll down to hit green colored 'Save changes' button located just
after 'Project Avatar'.
Please refer image below:
See also GitLab 13.6 (November 2020)
Customize the initial branch name for new projects within a group
When creating a new Git repository, the first branch created is named master by default.
In coordination with the Git project, broader community, and other Git vendors, GitLab has been listening to the development community’s feedback on determining a more descriptive and inclusive name for the default branch, and is now offering users options to change the name of the default branch name for their repositories.
Previously, we shipped the ability to customize the initial branch name at the instance-level and as part of 13.6, GitLab now allows group administrators to configure the default branch name for new repositories created through the GitLab interface.
See Documentation and Issue.
GitLab 13.9 (Feb 2021) details:
Git default branch name change
Every Git repository has an initial branch. It’s the first branch to be created automatically when you create a new repository.
By default, this initial branch is named master.
Git version 2.31.0 (scheduled for release March 15, 2021) will change the default branch name in Git from master to main.
In coordination with the Git project and the broader community, GitLab will be changing the default branch name for new projects on both our SaaS (GitLab.com) and self-managed offerings starting with GitLab 14.0.
This will not affect existing projects.
For more information, see the related epic and the Git mailing list discussion.
Deprecation date: Apr 22, 2021
As promise, with GitLab 13.11 (Apr. 2021):
Git default branch name change
Every Git repository has an initial branch.
It’s the first branch to be created automatically when you create a new repository.
By default, this initial branch is named master.
Future Git versions will change the default branch name in Git from master to main.
In coordination with the Git project and the broader community, GitLab will be changing the default branch name for new projects on both our SaaS (GitLab.com) and self-managed offerings starting with GitLab 14.0.
This will not affect existing projects.
GitLab has already introduced changes that allow users to change the default branch name both at the instance-level (for self-managed users) and at the group-level (for both SaaS and self-managed users).
We encourage users to make use of these features to set default branch names on new projects.
For more information, see the related epic and related blog post.
Settings > Repository > Default Branch
First I needed to remote into my server with ssh. If someone has a non ssh way of doing this please post.
I found my bare repositories at
cd /var/opt/gitlab/git-data/repositories/group-name/project-name.git
used
git branch
to see the wrong active branch
git symbolic-ref HEAD refs/heads/master
to change the master to to be the branch called master then use the web interface and "git branch" to confirm.
In Gitlab CE 9.0, You can change the default branch from the Settings Tab in a repository's header.
For GitLab 11.5.0-ee, go to
https://gitlab.com/<username>/<project name>/settings/repository.
You should see:
Default Branch
Select the branch you want to set as the default for this project. All merge requests and commits will automatically be made against this branch unless you specify a different one.
Click Expand, select a branch, and click Save Changes.
December, 2022
settings >> repository >> Branch defaults :: expand and set your desire branch.
We are using gitlab to serve our git repositories. Now I've cloned our master project to track my own changes and now I would like to create a merge request to the master again. When I look at the youtube movie, I can see Job selecting a user to assign the merge request to. In my version this whole drop-down box is missing.
Is this because I use version 6.8.2, is it because I do a merge request from a fork? What is happening here?
I would like to be able to select an other team member to review the commit before merging it into the master project.
You're able to assign it to someone on the next screen. We agree that this flow is not ideal so we changed it in 6.9 https://www.gitlab.com/2014/05/22/gitlab-6-9-ee-and-ce-release/